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