parser.pas 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  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,aasmdata,
  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. current_module:=nil;
  58. compiled_module:=nil;
  59. current_asmdata:=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_amiga:
  96. include(supported_calling_conventions,pocall_syscall);
  97. system_powerpc_morphos:
  98. include(supported_calling_conventions,pocall_syscall);
  99. system_m68k_amiga:
  100. include(supported_calling_conventions,pocall_syscall);
  101. end;
  102. end;
  103. procedure doneparser;
  104. begin
  105. { Reset current compiling info, so destroy routines can't
  106. reference the data that might already be destroyed }
  107. current_module:=nil;
  108. compiled_module:=nil;
  109. current_procinfo:=nil;
  110. current_asmdata:=nil;
  111. SetCompileModule(nil);
  112. { unload units }
  113. if assigned(loaded_units) then
  114. begin
  115. loaded_units.free;
  116. loaded_units:=nil;
  117. end;
  118. if assigned(usedunits) then
  119. begin
  120. usedunits.free;
  121. usedunits:=nil;
  122. end;
  123. { if there was an error in the scanner, the scanner is
  124. still assinged }
  125. if assigned(current_scanner) then
  126. begin
  127. current_scanner.free;
  128. current_scanner:=nil;
  129. end;
  130. { close scanner }
  131. DoneScanner;
  132. { close ppas,deffile }
  133. asmres.free;
  134. deffile.free;
  135. { free list of .o files }
  136. SmartLinkOFiles.Free;
  137. end;
  138. {$ifdef PREPROCWRITE}
  139. procedure preprocess(const filename:string);
  140. var
  141. i : longint;
  142. begin
  143. new(preprocfile,init('pre'));
  144. { initialize a module }
  145. current_module:=new(pmodule,init(filename,false));
  146. macrosymtablestack:= initialmacrosymtable;
  147. current_module.localmacrosymtable:= tmacrosymtable.create(false);
  148. current_module.localmacrosymtable.next:= initialmacrosymtable;
  149. macrosymtablestack:= current_module.localmacrosymtable;
  150. main_module:=current_module;
  151. { startup scanner, and save in current_module }
  152. current_scanner:=new(pscannerfile,Init(filename));
  153. current_module.scanner:=current_scanner;
  154. { loop until EOF is found }
  155. repeat
  156. current_scanner^.readtoken(true);
  157. preprocfile^.AddSpace;
  158. case token of
  159. _ID :
  160. begin
  161. preprocfile^.Add(orgpattern);
  162. end;
  163. _REALNUMBER,
  164. _INTCONST :
  165. preprocfile^.Add(pattern);
  166. _CSTRING :
  167. begin
  168. i:=0;
  169. while (i<length(pattern)) do
  170. begin
  171. inc(i);
  172. if pattern[i]='''' then
  173. begin
  174. insert('''',pattern,i);
  175. inc(i);
  176. end;
  177. end;
  178. preprocfile^.Add(''''+pattern+'''');
  179. end;
  180. _CCHAR :
  181. begin
  182. case pattern[1] of
  183. #39 :
  184. pattern:='''''''';
  185. #0..#31,
  186. #128..#255 :
  187. begin
  188. str(ord(pattern[1]),pattern);
  189. pattern:='#'+pattern;
  190. end;
  191. else
  192. pattern:=''''+pattern[1]+'''';
  193. end;
  194. preprocfile^.Add(pattern);
  195. end;
  196. _EOF :
  197. break;
  198. else
  199. preprocfile^.Add(tokeninfo^[token].str)
  200. end;
  201. until false;
  202. { free scanner }
  203. dispose(current_scanner,done);
  204. current_scanner:=nil;
  205. { close }
  206. dispose(preprocfile,done);
  207. end;
  208. {$endif PREPROCWRITE}
  209. {*****************************************************************************
  210. Compile a source file
  211. *****************************************************************************}
  212. procedure compile(const filename:string);
  213. type
  214. polddata=^tolddata;
  215. tolddata=record
  216. { scanner }
  217. oldidtoken,
  218. oldtoken : ttoken;
  219. oldtokenpos : tfileposinfo;
  220. oldc : char;
  221. oldpattern,
  222. oldorgpattern : string;
  223. old_block_type : tblock_type;
  224. { symtable }
  225. oldsymtablestack,
  226. oldmacrosymtablestack : tsymtablestack;
  227. oldaktprocsym : tprocsym;
  228. { cg }
  229. oldparse_only : boolean;
  230. { akt.. things }
  231. oldaktlocalswitches : tlocalswitches;
  232. oldaktmoduleswitches : tmoduleswitches;
  233. oldaktfilepos : tfileposinfo;
  234. oldaktpackrecords,
  235. oldaktpackenum : shortint;
  236. oldaktmaxfpuregisters : longint;
  237. oldaktalignment : talignmentinfo;
  238. oldaktoptimizecputype,
  239. oldaktcputype : tcputype;
  240. oldaktfputype : tfputype;
  241. oldaktasmmode : tasmmode;
  242. oldaktinterfacetype: tinterfacetypes;
  243. oldaktmodeswitches : tmodeswitches;
  244. oldaktoptimizerswitches : toptimizerswitches;
  245. old_compiled_module : tmodule;
  246. oldcurrent_procinfo : tprocinfo;
  247. oldaktdefproccall : tproccalloption;
  248. oldsourcecodepage : tcodepagestring;
  249. end;
  250. var
  251. olddata : polddata;
  252. begin
  253. inc(compile_level);
  254. parser_current_file:=filename;
  255. { Uses heap memory instead of placing everything on the
  256. stack. This is needed because compile() can be called
  257. recursively }
  258. new(olddata);
  259. with olddata^ do
  260. begin
  261. old_compiled_module:=compiled_module;
  262. { save symtable state }
  263. oldsymtablestack:=symtablestack;
  264. oldmacrosymtablestack:=macrosymtablestack;
  265. oldcurrent_procinfo:=current_procinfo;
  266. oldaktdefproccall:=aktdefproccall;
  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. oldsourcecodepage:=aktsourcecodepage;
  276. { save cg }
  277. oldparse_only:=parse_only;
  278. { save akt... state }
  279. { handle the postponed case first }
  280. if localswitcheschanged then
  281. begin
  282. aktlocalswitches:=nextaktlocalswitches;
  283. localswitcheschanged:=false;
  284. end;
  285. oldaktlocalswitches:=aktlocalswitches;
  286. oldaktmoduleswitches:=aktmoduleswitches;
  287. oldaktalignment:=aktalignment;
  288. oldaktpackenum:=aktpackenum;
  289. oldaktpackrecords:=aktpackrecords;
  290. oldaktfputype:=aktfputype;
  291. oldaktmaxfpuregisters:=aktmaxfpuregisters;
  292. oldaktcputype:=aktcputype;
  293. oldaktoptimizecputype:=aktoptimizecputype;
  294. oldaktasmmode:=aktasmmode;
  295. oldaktinterfacetype:=aktinterfacetype;
  296. oldaktfilepos:=aktfilepos;
  297. oldaktmodeswitches:=aktmodeswitches;
  298. oldaktoptimizerswitches:=aktoptimizerswitches;
  299. end;
  300. { reset parser, a previous fatal error could have left these variables in an unreliable state, this is
  301. important for the IDE }
  302. afterassignment:=false;
  303. in_args:=false;
  304. got_addrn:=false;
  305. getprocvardef:=nil;
  306. { show info }
  307. Message1(parser_i_compiling,filename);
  308. { reset symtable }
  309. symtablestack:=tsymtablestack.create;
  310. macrosymtablestack:=tsymtablestack.create;
  311. systemunit:=nil;
  312. aktdefproccall:=initdefproccall;
  313. aktexceptblock:=0;
  314. exceptblockcounter:=0;
  315. aktmaxfpuregisters:=-1;
  316. { reset the unit or create a new program }
  317. { a unit compiled at command line must be inside the loaded_unit list }
  318. if (compile_level=1) then
  319. begin
  320. if assigned(current_module) then
  321. internalerror(200501158);
  322. current_module:=tppumodule.create(nil,filename,'',false);
  323. addloadedunit(current_module);
  324. main_module:=current_module;
  325. current_module.state:=ms_compile;
  326. end;
  327. if not(assigned(current_module) and
  328. (current_module.state in [ms_compile,ms_second_compile])) then
  329. internalerror(200212281);
  330. { Set the module to use for verbose }
  331. compiled_module:=current_module;
  332. SetCompileModule(current_module);
  333. Fillchar(aktfilepos,0,sizeof(aktfilepos));
  334. { Load current state from the init values }
  335. aktlocalswitches:=initlocalswitches;
  336. aktmoduleswitches:=initmoduleswitches;
  337. aktmodeswitches:=initmodeswitches;
  338. aktoptimizerswitches:=initoptimizerswitches;
  339. {$IFDEF Testvarsets}
  340. aktsetalloc:=initsetalloc;
  341. {$ENDIF}
  342. aktalignment:=initalignment;
  343. aktfputype:=initfputype;
  344. aktpackenum:=initpackenum;
  345. aktpackrecords:=0;
  346. aktcputype:=initcputype;
  347. aktoptimizecputype:=initoptimizecputype;
  348. aktasmmode:=initasmmode;
  349. aktinterfacetype:=initinterfacetype;
  350. { load current asmdata from current_module }
  351. current_asmdata:=TAsmData(current_module.asmdata);
  352. { startup scanner and load the first file }
  353. current_scanner:=tscannerfile.Create(filename);
  354. current_scanner.firstfile;
  355. current_module.scanner:=current_scanner;
  356. { init macros before anything in the file is parsed.}
  357. current_module.localmacrosymtable:= tmacrosymtable.create(false);
  358. macrosymtablestack.push(initialmacrosymtable);
  359. macrosymtablestack.push(current_module.localmacrosymtable);
  360. { read the first token }
  361. current_scanner.readtoken(false);
  362. { If the compile level > 1 we get a nice "unit expected" error
  363. message if we are trying to use a program as unit.}
  364. try
  365. try
  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. except
  374. on ECompilerAbort do
  375. raise;
  376. on Exception do
  377. begin
  378. { Increase errorcounter to prevent some
  379. checks during cleanup }
  380. inc(status.errorcount);
  381. raise;
  382. end;
  383. end;
  384. finally
  385. if assigned(current_module) then
  386. begin
  387. { module is now compiled }
  388. tppumodule(current_module).state:=ms_compiled;
  389. { free ppu }
  390. if assigned(tppumodule(current_module).ppufile) then
  391. begin
  392. tppumodule(current_module).ppufile.free;
  393. tppumodule(current_module).ppufile:=nil;
  394. end;
  395. { free asmdata }
  396. if assigned(current_module.asmdata) then
  397. begin
  398. current_module.asmdata.free;
  399. current_module.asmdata:=nil;
  400. end;
  401. { free scanner }
  402. if assigned(current_module.scanner) then
  403. begin
  404. if current_scanner=tscannerfile(current_module.scanner) then
  405. current_scanner:=nil;
  406. tscannerfile(current_module.scanner).free;
  407. current_module.scanner:=nil;
  408. end;
  409. { free symtable stack }
  410. if assigned(symtablestack) then
  411. begin
  412. symtablestack.free;
  413. symtablestack:=nil;
  414. end;
  415. if assigned(macrosymtablestack) then
  416. begin
  417. macrosymtablestack.free;
  418. macrosymtablestack:=nil;
  419. end;
  420. end;
  421. with olddata^ do
  422. begin
  423. { restore scanner }
  424. c:=oldc;
  425. pattern:=oldpattern;
  426. orgpattern:=oldorgpattern;
  427. token:=oldtoken;
  428. idtoken:=oldidtoken;
  429. akttokenpos:=oldtokenpos;
  430. block_type:=old_block_type;
  431. { restore cg }
  432. parse_only:=oldparse_only;
  433. { asm data }
  434. if assigned(old_compiled_module) then
  435. current_asmdata:=tasmdata(old_compiled_module.asmdata)
  436. else
  437. current_asmdata:=nil;
  438. { restore previous scanner }
  439. if assigned(old_compiled_module) then
  440. current_scanner:=tscannerfile(old_compiled_module.scanner)
  441. else
  442. current_scanner:=nil;
  443. if assigned(current_scanner) then
  444. parser_current_file:=current_scanner.inputfile.name^;
  445. { restore symtable state }
  446. symtablestack:=oldsymtablestack;
  447. macrosymtablestack:=oldmacrosymtablestack;
  448. aktdefproccall:=oldaktdefproccall;
  449. current_procinfo:=oldcurrent_procinfo;
  450. aktsourcecodepage:=oldsourcecodepage;
  451. aktlocalswitches:=oldaktlocalswitches;
  452. aktmoduleswitches:=oldaktmoduleswitches;
  453. aktalignment:=oldaktalignment;
  454. aktpackenum:=oldaktpackenum;
  455. aktpackrecords:=oldaktpackrecords;
  456. aktmaxfpuregisters:=oldaktmaxfpuregisters;
  457. aktcputype:=oldaktcputype;
  458. aktoptimizecputype:=oldaktoptimizecputype;
  459. aktfputype:=oldaktfputype;
  460. aktasmmode:=oldaktasmmode;
  461. aktinterfacetype:=oldaktinterfacetype;
  462. aktfilepos:=oldaktfilepos;
  463. aktmodeswitches:=oldaktmodeswitches;
  464. aktoptimizerswitches:=oldaktoptimizerswitches;
  465. aktexceptblock:=0;
  466. exceptblockcounter:=0;
  467. end;
  468. { Shut down things when the last file is compiled succesfull }
  469. if (compile_level=1) and
  470. (status.errorcount=0) then
  471. begin
  472. parser_current_file:='';
  473. { Close script }
  474. if (not AsmRes.Empty) then
  475. begin
  476. Message1(exec_i_closing_script,AsmRes.Fn);
  477. AsmRes.WriteToDisk;
  478. end;
  479. { do not create browsers on errors !! }
  480. if status.errorcount=0 then
  481. begin
  482. {$ifdef BrowserLog}
  483. { Write Browser Log }
  484. if (cs_browser_log in aktglobalswitches) and
  485. (cs_browser in aktmoduleswitches) then
  486. begin
  487. if browserlog.elements_to_list.empty then
  488. begin
  489. Message1(parser_i_writing_browser_log,browserlog.Fname);
  490. WriteBrowserLog;
  491. end
  492. else
  493. browserlog.list_elements;
  494. end;
  495. {$endif BrowserLog}
  496. { Write Browser Collections, also used by the TextMode IDE to
  497. retrieve a list of sourcefiles }
  498. do_extractsymbolinfo{$ifdef FPC}(){$endif};
  499. end;
  500. end;
  501. dec(compile_level);
  502. compiled_module:=olddata^.old_compiled_module;
  503. SetCompileModule(compiled_module);
  504. dispose(olddata);
  505. end;
  506. end;
  507. end.