parser.pas 17 KB

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