2
0

parser.pas 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  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. { al_resourcestrings }
  231. Oldresourcestrings : tresourcestrings;
  232. { akt.. things }
  233. oldaktlocalswitches : tlocalswitches;
  234. oldaktmoduleswitches : tmoduleswitches;
  235. oldaktfilepos : tfileposinfo;
  236. oldaktpackrecords,
  237. oldaktpackenum : shortint;
  238. oldaktmaxfpuregisters : longint;
  239. oldaktalignment : talignmentinfo;
  240. oldaktoptimizecputype,
  241. oldaktcputype : tcputype;
  242. oldaktfputype : tfputype;
  243. oldaktasmmode : tasmmode;
  244. oldaktinterfacetype: tinterfacetypes;
  245. oldaktmodeswitches : tmodeswitches;
  246. oldaktoptimizerswitches : toptimizerswitches;
  247. old_compiled_module : tmodule;
  248. oldcurrent_procinfo : tprocinfo;
  249. oldaktdefproccall : tproccalloption;
  250. oldsourcecodepage : tcodepagestring;
  251. end;
  252. var
  253. olddata : polddata;
  254. begin
  255. inc(compile_level);
  256. parser_current_file:=filename;
  257. { Uses heap memory instead of placing everything on the
  258. stack. This is needed because compile() can be called
  259. recursively }
  260. new(olddata);
  261. with olddata^ do
  262. begin
  263. old_compiled_module:=compiled_module;
  264. { save symtable state }
  265. oldsymtablestack:=symtablestack;
  266. oldmacrosymtablestack:=macrosymtablestack;
  267. oldcurrent_procinfo:=current_procinfo;
  268. oldaktdefproccall:=aktdefproccall;
  269. { save scanner state }
  270. oldc:=c;
  271. oldpattern:=pattern;
  272. oldorgpattern:=orgpattern;
  273. oldtoken:=token;
  274. oldidtoken:=idtoken;
  275. old_block_type:=block_type;
  276. oldtokenpos:=akttokenpos;
  277. oldsourcecodepage:=aktsourcecodepage;
  278. { save cg }
  279. oldparse_only:=parse_only;
  280. Oldresourcestrings:=resourcestrings;
  281. { save akt... state }
  282. { handle the postponed case first }
  283. if localswitcheschanged then
  284. begin
  285. aktlocalswitches:=nextaktlocalswitches;
  286. localswitcheschanged:=false;
  287. end;
  288. oldaktlocalswitches:=aktlocalswitches;
  289. oldaktmoduleswitches:=aktmoduleswitches;
  290. oldaktalignment:=aktalignment;
  291. oldaktpackenum:=aktpackenum;
  292. oldaktpackrecords:=aktpackrecords;
  293. oldaktfputype:=aktfputype;
  294. oldaktmaxfpuregisters:=aktmaxfpuregisters;
  295. oldaktcputype:=aktcputype;
  296. oldaktoptimizecputype:=aktoptimizecputype;
  297. oldaktasmmode:=aktasmmode;
  298. oldaktinterfacetype:=aktinterfacetype;
  299. oldaktfilepos:=aktfilepos;
  300. oldaktmodeswitches:=aktmodeswitches;
  301. oldaktoptimizerswitches:=aktoptimizerswitches;
  302. end;
  303. { reset parser, a previous fatal error could have left these variables in an unreliable state, this is
  304. important for the IDE }
  305. afterassignment:=false;
  306. in_args:=false;
  307. got_addrn:=false;
  308. getprocvardef:=nil;
  309. { show info }
  310. Message1(parser_i_compiling,filename);
  311. { reset symtable }
  312. symtablestack:=tsymtablestack.create;
  313. macrosymtablestack:=tsymtablestack.create;
  314. systemunit:=nil;
  315. aktdefproccall:=initdefproccall;
  316. aktexceptblock:=0;
  317. exceptblockcounter:=0;
  318. aktmaxfpuregisters:=-1;
  319. { reset the unit or create a new program }
  320. { a unit compiled at command line must be inside the loaded_unit list }
  321. if (compile_level=1) then
  322. begin
  323. if assigned(current_module) then
  324. internalerror(200501158);
  325. current_module:=tppumodule.create(nil,filename,'',false);
  326. addloadedunit(current_module);
  327. main_module:=current_module;
  328. current_module.state:=ms_compile;
  329. end;
  330. if not(assigned(current_module) and
  331. (current_module.state in [ms_compile,ms_second_compile])) then
  332. internalerror(200212281);
  333. { Set the module to use for verbose }
  334. compiled_module:=current_module;
  335. SetCompileModule(current_module);
  336. Fillchar(aktfilepos,0,sizeof(aktfilepos));
  337. { Load current state from the init values }
  338. aktlocalswitches:=initlocalswitches;
  339. aktmoduleswitches:=initmoduleswitches;
  340. aktmodeswitches:=initmodeswitches;
  341. aktoptimizerswitches:=initoptimizerswitches;
  342. {$IFDEF Testvarsets}
  343. aktsetalloc:=initsetalloc;
  344. {$ENDIF}
  345. aktalignment:=initalignment;
  346. aktfputype:=initfputype;
  347. aktpackenum:=initpackenum;
  348. aktpackrecords:=0;
  349. aktcputype:=initcputype;
  350. aktoptimizecputype:=initoptimizecputype;
  351. aktasmmode:=initasmmode;
  352. aktinterfacetype:=initinterfacetype;
  353. resourcestrings:=Tresourcestrings.Create;
  354. { load current asmdata from current_module }
  355. current_asmdata:=TAsmData(current_module.asmdata);
  356. { startup scanner and load the first file }
  357. current_scanner:=tscannerfile.Create(filename);
  358. current_scanner.firstfile;
  359. current_module.scanner:=current_scanner;
  360. { init macros before anything in the file is parsed.}
  361. current_module.localmacrosymtable:= tmacrosymtable.create(false);
  362. macrosymtablestack.push(initialmacrosymtable);
  363. macrosymtablestack.push(current_module.localmacrosymtable);
  364. { read the first token }
  365. current_scanner.readtoken(false);
  366. { If the compile level > 1 we get a nice "unit expected" error
  367. message if we are trying to use a program as unit.}
  368. try
  369. try
  370. if (token=_UNIT) or (compile_level>1) then
  371. begin
  372. current_module.is_unit:=true;
  373. proc_unit;
  374. end
  375. else
  376. proc_program(token=_LIBRARY);
  377. except
  378. on ECompilerAbort do
  379. raise;
  380. on Exception do
  381. begin
  382. { Increase errorcounter to prevent some
  383. checks during cleanup }
  384. inc(status.errorcount);
  385. raise;
  386. end;
  387. end;
  388. finally
  389. if assigned(current_module) then
  390. begin
  391. { module is now compiled }
  392. tppumodule(current_module).state:=ms_compiled;
  393. { free ppu }
  394. if assigned(tppumodule(current_module).ppufile) then
  395. begin
  396. tppumodule(current_module).ppufile.free;
  397. tppumodule(current_module).ppufile:=nil;
  398. end;
  399. resourcestrings.free;
  400. resourcestrings:=nil;
  401. { free asmdata }
  402. if assigned(current_module.asmdata) then
  403. begin
  404. current_module.asmdata.free;
  405. current_module.asmdata:=nil;
  406. end;
  407. { free scanner }
  408. if assigned(current_module.scanner) then
  409. begin
  410. if current_scanner=tscannerfile(current_module.scanner) then
  411. current_scanner:=nil;
  412. tscannerfile(current_module.scanner).free;
  413. current_module.scanner:=nil;
  414. end;
  415. { free symtable stack }
  416. if assigned(symtablestack) then
  417. begin
  418. symtablestack.free;
  419. symtablestack:=nil;
  420. end;
  421. if assigned(macrosymtablestack) then
  422. begin
  423. macrosymtablestack.free;
  424. macrosymtablestack:=nil;
  425. end;
  426. end;
  427. with olddata^ do
  428. begin
  429. { restore scanner }
  430. c:=oldc;
  431. pattern:=oldpattern;
  432. orgpattern:=oldorgpattern;
  433. token:=oldtoken;
  434. idtoken:=oldidtoken;
  435. akttokenpos:=oldtokenpos;
  436. block_type:=old_block_type;
  437. { restore cg }
  438. parse_only:=oldparse_only;
  439. resourcestrings:=oldresourcestrings;
  440. { asm data }
  441. if assigned(old_compiled_module) then
  442. current_asmdata:=tasmdata(old_compiled_module.asmdata)
  443. else
  444. current_asmdata:=nil;
  445. { restore previous scanner }
  446. if assigned(old_compiled_module) then
  447. current_scanner:=tscannerfile(old_compiled_module.scanner)
  448. else
  449. current_scanner:=nil;
  450. if assigned(current_scanner) then
  451. parser_current_file:=current_scanner.inputfile.name^;
  452. { restore symtable state }
  453. symtablestack:=oldsymtablestack;
  454. macrosymtablestack:=oldmacrosymtablestack;
  455. aktdefproccall:=oldaktdefproccall;
  456. current_procinfo:=oldcurrent_procinfo;
  457. aktsourcecodepage:=oldsourcecodepage;
  458. aktlocalswitches:=oldaktlocalswitches;
  459. aktmoduleswitches:=oldaktmoduleswitches;
  460. aktalignment:=oldaktalignment;
  461. aktpackenum:=oldaktpackenum;
  462. aktpackrecords:=oldaktpackrecords;
  463. aktmaxfpuregisters:=oldaktmaxfpuregisters;
  464. aktcputype:=oldaktcputype;
  465. aktoptimizecputype:=oldaktoptimizecputype;
  466. aktfputype:=oldaktfputype;
  467. aktasmmode:=oldaktasmmode;
  468. aktinterfacetype:=oldaktinterfacetype;
  469. aktfilepos:=oldaktfilepos;
  470. aktmodeswitches:=oldaktmodeswitches;
  471. aktoptimizerswitches:=oldaktoptimizerswitches;
  472. aktexceptblock:=0;
  473. exceptblockcounter:=0;
  474. end;
  475. { Shut down things when the last file is compiled succesfull }
  476. if (compile_level=1) and
  477. (status.errorcount=0) then
  478. begin
  479. parser_current_file:='';
  480. { Close script }
  481. if (not AsmRes.Empty) then
  482. begin
  483. Message1(exec_i_closing_script,AsmRes.Fn);
  484. AsmRes.WriteToDisk;
  485. end;
  486. { do not create browsers on errors !! }
  487. if status.errorcount=0 then
  488. begin
  489. {$ifdef BrowserLog}
  490. { Write Browser Log }
  491. if (cs_browser_log in aktglobalswitches) and
  492. (cs_browser in aktmoduleswitches) then
  493. begin
  494. if browserlog.elements_to_list.empty then
  495. begin
  496. Message1(parser_i_writing_browser_log,browserlog.Fname);
  497. WriteBrowserLog;
  498. end
  499. else
  500. browserlog.list_elements;
  501. end;
  502. {$endif BrowserLog}
  503. { Write Browser Collections, also used by the TextMode IDE to
  504. retrieve a list of sourcefiles }
  505. do_extractsymbolinfo{$ifdef FPC}(){$endif};
  506. end;
  507. end;
  508. dec(compile_level);
  509. compiled_module:=olddata^.old_compiled_module;
  510. SetCompileModule(compiled_module);
  511. dispose(olddata);
  512. end;
  513. end;
  514. end.