parser.pas 16 KB

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