parser.pas 16 KB

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