parser.pas 16 KB

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