parser.pas 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  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. uses fmodule;
  21. {$ifdef PREPROCWRITE}
  22. procedure preprocess(const filename:string);
  23. {$endif PREPROCWRITE}
  24. procedure compile(const filename:string);
  25. procedure compile_module(module : tmodule);
  26. procedure initparser;
  27. procedure doneparser;
  28. implementation
  29. uses
  30. {$IFNDEF USE_FAKE_SYSUTILS}
  31. sysutils,
  32. {$ELSE}
  33. fksysutl,
  34. {$ENDIF}
  35. cclasses,
  36. globtype,tokens,systems,globals,verbose,switches,globstat,
  37. symbase,symtable,symdef,
  38. finput,fppu,
  39. aasmdata,
  40. cscript,gendef,
  41. comphook,
  42. scanner,scandir,
  43. pbase,psystem,pmodules,psub,ncgrtti,
  44. cpuinfo,procinfo;
  45. procedure initparser;
  46. begin
  47. { Current compiled module/proc }
  48. set_current_module(nil);
  49. current_module:=nil;
  50. current_asmdata:=nil;
  51. current_procinfo:=nil;
  52. current_structdef:=nil;
  53. current_genericdef:=nil;
  54. current_specializedef:=nil;
  55. loaded_units:=TLinkedList.Create;
  56. usedunits:=TLinkedList.Create;
  57. unloaded_units:=TLinkedList.Create;
  58. { global switches }
  59. current_settings.globalswitches:=init_settings.globalswitches;
  60. current_settings.sourcecodepage:=init_settings.sourcecodepage;
  61. { initialize scanner }
  62. InitScanner;
  63. InitScannerDirectives;
  64. { scanner }
  65. c:=#0;
  66. pattern:='';
  67. orgpattern:='';
  68. cstringpattern:='';
  69. current_scanner:=nil;
  70. switchesstatestackpos:=0;
  71. { register all nodes and tais }
  72. registernodes;
  73. registertais;
  74. { memory sizes }
  75. if stacksize=0 then
  76. stacksize:=target_info.stacksize;
  77. { RTTI writer }
  78. RTTIWriter:=TRTTIWriter.Create;
  79. { open assembler response }
  80. if cs_link_on_target in current_settings.globalswitches then
  81. GenerateAsmRes(outputexedir+ChangeFileExt(inputfilename,'_ppas'))
  82. else
  83. GenerateAsmRes(outputexedir+'ppas');
  84. { open deffile }
  85. DefFile:=TDefFile.Create(outputexedir+ChangeFileExt(inputfilename,target_info.defext));
  86. { list of generated .o files, so the linker can remove them }
  87. SmartLinkOFiles:=TCmdStrList.Create;
  88. { codegen }
  89. if paraprintnodetree<>0 then
  90. printnode_reset;
  91. { target specific stuff }
  92. case target_info.system of
  93. system_arm_aros,
  94. system_arm_palmos,
  95. system_m68k_amiga,
  96. system_m68k_atari,
  97. system_m68k_palmos,
  98. system_i386_aros,
  99. system_powerpc_amiga,
  100. system_powerpc_morphos,
  101. system_x86_64_aros:
  102. include(supported_calling_conventions,pocall_syscall);
  103. system_m68k_human68k:
  104. begin
  105. include(supported_calling_conventions,pocall_syscall);
  106. if heapsize=0 then
  107. heapsize:=65536;
  108. end;
  109. {$ifdef i8086}
  110. system_i8086_embedded:
  111. begin
  112. if stacksize=0 then
  113. begin
  114. if init_settings.x86memorymodel in x86_far_data_models then
  115. stacksize:=16384
  116. else
  117. stacksize:=2048;
  118. end;
  119. end;
  120. system_i8086_msdos:
  121. begin
  122. if stacksize=0 then
  123. begin
  124. if init_settings.x86memorymodel in x86_far_data_models then
  125. stacksize:=16384
  126. else
  127. stacksize:=4096;
  128. end;
  129. if maxheapsize=0 then
  130. begin
  131. if init_settings.x86memorymodel in x86_far_data_models then
  132. maxheapsize:=655360
  133. else
  134. maxheapsize:=65520;
  135. end;
  136. end;
  137. system_i8086_win16:
  138. begin
  139. if stacksize=0 then
  140. begin
  141. if init_settings.x86memorymodel in x86_far_data_models then
  142. stacksize:=8192
  143. else
  144. stacksize:=5120;
  145. end;
  146. if heapsize=0 then
  147. begin
  148. if init_settings.x86memorymodel in x86_far_data_models then
  149. heapsize:=8192
  150. else
  151. heapsize:=4096;
  152. end;
  153. end;
  154. {$endif i8086}
  155. else
  156. ;
  157. end;
  158. end;
  159. procedure doneparser;
  160. begin
  161. { Reset current compiling info, so destroy routines can't
  162. reference the data that might already be destroyed }
  163. set_current_module(nil);
  164. current_module:=nil;
  165. current_procinfo:=nil;
  166. current_asmdata:=nil;
  167. current_structdef:=nil;
  168. current_genericdef:=nil;
  169. current_specializedef:=nil;
  170. { unload units }
  171. if assigned(loaded_units) then
  172. begin
  173. loaded_units.free;
  174. loaded_units:=nil;
  175. end;
  176. if assigned(usedunits) then
  177. begin
  178. usedunits.free;
  179. usedunits:=nil;
  180. end;
  181. if assigned(unloaded_units) then
  182. begin
  183. unloaded_units.free;
  184. unloaded_units:=nil;
  185. end;
  186. { if there was an error in the scanner, the scanner is
  187. still assinged }
  188. if assigned(current_scanner) then
  189. begin
  190. current_scanner.free;
  191. current_scanner:=nil;
  192. end;
  193. { close scanner }
  194. DoneScanner;
  195. RTTIWriter.free;
  196. { close ppas,deffile }
  197. asmres.free;
  198. deffile.free;
  199. { free list of .o files }
  200. SmartLinkOFiles.Free;
  201. end;
  202. {$ifdef PREPROCWRITE}
  203. procedure preprocess(const filename:string);
  204. var
  205. i : longint;
  206. begin
  207. preprocfile:=tpreprocfile.create('pre_'+filename);
  208. { initialize a module }
  209. set_current_module(tppumodule.create(nil,'',filename,false));
  210. macrosymtablestack:=TSymtablestack.create;
  211. current_scanner:=tscannerfile.Create(filename);
  212. current_scanner.firstfile;
  213. current_module.scanner:=current_scanner;
  214. { init macros before anything in the file is parsed.}
  215. current_module.localmacrosymtable:= tmacrosymtable.create(false);
  216. macrosymtablestack.push(initialmacrosymtable);
  217. macrosymtablestack.push(current_module.localmacrosymtable);
  218. { read the first token }
  219. // current_scanner.readtoken(false);
  220. main_module:=current_module;
  221. repeat
  222. current_scanner.readtoken(true);
  223. preprocfile.AddSpace;
  224. case token of
  225. _ID :
  226. begin
  227. preprocfile.Add(orgpattern);
  228. end;
  229. _REALNUMBER,
  230. _INTCONST :
  231. preprocfile.Add(pattern);
  232. _CSTRING :
  233. begin
  234. i:=0;
  235. while (i<length(cstringpattern)) do
  236. begin
  237. inc(i);
  238. if cstringpattern[i]='''' then
  239. begin
  240. insert('''',cstringpattern,i);
  241. inc(i);
  242. end;
  243. end;
  244. preprocfile.Add(''''+cstringpattern+'''');
  245. end;
  246. _CCHAR :
  247. begin
  248. case pattern[1] of
  249. #39 :
  250. pattern:='''''''';
  251. #0..#31,
  252. #128..#255 :
  253. begin
  254. str(ord(pattern[1]),pattern);
  255. pattern:='#'+pattern;
  256. end;
  257. else
  258. pattern:=''''+pattern[1]+'''';
  259. end;
  260. preprocfile.Add(pattern);
  261. end;
  262. _EOF :
  263. break;
  264. else
  265. preprocfile.Add(tokeninfo^[token].str)
  266. end;
  267. until false;
  268. { free scanner }
  269. current_scanner.destroy;
  270. current_scanner:=nil;
  271. { close }
  272. preprocfile.destroy;
  273. end;
  274. {$endif PREPROCWRITE}
  275. {*****************************************************************************
  276. Compile a source file
  277. *****************************************************************************}
  278. procedure compile(const filename:string);
  279. var
  280. m : TModule;
  281. begin
  282. m:=tppumodule.create(nil,'',filename,false);
  283. // m.is_initial:=initial;
  284. m.state:=ms_compile;
  285. compile_module(m);
  286. end;
  287. procedure compile_module(module : tmodule);
  288. var
  289. olddata : pglobalstate;
  290. hp,hp2 : tmodule;
  291. finished : boolean;
  292. begin
  293. { parsing a procedure or declaration should be finished }
  294. if assigned(current_procinfo) then
  295. internalerror(200811121);
  296. if assigned(current_structdef) then
  297. internalerror(200811122);
  298. inc(compile_level);
  299. parser_current_file:=module.mainsource;
  300. { Uses heap memory instead of placing everything on the
  301. stack. This is needed because compile() can be called
  302. recursively }
  303. new(olddata);
  304. { handle the postponed case first }
  305. flushpendingswitchesstate;
  306. save_global_state(olddata^,false);
  307. { reset parser, a previous fatal error could have left these variables in an unreliable state, this is
  308. important for the IDE }
  309. afterassignment:=false;
  310. in_args:=false;
  311. named_args_allowed:=false;
  312. got_addrn:=false;
  313. getprocvardef:=nil;
  314. getfuncrefdef:=nil;
  315. { show info }
  316. Message1(parser_i_compiling,module.mainsource);
  317. { reset symtable }
  318. symtablestack:=tdefawaresymtablestack.create;
  319. macrosymtablestack:=TSymtablestack.create;
  320. systemunit:=nil;
  321. current_settings.defproccall:=init_settings.defproccall;
  322. current_exceptblock:=0;
  323. exceptblockcounter:=0;
  324. current_settings.maxfpuregisters:=-1;
  325. current_settings.pmessage:=nil;
  326. { Load current state from the init values }
  327. current_settings:=init_settings;
  328. { reset the unit or create a new program }
  329. { a unit compiled at command line must be inside the loaded_unit list }
  330. if (compile_level=1) then
  331. begin
  332. if assigned(current_module) then
  333. internalerror(200501158);
  334. set_current_module(module);
  335. addloadedunit(current_module);
  336. main_module:=current_module;
  337. current_module.state:=ms_compile;
  338. end;
  339. if not(assigned(current_module) and
  340. (current_module.state in [ms_compile,ms_second_compile])) then
  341. internalerror(200212281);
  342. { load current asmdata from current_module }
  343. current_asmdata:=TAsmData(current_module.asmdata);
  344. { startup scanner and load the first file }
  345. current_scanner:=tscannerfile.Create(module.mainsource);
  346. current_scanner.firstfile;
  347. current_module.scanner:=current_scanner;
  348. { init macros before anything in the file is parsed.}
  349. current_module.localmacrosymtable:= tmacrosymtable.create(false);
  350. macrosymtablestack.push(initialmacrosymtable);
  351. macrosymtablestack.push(current_module.localmacrosymtable);
  352. { read the first token }
  353. current_scanner.readtoken(false);
  354. { this is set to false if a unit needs to wait for other units }
  355. finished:=true;
  356. { If the compile level > 1 we get a nice "unit expected" error
  357. message if we are trying to use a program as unit.}
  358. try
  359. try
  360. if (token=_UNIT) or (compile_level>1) then
  361. begin
  362. current_module.is_unit:=true;
  363. finished:=proc_unit(current_module);
  364. end
  365. else if (token=_ID) and (idtoken=_PACKAGE) then
  366. begin
  367. current_module.IsPackage:=true;
  368. proc_package(current_module);
  369. end
  370. else
  371. proc_program(current_module,token=_LIBRARY);
  372. except
  373. on ECompilerAbort do
  374. raise;
  375. on Exception do
  376. begin
  377. { Generate exception_raised message,
  378. but avoid multiple messages by
  379. guarding with exception_raised global variable }
  380. if not exception_raised then
  381. begin
  382. exception_raised:=true;
  383. Message(general_e_exception_raised);
  384. end;
  385. raise;
  386. end;
  387. end;
  388. { the program or the unit at the command line should not need to wait
  389. for other units }
  390. if (compile_level=1) and not finished then
  391. internalerror(2012091901);
  392. finally
  393. if assigned(current_module) then
  394. begin
  395. if finished then
  396. current_module.end_of_parsing
  397. else
  398. begin
  399. { these are saved in the unit's state and thus can be set to
  400. Nil again as would be done by tmodule.end_of_parsing }
  401. macrosymtablestack:=nil;
  402. symtablestack:=nil;
  403. if current_scanner=current_module.scanner then
  404. current_scanner:=nil;
  405. end;
  406. end;
  407. if (compile_level=1) and
  408. (status.errorcount=0) then
  409. { Write Browser Collections }
  410. do_extractsymbolinfo;
  411. restore_global_state(olddata^,false);
  412. { Restore all locally modified warning messages }
  413. RestoreLocalVerbosity(current_settings.pmessage);
  414. current_exceptblock:=0;
  415. exceptblockcounter:=0;
  416. { Shut down things when the last file is compiled succesfull }
  417. if (compile_level=1) and
  418. (status.errorcount=0) then
  419. begin
  420. parser_current_file:='';
  421. { Close script }
  422. if (not AsmRes.Empty) then
  423. begin
  424. Message1(exec_i_closing_script,AsmRes.Fn);
  425. AsmRes.WriteToDisk;
  426. end;
  427. end;
  428. { free now what we did not free earlier in
  429. proc_program PM }
  430. if (compile_level=1) and needsymbolinfo then
  431. begin
  432. hp:=tmodule(loaded_units.first);
  433. while assigned(hp) do
  434. begin
  435. hp2:=tmodule(hp.next);
  436. if (hp<>current_module) then
  437. begin
  438. loaded_units.remove(hp);
  439. hp.free;
  440. end;
  441. hp:=hp2;
  442. end;
  443. { free also unneeded units we didn't free before }
  444. unloaded_units.Clear;
  445. end;
  446. dec(compile_level);
  447. { If used units are compiled current_module is already the same as
  448. the stored module. Now if the unit is not finished its scanner is
  449. not yet freed and thus set_current_module would reopen the scanned
  450. file which will result in pointing to the wrong position in the
  451. file. In the normal case current_scanner and current_module.scanner
  452. would be Nil, thus nothing bad would happen }
  453. if olddata^.old_current_module<>current_module then
  454. set_current_module(olddata^.old_current_module);
  455. FreeLocalVerbosity(current_settings.pmessage);
  456. dispose(olddata);
  457. end;
  458. end;
  459. end.