parser.pas 15 KB

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