parser.pas 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  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 USE_FAKE_SYSUTILS}
  29. sysutils,
  30. {$ELSE}
  31. fksysutl,
  32. {$ENDIF}
  33. cutils,cclasses,
  34. globtype,version,tokens,systems,globals,verbose,switches,
  35. symbase,symtable,symdef,symsym,
  36. finput,fmodule,fppu,
  37. aasmbase,aasmtai,aasmdata,
  38. cgbase,
  39. script,gendef,
  40. comphook,
  41. scanner,scandir,
  42. pbase,ptype,psystem,pmodules,psub,ncgrtti,
  43. cresstr,cpuinfo,procinfo;
  44. procedure initparser;
  45. begin
  46. { we didn't parse a object or class declaration }
  47. { and no function header }
  48. testcurobject:=0;
  49. { Current compiled module/proc }
  50. set_current_module(nil);
  51. current_module:=nil;
  52. current_asmdata:=nil;
  53. current_procinfo:=nil;
  54. current_objectdef:=nil;
  55. loaded_units:=TLinkedList.Create;
  56. usedunits:=TLinkedList.Create;
  57. unloaded_units:=TLinkedList.Create;
  58. { global switches }
  59. current_settings.globalswitches:=init_settings.globalswitches;
  60. current_settings.sourcecodepage:=init_settings.sourcecodepage;
  61. { initialize scanner }
  62. InitScanner;
  63. InitScannerDirectives;
  64. { scanner }
  65. c:=#0;
  66. pattern:='';
  67. orgpattern:='';
  68. current_scanner:=nil;
  69. { register all nodes and tais }
  70. registernodes;
  71. registertais;
  72. { memory sizes }
  73. if stacksize=0 then
  74. stacksize:=target_info.stacksize;
  75. { RTTI writer }
  76. RTTIWriter:=TRTTIWriter.Create;
  77. { open assembler response }
  78. if cs_link_on_target in current_settings.globalswitches then
  79. GenerateAsmRes(outputexedir+ChangeFileExt(inputfilename,'_ppas'))
  80. else
  81. GenerateAsmRes(outputexedir+'ppas');
  82. { open deffile }
  83. DefFile:=TDefFile.Create(outputexedir+ChangeFileExt(inputfilename,target_info.defext));
  84. { list of generated .o files, so the linker can remove them }
  85. SmartLinkOFiles:=TCmdStrList.Create;
  86. { codegen }
  87. if paraprintnodetree<>0 then
  88. printnode_reset;
  89. { target specific stuff }
  90. case target_info.system of
  91. system_powerpc_amiga:
  92. include(supported_calling_conventions,pocall_syscall);
  93. system_powerpc_morphos:
  94. include(supported_calling_conventions,pocall_syscall);
  95. system_m68k_amiga:
  96. include(supported_calling_conventions,pocall_syscall);
  97. end;
  98. end;
  99. procedure doneparser;
  100. begin
  101. { Reset current compiling info, so destroy routines can't
  102. reference the data that might already be destroyed }
  103. set_current_module(nil);
  104. current_module:=nil;
  105. current_procinfo:=nil;
  106. current_asmdata:=nil;
  107. current_objectdef:=nil;
  108. { unload units }
  109. if assigned(loaded_units) then
  110. begin
  111. loaded_units.free;
  112. loaded_units:=nil;
  113. end;
  114. if assigned(usedunits) then
  115. begin
  116. usedunits.free;
  117. usedunits:=nil;
  118. end;
  119. if assigned(unloaded_units) then
  120. begin
  121. unloaded_units.free;
  122. unloaded_units:=nil;
  123. end;
  124. { if there was an error in the scanner, the scanner is
  125. still assinged }
  126. if assigned(current_scanner) then
  127. begin
  128. current_scanner.free;
  129. current_scanner:=nil;
  130. end;
  131. { close scanner }
  132. DoneScanner;
  133. RTTIWriter.free;
  134. { close ppas,deffile }
  135. asmres.free;
  136. deffile.free;
  137. { free list of .o files }
  138. SmartLinkOFiles.Free;
  139. end;
  140. {$ifdef PREPROCWRITE}
  141. procedure preprocess(const filename:string);
  142. var
  143. i : longint;
  144. begin
  145. new(preprocfile,init('pre'));
  146. { initialize a module }
  147. set_current_module(new(pmodule,init(filename,false)));
  148. macrosymtablestack:= initialmacrosymtable;
  149. current_module.localmacrosymtable:= tmacrosymtable.create(false);
  150. current_module.localmacrosymtable.next:= initialmacrosymtable;
  151. macrosymtablestack:= current_module.localmacrosymtable;
  152. main_module:=current_module;
  153. { startup scanner, and save in current_module }
  154. current_scanner:=new(pscannerfile,Init(filename));
  155. current_module.scanner:=current_scanner;
  156. { loop until EOF is found }
  157. repeat
  158. current_scanner^.readtoken(true);
  159. preprocfile^.AddSpace;
  160. case token of
  161. _ID :
  162. begin
  163. preprocfile^.Add(orgpattern);
  164. end;
  165. _REALNUMBER,
  166. _INTCONST :
  167. preprocfile^.Add(pattern);
  168. _CSTRING :
  169. begin
  170. i:=0;
  171. while (i<length(pattern)) do
  172. begin
  173. inc(i);
  174. if pattern[i]='''' then
  175. begin
  176. insert('''',pattern,i);
  177. inc(i);
  178. end;
  179. end;
  180. preprocfile^.Add(''''+pattern+'''');
  181. end;
  182. _CCHAR :
  183. begin
  184. case pattern[1] of
  185. #39 :
  186. pattern:='''''''';
  187. #0..#31,
  188. #128..#255 :
  189. begin
  190. str(ord(pattern[1]),pattern);
  191. pattern:='#'+pattern;
  192. end;
  193. else
  194. pattern:=''''+pattern[1]+'''';
  195. end;
  196. preprocfile^.Add(pattern);
  197. end;
  198. _EOF :
  199. break;
  200. else
  201. preprocfile^.Add(tokeninfo^[token].str)
  202. end;
  203. until false;
  204. { free scanner }
  205. dispose(current_scanner,done);
  206. current_scanner:=nil;
  207. { close }
  208. dispose(preprocfile,done);
  209. end;
  210. {$endif PREPROCWRITE}
  211. {*****************************************************************************
  212. Compile a source file
  213. *****************************************************************************}
  214. procedure compile(const filename:string);
  215. type
  216. polddata=^tolddata;
  217. tolddata=record
  218. { scanner }
  219. oldidtoken,
  220. oldtoken : ttoken;
  221. oldtokenpos : tfileposinfo;
  222. oldc : char;
  223. oldpattern,
  224. oldorgpattern : string;
  225. old_block_type : tblock_type;
  226. { symtable }
  227. oldsymtablestack,
  228. oldmacrosymtablestack : TSymtablestack;
  229. oldaktprocsym : tprocsym;
  230. { cg }
  231. oldparse_only : boolean;
  232. { akt.. things }
  233. oldcurrent_filepos : tfileposinfo;
  234. old_current_module : tmodule;
  235. oldcurrent_procinfo : tprocinfo;
  236. old_settings : tsettings;
  237. oldsourcecodepage : tcodepagestring;
  238. end;
  239. var
  240. olddata : polddata;
  241. hp,hp2 : tmodule;
  242. begin
  243. { parsing a procedure or declaration should be finished }
  244. if assigned(current_procinfo) then
  245. internalerror(200811121);
  246. if assigned(current_objectdef) then
  247. internalerror(200811122);
  248. inc(compile_level);
  249. parser_current_file:=filename;
  250. { Uses heap memory instead of placing everything on the
  251. stack. This is needed because compile() can be called
  252. recursively }
  253. new(olddata);
  254. with olddata^ do
  255. begin
  256. old_current_module:=current_module;
  257. { save symtable state }
  258. oldsymtablestack:=symtablestack;
  259. oldmacrosymtablestack:=macrosymtablestack;
  260. oldcurrent_procinfo:=current_procinfo;
  261. { save scanner state }
  262. oldc:=c;
  263. oldpattern:=pattern;
  264. oldorgpattern:=orgpattern;
  265. oldtoken:=token;
  266. oldidtoken:=idtoken;
  267. old_block_type:=block_type;
  268. oldtokenpos:=current_tokenpos;
  269. { save cg }
  270. oldparse_only:=parse_only;
  271. { save akt... state }
  272. { handle the postponed case first }
  273. flushpendingswitchesstate;
  274. oldcurrent_filepos:=current_filepos;
  275. old_settings:=current_settings;
  276. end;
  277. { reset parser, a previous fatal error could have left these variables in an unreliable state, this is
  278. important for the IDE }
  279. afterassignment:=false;
  280. in_args:=false;
  281. named_args_allowed:=false;
  282. got_addrn:=false;
  283. getprocvardef:=nil;
  284. { show info }
  285. Message1(parser_i_compiling,filename);
  286. { reset symtable }
  287. symtablestack:=TSymtablestack.create;
  288. macrosymtablestack:=TSymtablestack.create;
  289. systemunit:=nil;
  290. current_settings.defproccall:=init_settings.defproccall;
  291. current_exceptblock:=0;
  292. exceptblockcounter:=0;
  293. current_settings.maxfpuregisters:=-1;
  294. { reset the unit or create a new program }
  295. { a unit compiled at command line must be inside the loaded_unit list }
  296. if (compile_level=1) then
  297. begin
  298. if assigned(current_module) then
  299. internalerror(200501158);
  300. set_current_module(tppumodule.create(nil,filename,'',false));
  301. addloadedunit(current_module);
  302. main_module:=current_module;
  303. current_module.state:=ms_compile;
  304. end;
  305. if not(assigned(current_module) and
  306. (current_module.state in [ms_compile,ms_second_compile])) then
  307. internalerror(200212281);
  308. { Load current state from the init values }
  309. current_settings:=init_settings;
  310. { load current asmdata from current_module }
  311. current_asmdata:=TAsmData(current_module.asmdata);
  312. { startup scanner and load the first file }
  313. current_scanner:=tscannerfile.Create(filename);
  314. current_scanner.firstfile;
  315. current_module.scanner:=current_scanner;
  316. { init macros before anything in the file is parsed.}
  317. current_module.localmacrosymtable:= tmacrosymtable.create(false);
  318. macrosymtablestack.push(initialmacrosymtable);
  319. macrosymtablestack.push(current_module.localmacrosymtable);
  320. { read the first token }
  321. current_scanner.readtoken(false);
  322. { If the compile level > 1 we get a nice "unit expected" error
  323. message if we are trying to use a program as unit.}
  324. try
  325. try
  326. if (token=_UNIT) or (compile_level>1) then
  327. begin
  328. current_module.is_unit:=true;
  329. proc_unit;
  330. end
  331. else if (token=_ID) and (idtoken=_PACKAGE) then
  332. begin
  333. current_module.IsPackage:=true;
  334. proc_package;
  335. end
  336. else
  337. proc_program(token=_LIBRARY);
  338. except
  339. on ECompilerAbort do
  340. raise;
  341. on Exception do
  342. begin
  343. { Increase errorcounter to prevent some
  344. checks during cleanup }
  345. inc(status.errorcount);
  346. raise;
  347. end;
  348. end;
  349. finally
  350. if assigned(current_module) then
  351. begin
  352. { module is now compiled }
  353. tppumodule(current_module).state:=ms_compiled;
  354. { free ppu }
  355. if assigned(tppumodule(current_module).ppufile) then
  356. begin
  357. tppumodule(current_module).ppufile.free;
  358. tppumodule(current_module).ppufile:=nil;
  359. end;
  360. { free asmdata }
  361. if assigned(current_module.asmdata) then
  362. begin
  363. current_module.asmdata.free;
  364. current_module.asmdata:=nil;
  365. end;
  366. { free scanner }
  367. if assigned(current_module.scanner) then
  368. begin
  369. if current_scanner=tscannerfile(current_module.scanner) then
  370. current_scanner:=nil;
  371. tscannerfile(current_module.scanner).free;
  372. current_module.scanner:=nil;
  373. end;
  374. { free symtable stack }
  375. if assigned(symtablestack) then
  376. begin
  377. symtablestack.free;
  378. symtablestack:=nil;
  379. end;
  380. if assigned(macrosymtablestack) then
  381. begin
  382. macrosymtablestack.free;
  383. macrosymtablestack:=nil;
  384. end;
  385. end;
  386. if (compile_level=1) and
  387. (status.errorcount=0) then
  388. { Write Browser Collections }
  389. do_extractsymbolinfo;
  390. with olddata^ do
  391. begin
  392. { restore scanner }
  393. c:=oldc;
  394. pattern:=oldpattern;
  395. orgpattern:=oldorgpattern;
  396. token:=oldtoken;
  397. idtoken:=oldidtoken;
  398. current_tokenpos:=oldtokenpos;
  399. block_type:=old_block_type;
  400. { restore cg }
  401. parse_only:=oldparse_only;
  402. { restore symtable state }
  403. symtablestack:=oldsymtablestack;
  404. macrosymtablestack:=oldmacrosymtablestack;
  405. current_procinfo:=oldcurrent_procinfo;
  406. current_filepos:=oldcurrent_filepos;
  407. current_settings:=old_settings;
  408. current_exceptblock:=0;
  409. exceptblockcounter:=0;
  410. end;
  411. { Shut down things when the last file is compiled succesfull }
  412. if (compile_level=1) and
  413. (status.errorcount=0) then
  414. begin
  415. parser_current_file:='';
  416. { Close script }
  417. if (not AsmRes.Empty) then
  418. begin
  419. Message1(exec_i_closing_script,AsmRes.Fn);
  420. AsmRes.WriteToDisk;
  421. end;
  422. end;
  423. { free now what we did not free earlier in
  424. proc_program PM }
  425. if (compile_level=1) and needsymbolinfo then
  426. begin
  427. hp:=tmodule(loaded_units.first);
  428. while assigned(hp) do
  429. begin
  430. hp2:=tmodule(hp.next);
  431. if (hp<>current_module) then
  432. begin
  433. loaded_units.remove(hp);
  434. hp.free;
  435. end;
  436. hp:=hp2;
  437. end;
  438. { free also unneeded units we didn't free before }
  439. unloaded_units.Clear;
  440. end;
  441. dec(compile_level);
  442. set_current_module(olddata^.old_current_module);
  443. dispose(olddata);
  444. end;
  445. end;
  446. end.