2
0

parser.pas 16 KB

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