parser.pas 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  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. cclasses,
  34. globtype,tokens,systems,globals,verbose,switches,globstat,
  35. symbase,symtable,symdef,
  36. finput,fmodule,fppu,
  37. aasmdata,
  38. cscript,gendef,
  39. comphook,
  40. scanner,scandir,
  41. pbase,psystem,pmodules,psub,ncgrtti,
  42. cpuinfo,procinfo;
  43. procedure initparser;
  44. begin
  45. { Current compiled module/proc }
  46. set_current_module(nil);
  47. current_module:=nil;
  48. current_asmdata:=nil;
  49. current_procinfo:=nil;
  50. current_structdef:=nil;
  51. current_genericdef:=nil;
  52. current_specializedef:=nil;
  53. loaded_units:=TLinkedList.Create;
  54. usedunits:=TLinkedList.Create;
  55. unloaded_units:=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. cstringpattern:='';
  67. current_scanner:=nil;
  68. switchesstatestackpos:=0;
  69. { register all nodes and tais }
  70. registernodes;
  71. registertais;
  72. { memory sizes }
  73. if stacksize=0 then
  74. stacksize:=target_info.stacksize;
  75. { RTTI writer }
  76. RTTIWriter:=TRTTIWriter.Create;
  77. { open assembler response }
  78. if cs_link_on_target in current_settings.globalswitches then
  79. GenerateAsmRes(outputexedir+ChangeFileExt(inputfilename,'_ppas'))
  80. else
  81. GenerateAsmRes(outputexedir+'ppas');
  82. { open deffile }
  83. DefFile:=TDefFile.Create(outputexedir+ChangeFileExt(inputfilename,target_info.defext));
  84. { list of generated .o files, so the linker can remove them }
  85. SmartLinkOFiles:=TCmdStrList.Create;
  86. { codegen }
  87. if paraprintnodetree<>0 then
  88. printnode_reset;
  89. { target specific stuff }
  90. case target_info.system of
  91. system_arm_aros,
  92. system_arm_palmos,
  93. system_m68k_amiga,
  94. system_m68k_atari,
  95. system_m68k_palmos,
  96. system_i386_aros,
  97. system_powerpc_amiga,
  98. system_powerpc_morphos,
  99. system_x86_64_aros:
  100. include(supported_calling_conventions,pocall_syscall);
  101. {$ifdef i8086}
  102. system_i8086_msdos:
  103. begin
  104. if stacksize=0 then
  105. begin
  106. if init_settings.x86memorymodel in x86_far_data_models then
  107. stacksize:=16384
  108. else
  109. stacksize:=4096;
  110. end;
  111. if maxheapsize=0 then
  112. begin
  113. if init_settings.x86memorymodel in x86_far_data_models then
  114. maxheapsize:=655360
  115. else
  116. maxheapsize:=65520;
  117. end;
  118. end;
  119. system_i8086_win16:
  120. begin
  121. if stacksize=0 then
  122. begin
  123. if init_settings.x86memorymodel in x86_far_data_models then
  124. stacksize:=8192
  125. else
  126. stacksize:=5120;
  127. end;
  128. if heapsize=0 then
  129. begin
  130. if init_settings.x86memorymodel in x86_far_data_models then
  131. heapsize:=8192
  132. else
  133. heapsize:=4096;
  134. end;
  135. end;
  136. {$endif i8086}
  137. end;
  138. end;
  139. procedure doneparser;
  140. begin
  141. { Reset current compiling info, so destroy routines can't
  142. reference the data that might already be destroyed }
  143. set_current_module(nil);
  144. current_module:=nil;
  145. current_procinfo:=nil;
  146. current_asmdata:=nil;
  147. current_structdef:=nil;
  148. current_genericdef:=nil;
  149. current_specializedef:=nil;
  150. { unload units }
  151. if assigned(loaded_units) then
  152. begin
  153. loaded_units.free;
  154. loaded_units:=nil;
  155. end;
  156. if assigned(usedunits) then
  157. begin
  158. usedunits.free;
  159. usedunits:=nil;
  160. end;
  161. if assigned(unloaded_units) then
  162. begin
  163. unloaded_units.free;
  164. unloaded_units:=nil;
  165. end;
  166. { if there was an error in the scanner, the scanner is
  167. still assinged }
  168. if assigned(current_scanner) then
  169. begin
  170. current_scanner.free;
  171. current_scanner:=nil;
  172. end;
  173. { close scanner }
  174. DoneScanner;
  175. RTTIWriter.free;
  176. { close ppas,deffile }
  177. asmres.free;
  178. deffile.free;
  179. { free list of .o files }
  180. SmartLinkOFiles.Free;
  181. end;
  182. {$ifdef PREPROCWRITE}
  183. procedure preprocess(const filename:string);
  184. var
  185. i : longint;
  186. begin
  187. new(preprocfile,init('pre'));
  188. { initialize a module }
  189. set_current_module(new(pmodule,init(filename,false)));
  190. macrosymtablestack:= initialmacrosymtable;
  191. current_module.localmacrosymtable:= tmacrosymtable.create(false);
  192. current_module.localmacrosymtable.next:= initialmacrosymtable;
  193. macrosymtablestack:= current_module.localmacrosymtable;
  194. main_module:=current_module;
  195. { startup scanner, and save in current_module }
  196. current_scanner:=new(pscannerfile,Init(filename));
  197. current_module.scanner:=current_scanner;
  198. { loop until EOF is found }
  199. repeat
  200. current_scanner^.readtoken(true);
  201. preprocfile^.AddSpace;
  202. case token of
  203. _ID :
  204. begin
  205. preprocfile^.Add(orgpattern);
  206. end;
  207. _REALNUMBER,
  208. _INTCONST :
  209. preprocfile^.Add(pattern);
  210. _CSTRING :
  211. begin
  212. i:=0;
  213. while (i<length(cstringpattern)) do
  214. begin
  215. inc(i);
  216. if cstringpattern[i]='''' then
  217. begin
  218. insert('''',cstringpattern,i);
  219. inc(i);
  220. end;
  221. end;
  222. preprocfile^.Add(''''+cstringpattern+'''');
  223. end;
  224. _CCHAR :
  225. begin
  226. case pattern[1] of
  227. #39 :
  228. pattern:='''''''';
  229. #0..#31,
  230. #128..#255 :
  231. begin
  232. str(ord(pattern[1]),pattern);
  233. pattern:='#'+pattern;
  234. end;
  235. else
  236. pattern:=''''+pattern[1]+'''';
  237. end;
  238. preprocfile^.Add(pattern);
  239. end;
  240. _EOF :
  241. break;
  242. else
  243. preprocfile^.Add(tokeninfo^[token].str)
  244. end;
  245. until false;
  246. { free scanner }
  247. dispose(current_scanner,done);
  248. current_scanner:=nil;
  249. { close }
  250. dispose(preprocfile,done);
  251. end;
  252. {$endif PREPROCWRITE}
  253. {*****************************************************************************
  254. Compile a source file
  255. *****************************************************************************}
  256. procedure compile(const filename:string);
  257. var
  258. olddata : pglobalstate;
  259. hp,hp2 : tmodule;
  260. finished : boolean;
  261. begin
  262. { parsing a procedure or declaration should be finished }
  263. if assigned(current_procinfo) then
  264. internalerror(200811121);
  265. if assigned(current_structdef) then
  266. internalerror(200811122);
  267. inc(compile_level);
  268. parser_current_file:=filename;
  269. { Uses heap memory instead of placing everything on the
  270. stack. This is needed because compile() can be called
  271. recursively }
  272. new(olddata);
  273. { handle the postponed case first }
  274. flushpendingswitchesstate;
  275. save_global_state(olddata^,false);
  276. { reset parser, a previous fatal error could have left these variables in an unreliable state, this is
  277. important for the IDE }
  278. afterassignment:=false;
  279. in_args:=false;
  280. named_args_allowed:=false;
  281. got_addrn:=false;
  282. getprocvardef:=nil;
  283. { show info }
  284. Message1(parser_i_compiling,filename);
  285. { reset symtable }
  286. symtablestack:=tdefawaresymtablestack.create;
  287. macrosymtablestack:=TSymtablestack.create;
  288. systemunit:=nil;
  289. current_settings.defproccall:=init_settings.defproccall;
  290. current_exceptblock:=0;
  291. exceptblockcounter:=0;
  292. current_settings.maxfpuregisters:=-1;
  293. current_settings.pmessage:=nil;
  294. { reset the unit or create a new program }
  295. { a unit compiled at command line must be inside the loaded_unit list }
  296. if (compile_level=1) then
  297. begin
  298. if assigned(current_module) then
  299. internalerror(200501158);
  300. set_current_module(tppumodule.create(nil,'',filename,false));
  301. addloadedunit(current_module);
  302. main_module:=current_module;
  303. current_module.state:=ms_compile;
  304. end;
  305. if not(assigned(current_module) and
  306. (current_module.state in [ms_compile,ms_second_compile])) then
  307. internalerror(200212281);
  308. { Load current state from the init values }
  309. current_settings:=init_settings;
  310. { load current asmdata from current_module }
  311. current_asmdata:=TAsmData(current_module.asmdata);
  312. { startup scanner and load the first file }
  313. current_scanner:=tscannerfile.Create(filename);
  314. current_scanner.firstfile;
  315. current_module.scanner:=current_scanner;
  316. { init macros before anything in the file is parsed.}
  317. current_module.localmacrosymtable:= tmacrosymtable.create(false);
  318. macrosymtablestack.push(initialmacrosymtable);
  319. macrosymtablestack.push(current_module.localmacrosymtable);
  320. { read the first token }
  321. current_scanner.readtoken(false);
  322. { this is set to false if a unit needs to wait for other units }
  323. finished:=true;
  324. { If the compile level > 1 we get a nice "unit expected" error
  325. message if we are trying to use a program as unit.}
  326. try
  327. try
  328. if (token=_UNIT) or (compile_level>1) then
  329. begin
  330. current_module.is_unit:=true;
  331. finished:=proc_unit;
  332. end
  333. else if (token=_ID) and (idtoken=_PACKAGE) then
  334. begin
  335. current_module.IsPackage:=true;
  336. proc_package;
  337. end
  338. else
  339. proc_program(token=_LIBRARY);
  340. except
  341. on ECompilerAbort do
  342. raise;
  343. on Exception do
  344. begin
  345. { Generate exception_raised message,
  346. but avoid multiple messages by
  347. guarding with exception_raised global variable }
  348. if not exception_raised then
  349. begin
  350. exception_raised:=true;
  351. Message(general_e_exception_raised);
  352. end;
  353. raise;
  354. end;
  355. end;
  356. { the program or the unit at the command line should not need to wait
  357. for other units }
  358. if (compile_level=1) and not finished then
  359. internalerror(2012091901);
  360. finally
  361. if assigned(current_module) then
  362. begin
  363. if finished then
  364. current_module.end_of_parsing
  365. else
  366. begin
  367. { these are saved in the unit's state and thus can be set to
  368. Nil again as would be done by tmodule.end_of_parsing }
  369. macrosymtablestack:=nil;
  370. symtablestack:=nil;
  371. if current_scanner=current_module.scanner then
  372. current_scanner:=nil;
  373. end;
  374. end;
  375. if (compile_level=1) and
  376. (status.errorcount=0) then
  377. { Write Browser Collections }
  378. do_extractsymbolinfo;
  379. restore_global_state(olddata^,false);
  380. { Restore all locally modified warning messages }
  381. RestoreLocalVerbosity(current_settings.pmessage);
  382. current_exceptblock:=0;
  383. exceptblockcounter:=0;
  384. { Shut down things when the last file is compiled succesfull }
  385. if (compile_level=1) and
  386. (status.errorcount=0) then
  387. begin
  388. parser_current_file:='';
  389. { Close script }
  390. if (not AsmRes.Empty) then
  391. begin
  392. Message1(exec_i_closing_script,AsmRes.Fn);
  393. AsmRes.WriteToDisk;
  394. end;
  395. end;
  396. { free now what we did not free earlier in
  397. proc_program PM }
  398. if (compile_level=1) and needsymbolinfo then
  399. begin
  400. hp:=tmodule(loaded_units.first);
  401. while assigned(hp) do
  402. begin
  403. hp2:=tmodule(hp.next);
  404. if (hp<>current_module) then
  405. begin
  406. loaded_units.remove(hp);
  407. hp.free;
  408. end;
  409. hp:=hp2;
  410. end;
  411. { free also unneeded units we didn't free before }
  412. unloaded_units.Clear;
  413. end;
  414. dec(compile_level);
  415. { If used units are compiled current_module is already the same as
  416. the stored module. Now if the unit is not finished its scanner is
  417. not yet freed and thus set_current_module would reopen the scanned
  418. file which will result in pointing to the wrong position in the
  419. file. In the normal case current_scanner and current_module.scanner
  420. would be Nil, thus nothing bad would happen }
  421. if olddata^.old_current_module<>current_module then
  422. set_current_module(olddata^.old_current_module);
  423. FreeLocalVerbosity(current_settings.pmessage);
  424. dispose(olddata);
  425. end;
  426. end;
  427. end.