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