parser.pas 16 KB

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