2
0

parser.pas 16 KB

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