parser.pas 16 KB

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