parser.pas 16 KB

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