parser.pas 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. This unit does the parsing process
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit parser;
  18. {$i fpcdefs.inc}
  19. interface
  20. {$ifdef PREPROCWRITE}
  21. procedure preprocess(const filename:string);
  22. {$endif PREPROCWRITE}
  23. procedure compile(const filename:string);
  24. procedure initparser;
  25. procedure doneparser;
  26. implementation
  27. uses
  28. {$IFNDEF MACOS_USE_FAKE_SYSUTILS}
  29. sysutils,
  30. {$ENDIF MACOS_USE_FAKE_SYSUTILS}
  31. cutils,cclasses,
  32. globtype,version,tokens,systems,globals,verbose,
  33. symbase,symtable,symsym,
  34. finput,fmodule,fppu,
  35. aasmbase,aasmtai,
  36. cgbase,
  37. script,gendef,
  38. {$ifdef BrowserCol}
  39. browcol,
  40. {$endif BrowserCol}
  41. {$ifdef BrowserLog}
  42. browlog,
  43. {$endif BrowserLog}
  44. comphook,
  45. scanner,scandir,
  46. pbase,ptype,psystem,pmodules,psub,
  47. cresstr,cpuinfo,procinfo;
  48. procedure initparser;
  49. begin
  50. { ^M means a string or a char, because we don't parse a }
  51. { type declaration }
  52. ignore_equal:=false;
  53. { we didn't parse a object or class declaration }
  54. { and no function header }
  55. testcurobject:=0;
  56. { Current compiled module/proc }
  57. objectlibrary:=nil;
  58. current_module:=nil;
  59. compiled_module:=nil;
  60. current_procinfo:=nil;
  61. SetCompileModule(nil);
  62. loaded_units:=TLinkedList.Create;
  63. usedunits:=TLinkedList.Create;
  64. { global switches }
  65. aktglobalswitches:=initglobalswitches;
  66. aktsourcecodepage:=initsourcecodepage;
  67. { initialize scanner }
  68. InitScanner;
  69. InitScannerDirectives;
  70. { scanner }
  71. c:=#0;
  72. pattern:='';
  73. orgpattern:='';
  74. current_scanner:=nil;
  75. { register all nodes and tais }
  76. registernodes;
  77. registertais;
  78. { memory sizes }
  79. if stacksize=0 then
  80. stacksize:=target_info.stacksize;
  81. { open assembler response }
  82. if cs_link_on_target in aktglobalswitches then
  83. GenerateAsmRes(outputexedir+inputfile+'_ppas')
  84. else
  85. GenerateAsmRes(outputexedir+'ppas');
  86. { open deffile }
  87. DefFile:=TDefFile.Create(outputexedir+inputfile+target_info.defext);
  88. { list of generated .o files, so the linker can remove them }
  89. SmartLinkOFiles:=TStringList.Create;
  90. { codegen }
  91. if paraprintnodetree<>0 then
  92. printnode_reset;
  93. { target specific stuff }
  94. case target_info.system of
  95. system_powerpc_morphos:
  96. include(supported_calling_conventions,pocall_syscall);
  97. system_m68k_amiga:
  98. include(supported_calling_conventions,pocall_syscall);
  99. end;
  100. end;
  101. procedure doneparser;
  102. begin
  103. { Reset current compiling info, so destroy routines can't
  104. reference the data that might already be destroyed }
  105. objectlibrary:=nil;
  106. current_module:=nil;
  107. compiled_module:=nil;
  108. current_procinfo:=nil;
  109. SetCompileModule(nil);
  110. { unload units }
  111. loaded_units.free;
  112. usedunits.free;
  113. { if there was an error in the scanner, the scanner is
  114. still assinged }
  115. if assigned(current_scanner) then
  116. begin
  117. current_scanner.free;
  118. current_scanner:=nil;
  119. end;
  120. { close scanner }
  121. DoneScanner;
  122. { close ppas,deffile }
  123. asmres.free;
  124. deffile.free;
  125. { free list of .o files }
  126. SmartLinkOFiles.Free;
  127. end;
  128. {$ifdef PREPROCWRITE}
  129. procedure preprocess(const filename:string);
  130. var
  131. i : longint;
  132. begin
  133. new(preprocfile,init('pre'));
  134. { initialize a module }
  135. current_module:=new(pmodule,init(filename,false));
  136. macrosymtablestack:= initialmacrosymtable;
  137. current_module.localmacrosymtable:= tmacrosymtable.create(false);
  138. current_module.localmacrosymtable.next:= initialmacrosymtable;
  139. macrosymtablestack:= current_module.localmacrosymtable;
  140. ConsolidateMode;
  141. main_module:=current_module;
  142. { startup scanner, and save in current_module }
  143. current_scanner:=new(pscannerfile,Init(filename));
  144. current_module.scanner:=current_scanner;
  145. { loop until EOF is found }
  146. repeat
  147. current_scanner^.readtoken;
  148. preprocfile^.AddSpace;
  149. case token of
  150. _ID :
  151. begin
  152. preprocfile^.Add(orgpattern);
  153. end;
  154. _REALNUMBER,
  155. _INTCONST :
  156. preprocfile^.Add(pattern);
  157. _CSTRING :
  158. begin
  159. i:=0;
  160. while (i<length(pattern)) do
  161. begin
  162. inc(i);
  163. if pattern[i]='''' then
  164. begin
  165. insert('''',pattern,i);
  166. inc(i);
  167. end;
  168. end;
  169. preprocfile^.Add(''''+pattern+'''');
  170. end;
  171. _CCHAR :
  172. begin
  173. case pattern[1] of
  174. #39 :
  175. pattern:='''''''';
  176. #0..#31,
  177. #128..#255 :
  178. begin
  179. str(ord(pattern[1]),pattern);
  180. pattern:='#'+pattern;
  181. end;
  182. else
  183. pattern:=''''+pattern[1]+'''';
  184. end;
  185. preprocfile^.Add(pattern);
  186. end;
  187. _EOF :
  188. break;
  189. else
  190. preprocfile^.Add(tokeninfo^[token].str)
  191. end;
  192. until false;
  193. { free scanner }
  194. dispose(current_scanner,done);
  195. current_scanner:=nil;
  196. { close }
  197. dispose(preprocfile,done);
  198. end;
  199. {$endif PREPROCWRITE}
  200. {*****************************************************************************
  201. Create information for a new module
  202. *****************************************************************************}
  203. procedure init_module;
  204. var
  205. i : Tasmlist;
  206. begin
  207. exprasmlist:=taasmoutput.create;
  208. for i:=low(Tasmlist) to high(Tasmlist) do
  209. asmlist[i]:=Taasmoutput.create;
  210. { PIC data }
  211. {$ifdef powerpc}
  212. if target_info.system=system_powerpc_darwin then
  213. asmlist[al_picdata].concat(tai_directive.create(asd_non_lazy_symbol_pointer,''));
  214. {$endif powerpc}
  215. { Resource strings }
  216. cresstr.resourcestrings:=Tresourcestrings.Create;
  217. { use the librarydata from current_module }
  218. objectlibrary:=current_module.librarydata;
  219. end;
  220. procedure done_module;
  221. var
  222. {$ifdef MEMDEBUG}
  223. d : tmemdebug;
  224. {$endif}
  225. i:Tasmlist;
  226. begin
  227. {$ifdef MEMDEBUG}
  228. d:=tmemdebug.create(current_module.modulename^+' - asmlists');
  229. {$endif}
  230. for i:=low(Tasmlist) to high(Tasmlist) do
  231. if asmlist[i]<>nil then
  232. asmlist[i].free;
  233. {$ifdef MEMDEBUG}
  234. d.free;
  235. {$endif}
  236. { resource strings }
  237. cresstr.resourcestrings.free;
  238. objectlibrary:=nil;
  239. end;
  240. {*****************************************************************************
  241. Compile a source file
  242. *****************************************************************************}
  243. procedure compile(const filename:string);
  244. type
  245. polddata=^tolddata;
  246. tolddata=record
  247. { scanner }
  248. oldidtoken,
  249. oldtoken : ttoken;
  250. oldtokenpos : tfileposinfo;
  251. oldc : char;
  252. oldpattern,
  253. oldorgpattern : string;
  254. old_block_type : tblock_type;
  255. { symtable }
  256. oldrefsymtable,
  257. olddefaultsymtablestack,
  258. oldsymtablestack : tsymtable;
  259. olddefaultmacrosymtablestack,
  260. oldmacrosymtablestack : tsymtable;
  261. oldaktprocsym : tprocsym;
  262. { cg }
  263. oldparse_only : boolean;
  264. { asmlists }
  265. oldexprasmlist:Taasmoutput;
  266. oldasmlist:array[Tasmlist] of Taasmoutput;
  267. oldobjectlibrary : tasmlibrarydata;
  268. { al_resourcestrings }
  269. Oldresourcestrings : tresourcestrings;
  270. { akt.. things }
  271. oldaktlocalswitches : tlocalswitches;
  272. oldaktmoduleswitches : tmoduleswitches;
  273. oldaktfilepos : tfileposinfo;
  274. oldaktpackrecords,
  275. oldaktpackenum : shortint;
  276. oldaktmaxfpuregisters : longint;
  277. oldaktalignment : talignmentinfo;
  278. oldaktspecificoptprocessor,
  279. oldaktoptprocessor : tprocessors;
  280. oldaktfputype : tfputype;
  281. oldaktasmmode : tasmmode;
  282. oldaktinterfacetype: tinterfacetypes;
  283. oldaktmodeswitches : tmodeswitches;
  284. old_compiled_module : tmodule;
  285. oldcurrent_procinfo : tprocinfo;
  286. oldaktdefproccall : tproccalloption;
  287. oldsourcecodepage : tcodepagestring;
  288. end;
  289. var
  290. olddata : polddata;
  291. begin
  292. inc(compile_level);
  293. parser_current_file:=filename;
  294. { Uses heap memory instead of placing everything on the
  295. stack. This is needed because compile() can be called
  296. recursively }
  297. new(olddata);
  298. with olddata^ do
  299. begin
  300. old_compiled_module:=compiled_module;
  301. { save symtable state }
  302. oldsymtablestack:=symtablestack;
  303. oldmacrosymtablestack:=macrosymtablestack;
  304. olddefaultsymtablestack:=defaultsymtablestack;
  305. olddefaultmacrosymtablestack:=defaultmacrosymtablestack;
  306. oldrefsymtable:=refsymtable;
  307. oldcurrent_procinfo:=current_procinfo;
  308. oldaktdefproccall:=aktdefproccall;
  309. { save scanner state }
  310. oldc:=c;
  311. oldpattern:=pattern;
  312. oldorgpattern:=orgpattern;
  313. oldtoken:=token;
  314. oldidtoken:=idtoken;
  315. old_block_type:=block_type;
  316. oldtokenpos:=akttokenpos;
  317. oldsourcecodepage:=aktsourcecodepage;
  318. { save cg }
  319. oldparse_only:=parse_only;
  320. { save assembler lists }
  321. oldasmlist:=asmlist;
  322. oldexprasmlist:=exprasmlist;
  323. oldobjectlibrary:=objectlibrary;
  324. Oldresourcestrings:=resourcestrings;
  325. { save akt... state }
  326. { handle the postponed case first }
  327. if localswitcheschanged then
  328. begin
  329. aktlocalswitches:=nextaktlocalswitches;
  330. localswitcheschanged:=false;
  331. end;
  332. oldaktlocalswitches:=aktlocalswitches;
  333. oldaktmoduleswitches:=aktmoduleswitches;
  334. oldaktalignment:=aktalignment;
  335. oldaktpackenum:=aktpackenum;
  336. oldaktpackrecords:=aktpackrecords;
  337. oldaktfputype:=aktfputype;
  338. oldaktmaxfpuregisters:=aktmaxfpuregisters;
  339. oldaktoptprocessor:=aktoptprocessor;
  340. oldaktspecificoptprocessor:=aktspecificoptprocessor;
  341. oldaktasmmode:=aktasmmode;
  342. oldaktinterfacetype:=aktinterfacetype;
  343. oldaktfilepos:=aktfilepos;
  344. oldaktmodeswitches:=aktmodeswitches;
  345. end;
  346. { show info }
  347. Message1(parser_i_compiling,filename);
  348. { reset symtable }
  349. symtablestack:=nil;
  350. macrosymtablestack:=nil;
  351. defaultsymtablestack:=nil;
  352. defaultmacrosymtablestack:=nil;
  353. systemunit:=nil;
  354. refsymtable:=nil;
  355. aktdefproccall:=initdefproccall;
  356. registerdef:=true;
  357. aktexceptblock:=0;
  358. exceptblockcounter:=0;
  359. aktmaxfpuregisters:=-1;
  360. { reset the unit or create a new program }
  361. { a unit compiled at command line must be inside the loaded_unit list }
  362. if (compile_level=1) then
  363. begin
  364. if assigned(current_module) then
  365. internalerror(200501158);
  366. current_module:=tppumodule.create(nil,filename,'',false);
  367. addloadedunit(current_module);
  368. main_module:=current_module;
  369. current_module.state:=ms_compile;
  370. end;
  371. if not(assigned(current_module) and
  372. (current_module.state in [ms_compile,ms_second_compile])) then
  373. internalerror(200212281);
  374. { Set the module to use for verbose }
  375. compiled_module:=current_module;
  376. SetCompileModule(current_module);
  377. Fillchar(aktfilepos,0,sizeof(aktfilepos));
  378. { Load current state from the init values }
  379. aktlocalswitches:=initlocalswitches;
  380. aktmoduleswitches:=initmoduleswitches;
  381. aktmodeswitches:=initmodeswitches;
  382. {$IFDEF Testvarsets}
  383. aktsetalloc:=initsetalloc;
  384. {$ENDIF}
  385. aktalignment:=initalignment;
  386. aktfputype:=initfputype;
  387. aktpackenum:=initpackenum;
  388. aktpackrecords:=0;
  389. aktoptprocessor:=initoptprocessor;
  390. aktspecificoptprocessor:=initspecificoptprocessor;
  391. aktasmmode:=initasmmode;
  392. aktinterfacetype:=initinterfacetype;
  393. { startup scanner and load the first file }
  394. current_scanner:=tscannerfile.Create(filename);
  395. current_scanner.firstfile;
  396. current_module.scanner:=current_scanner;
  397. { init macros before anything in the file is parsed.}
  398. macrosymtablestack:= initialmacrosymtable;
  399. current_module.localmacrosymtable:= tmacrosymtable.create(false);
  400. current_module.localmacrosymtable.next:= initialmacrosymtable;
  401. macrosymtablestack:= current_module.localmacrosymtable;
  402. { read the first token }
  403. current_scanner.readtoken;
  404. { init code generator for a new module }
  405. init_module;
  406. { If the compile level > 1 we get a nice "unit expected" error
  407. message if we are trying to use a program as unit.}
  408. try
  409. try
  410. if (token=_UNIT) or (compile_level>1) then
  411. begin
  412. current_module.is_unit:=true;
  413. proc_unit;
  414. end
  415. else
  416. proc_program(token=_LIBRARY);
  417. except
  418. on ECompilerAbort do
  419. raise;
  420. on Exception do
  421. begin
  422. { Increase errorcounter to prevent some
  423. checks during cleanup }
  424. inc(status.errorcount);
  425. raise;
  426. end;
  427. end;
  428. finally
  429. { restore old state }
  430. done_module;
  431. if assigned(current_module) then
  432. begin
  433. { module is now compiled }
  434. tppumodule(current_module).state:=ms_compiled;
  435. { free ppu }
  436. if assigned(tppumodule(current_module).ppufile) then
  437. begin
  438. tppumodule(current_module).ppufile.free;
  439. tppumodule(current_module).ppufile:=nil;
  440. end;
  441. { free scanner }
  442. if assigned(current_module.scanner) then
  443. begin
  444. if current_scanner=tscannerfile(current_module.scanner) then
  445. current_scanner:=nil;
  446. tscannerfile(current_module.scanner).free;
  447. current_module.scanner:=nil;
  448. end;
  449. end;
  450. if (compile_level>1) then
  451. begin
  452. with olddata^ do
  453. begin
  454. { restore scanner }
  455. c:=oldc;
  456. pattern:=oldpattern;
  457. orgpattern:=oldorgpattern;
  458. token:=oldtoken;
  459. idtoken:=oldidtoken;
  460. akttokenpos:=oldtokenpos;
  461. block_type:=old_block_type;
  462. { restore cg }
  463. parse_only:=oldparse_only;
  464. { restore asmlists }
  465. exprasmlist:=oldexprasmlist;
  466. asmlist:=oldasmlist;
  467. { object data }
  468. resourcestrings:=oldresourcestrings;
  469. objectlibrary:=oldobjectlibrary;
  470. { restore previous scanner }
  471. if assigned(old_compiled_module) then
  472. current_scanner:=tscannerfile(old_compiled_module.scanner)
  473. else
  474. current_scanner:=nil;
  475. if assigned(current_scanner) then
  476. parser_current_file:=current_scanner.inputfile.name^;
  477. { restore symtable state }
  478. refsymtable:=oldrefsymtable;
  479. symtablestack:=oldsymtablestack;
  480. macrosymtablestack:=oldmacrosymtablestack;
  481. defaultsymtablestack:=olddefaultsymtablestack;
  482. defaultmacrosymtablestack:=olddefaultmacrosymtablestack;
  483. aktdefproccall:=oldaktdefproccall;
  484. current_procinfo:=oldcurrent_procinfo;
  485. aktsourcecodepage:=oldsourcecodepage;
  486. aktlocalswitches:=oldaktlocalswitches;
  487. aktmoduleswitches:=oldaktmoduleswitches;
  488. aktalignment:=oldaktalignment;
  489. aktpackenum:=oldaktpackenum;
  490. aktpackrecords:=oldaktpackrecords;
  491. aktmaxfpuregisters:=oldaktmaxfpuregisters;
  492. aktoptprocessor:=oldaktoptprocessor;
  493. aktspecificoptprocessor:=oldaktspecificoptprocessor;
  494. aktfputype:=oldaktfputype;
  495. aktasmmode:=oldaktasmmode;
  496. aktinterfacetype:=oldaktinterfacetype;
  497. aktfilepos:=oldaktfilepos;
  498. aktmodeswitches:=oldaktmodeswitches;
  499. aktexceptblock:=0;
  500. exceptblockcounter:=0;
  501. end;
  502. end
  503. else
  504. begin
  505. { Shut down things when the last file is compiled succesfull }
  506. if (compile_level=1) and
  507. (status.errorcount=0) then
  508. begin
  509. parser_current_file:='';
  510. { Close script }
  511. if (not AsmRes.Empty) then
  512. begin
  513. Message1(exec_i_closing_script,AsmRes.Fn);
  514. AsmRes.WriteToDisk;
  515. end;
  516. { do not create browsers on errors !! }
  517. if status.errorcount=0 then
  518. begin
  519. {$ifdef BrowserLog}
  520. { Write Browser Log }
  521. if (cs_browser_log in aktglobalswitches) and
  522. (cs_browser in aktmoduleswitches) then
  523. begin
  524. if browserlog.elements_to_list.empty then
  525. begin
  526. Message1(parser_i_writing_browser_log,browserlog.Fname);
  527. WriteBrowserLog;
  528. end
  529. else
  530. browserlog.list_elements;
  531. end;
  532. {$endif BrowserLog}
  533. { Write Browser Collections, also used by the TextMode IDE to
  534. retrieve a list of sourcefiles }
  535. do_extractsymbolinfo{$ifdef FPC}(){$endif};
  536. end;
  537. end;
  538. end;
  539. dec(compile_level);
  540. compiled_module:=olddata^.old_compiled_module;
  541. SetCompileModule(compiled_module);
  542. dispose(olddata);
  543. end;
  544. end;
  545. end.