parser.pas 16 KB

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