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