2
0

parser.pas 16 KB

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