parser.pas 16 KB

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