parser.pas 15 KB

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