2
0

parser.pas 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  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,
  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. { we didn't parse a object or class declaration }
  47. { and no function header }
  48. testcurobject:=0;
  49. { Current compiled module/proc }
  50. set_current_module(nil);
  51. current_module:=nil;
  52. current_asmdata:=nil;
  53. current_procinfo:=nil;
  54. current_objectdef:=nil;
  55. loaded_units:=TLinkedList.Create;
  56. usedunits:=TLinkedList.Create;
  57. unloaded_units:=TLinkedList.Create;
  58. { global switches }
  59. current_settings.globalswitches:=init_settings.globalswitches;
  60. current_settings.sourcecodepage:=init_settings.sourcecodepage;
  61. { initialize scanner }
  62. InitScanner;
  63. InitScannerDirectives;
  64. { scanner }
  65. c:=#0;
  66. pattern:='';
  67. orgpattern:='';
  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. end;
  99. end;
  100. procedure doneparser;
  101. begin
  102. { Reset current compiling info, so destroy routines can't
  103. reference the data that might already be destroyed }
  104. set_current_module(nil);
  105. current_module:=nil;
  106. current_procinfo:=nil;
  107. current_asmdata:=nil;
  108. current_objectdef:=nil;
  109. { unload units }
  110. if assigned(loaded_units) then
  111. begin
  112. loaded_units.free;
  113. loaded_units:=nil;
  114. end;
  115. if assigned(usedunits) then
  116. begin
  117. usedunits.free;
  118. usedunits:=nil;
  119. end;
  120. if assigned(unloaded_units) then
  121. begin
  122. unloaded_units.free;
  123. unloaded_units:=nil;
  124. end;
  125. { if there was an error in the scanner, the scanner is
  126. still assinged }
  127. if assigned(current_scanner) then
  128. begin
  129. current_scanner.free;
  130. current_scanner:=nil;
  131. end;
  132. { close scanner }
  133. DoneScanner;
  134. RTTIWriter.free;
  135. { close ppas,deffile }
  136. asmres.free;
  137. deffile.free;
  138. { free list of .o files }
  139. SmartLinkOFiles.Free;
  140. end;
  141. {$ifdef PREPROCWRITE}
  142. procedure preprocess(const filename:string);
  143. var
  144. i : longint;
  145. begin
  146. new(preprocfile,init('pre'));
  147. { initialize a module }
  148. set_current_module(new(pmodule,init(filename,false)));
  149. macrosymtablestack:= initialmacrosymtable;
  150. current_module.localmacrosymtable:= tmacrosymtable.create(false);
  151. current_module.localmacrosymtable.next:= initialmacrosymtable;
  152. macrosymtablestack:= current_module.localmacrosymtable;
  153. main_module:=current_module;
  154. { startup scanner, and save in current_module }
  155. current_scanner:=new(pscannerfile,Init(filename));
  156. current_module.scanner:=current_scanner;
  157. { loop until EOF is found }
  158. repeat
  159. current_scanner^.readtoken(true);
  160. preprocfile^.AddSpace;
  161. case token of
  162. _ID :
  163. begin
  164. preprocfile^.Add(orgpattern);
  165. end;
  166. _REALNUMBER,
  167. _INTCONST :
  168. preprocfile^.Add(pattern);
  169. _CSTRING :
  170. begin
  171. i:=0;
  172. while (i<length(pattern)) do
  173. begin
  174. inc(i);
  175. if pattern[i]='''' then
  176. begin
  177. insert('''',pattern,i);
  178. inc(i);
  179. end;
  180. end;
  181. preprocfile^.Add(''''+pattern+'''');
  182. end;
  183. _CCHAR :
  184. begin
  185. case pattern[1] of
  186. #39 :
  187. pattern:='''''''';
  188. #0..#31,
  189. #128..#255 :
  190. begin
  191. str(ord(pattern[1]),pattern);
  192. pattern:='#'+pattern;
  193. end;
  194. else
  195. pattern:=''''+pattern[1]+'''';
  196. end;
  197. preprocfile^.Add(pattern);
  198. end;
  199. _EOF :
  200. break;
  201. else
  202. preprocfile^.Add(tokeninfo^[token].str)
  203. end;
  204. until false;
  205. { free scanner }
  206. dispose(current_scanner,done);
  207. current_scanner:=nil;
  208. { close }
  209. dispose(preprocfile,done);
  210. end;
  211. {$endif PREPROCWRITE}
  212. {*****************************************************************************
  213. Compile a source file
  214. *****************************************************************************}
  215. procedure compile(const filename:string);
  216. type
  217. polddata=^tolddata;
  218. tolddata=record
  219. { scanner }
  220. oldidtoken,
  221. oldtoken : ttoken;
  222. oldtokenpos : tfileposinfo;
  223. oldc : char;
  224. oldpattern,
  225. oldorgpattern : string;
  226. old_block_type : tblock_type;
  227. { symtable }
  228. oldsymtablestack,
  229. oldmacrosymtablestack : TSymtablestack;
  230. oldaktprocsym : tprocsym;
  231. { cg }
  232. oldparse_only : boolean;
  233. { akt.. things }
  234. oldcurrent_filepos : tfileposinfo;
  235. old_current_module : tmodule;
  236. oldcurrent_procinfo : tprocinfo;
  237. old_settings : tsettings;
  238. oldsourcecodepage : tcodepagestring;
  239. old_switchesstatestack : tswitchesstatestack;
  240. old_switchesstatestackpos : Integer;
  241. end;
  242. var
  243. olddata : polddata;
  244. hp,hp2 : tmodule;
  245. begin
  246. { parsing a procedure or declaration should be finished }
  247. if assigned(current_procinfo) then
  248. internalerror(200811121);
  249. if assigned(current_objectdef) then
  250. internalerror(200811122);
  251. inc(compile_level);
  252. parser_current_file:=filename;
  253. { Uses heap memory instead of placing everything on the
  254. stack. This is needed because compile() can be called
  255. recursively }
  256. new(olddata);
  257. with olddata^ do
  258. begin
  259. old_current_module:=current_module;
  260. { save symtable state }
  261. oldsymtablestack:=symtablestack;
  262. oldmacrosymtablestack:=macrosymtablestack;
  263. oldcurrent_procinfo:=current_procinfo;
  264. { save scanner state }
  265. oldc:=c;
  266. oldpattern:=pattern;
  267. oldorgpattern:=orgpattern;
  268. oldtoken:=token;
  269. oldidtoken:=idtoken;
  270. old_block_type:=block_type;
  271. oldtokenpos:=current_tokenpos;
  272. old_switchesstatestack:=switchesstatestack;
  273. old_switchesstatestackpos:=switchesstatestackpos;
  274. { save cg }
  275. oldparse_only:=parse_only;
  276. { save akt... state }
  277. { handle the postponed case first }
  278. flushpendingswitchesstate;
  279. oldcurrent_filepos:=current_filepos;
  280. old_settings:=current_settings;
  281. end;
  282. { reset parser, a previous fatal error could have left these variables in an unreliable state, this is
  283. important for the IDE }
  284. afterassignment:=false;
  285. in_args:=false;
  286. named_args_allowed:=false;
  287. got_addrn:=false;
  288. getprocvardef:=nil;
  289. allow_array_constructor:=false;
  290. { show info }
  291. Message1(parser_i_compiling,filename);
  292. { reset symtable }
  293. symtablestack:=TSymtablestack.create;
  294. macrosymtablestack:=TSymtablestack.create;
  295. systemunit:=nil;
  296. current_settings.defproccall:=init_settings.defproccall;
  297. current_exceptblock:=0;
  298. exceptblockcounter:=0;
  299. current_settings.maxfpuregisters:=-1;
  300. { reset the unit or create a new program }
  301. { a unit compiled at command line must be inside the loaded_unit list }
  302. if (compile_level=1) then
  303. begin
  304. if assigned(current_module) then
  305. internalerror(200501158);
  306. set_current_module(tppumodule.create(nil,filename,'',false));
  307. addloadedunit(current_module);
  308. main_module:=current_module;
  309. current_module.state:=ms_compile;
  310. end;
  311. if not(assigned(current_module) and
  312. (current_module.state in [ms_compile,ms_second_compile])) then
  313. internalerror(200212281);
  314. { Load current state from the init values }
  315. current_settings:=init_settings;
  316. { load current asmdata from current_module }
  317. current_asmdata:=TAsmData(current_module.asmdata);
  318. { startup scanner and load the first file }
  319. current_scanner:=tscannerfile.Create(filename);
  320. current_scanner.firstfile;
  321. current_module.scanner:=current_scanner;
  322. { init macros before anything in the file is parsed.}
  323. current_module.localmacrosymtable:= tmacrosymtable.create(false);
  324. macrosymtablestack.push(initialmacrosymtable);
  325. macrosymtablestack.push(current_module.localmacrosymtable);
  326. { read the first token }
  327. current_scanner.readtoken(false);
  328. { If the compile level > 1 we get a nice "unit expected" error
  329. message if we are trying to use a program as unit.}
  330. try
  331. try
  332. if (token=_UNIT) or (compile_level>1) then
  333. begin
  334. current_module.is_unit:=true;
  335. proc_unit;
  336. end
  337. else if (token=_ID) and (idtoken=_PACKAGE) then
  338. begin
  339. current_module.IsPackage:=true;
  340. proc_package;
  341. end
  342. else
  343. proc_program(token=_LIBRARY);
  344. except
  345. on ECompilerAbort do
  346. raise;
  347. on Exception do
  348. begin
  349. { Increase errorcounter to prevent some
  350. checks during cleanup }
  351. inc(status.errorcount);
  352. raise;
  353. end;
  354. end;
  355. finally
  356. if assigned(current_module) then
  357. begin
  358. { module is now compiled }
  359. tppumodule(current_module).state:=ms_compiled;
  360. { free ppu }
  361. if assigned(tppumodule(current_module).ppufile) then
  362. begin
  363. tppumodule(current_module).ppufile.free;
  364. tppumodule(current_module).ppufile:=nil;
  365. end;
  366. { free asmdata }
  367. if assigned(current_module.asmdata) then
  368. begin
  369. current_module.asmdata.free;
  370. current_module.asmdata:=nil;
  371. end;
  372. { free scanner }
  373. if assigned(current_module.scanner) then
  374. begin
  375. if current_scanner=tscannerfile(current_module.scanner) then
  376. current_scanner:=nil;
  377. tscannerfile(current_module.scanner).free;
  378. current_module.scanner:=nil;
  379. end;
  380. { free symtable stack }
  381. if assigned(symtablestack) then
  382. begin
  383. symtablestack.free;
  384. symtablestack:=nil;
  385. end;
  386. if assigned(macrosymtablestack) then
  387. begin
  388. macrosymtablestack.free;
  389. macrosymtablestack:=nil;
  390. end;
  391. end;
  392. if (compile_level=1) and
  393. (status.errorcount=0) then
  394. { Write Browser Collections }
  395. do_extractsymbolinfo;
  396. with olddata^ do
  397. begin
  398. { restore scanner }
  399. c:=oldc;
  400. pattern:=oldpattern;
  401. orgpattern:=oldorgpattern;
  402. token:=oldtoken;
  403. idtoken:=oldidtoken;
  404. current_tokenpos:=oldtokenpos;
  405. block_type:=old_block_type;
  406. switchesstatestack:=old_switchesstatestack;
  407. switchesstatestackpos:=old_switchesstatestackpos;
  408. { restore cg }
  409. parse_only:=oldparse_only;
  410. { restore symtable state }
  411. symtablestack:=oldsymtablestack;
  412. macrosymtablestack:=oldmacrosymtablestack;
  413. current_procinfo:=oldcurrent_procinfo;
  414. current_filepos:=oldcurrent_filepos;
  415. current_settings:=old_settings;
  416. current_exceptblock:=0;
  417. exceptblockcounter:=0;
  418. end;
  419. { Shut down things when the last file is compiled succesfull }
  420. if (compile_level=1) and
  421. (status.errorcount=0) then
  422. begin
  423. parser_current_file:='';
  424. { Close script }
  425. if (not AsmRes.Empty) then
  426. begin
  427. Message1(exec_i_closing_script,AsmRes.Fn);
  428. AsmRes.WriteToDisk;
  429. end;
  430. end;
  431. { free now what we did not free earlier in
  432. proc_program PM }
  433. if (compile_level=1) and needsymbolinfo then
  434. begin
  435. hp:=tmodule(loaded_units.first);
  436. while assigned(hp) do
  437. begin
  438. hp2:=tmodule(hp.next);
  439. if (hp<>current_module) then
  440. begin
  441. loaded_units.remove(hp);
  442. hp.free;
  443. end;
  444. hp:=hp2;
  445. end;
  446. { free also unneeded units we didn't free before }
  447. unloaded_units.Clear;
  448. end;
  449. dec(compile_level);
  450. set_current_module(olddata^.old_current_module);
  451. dispose(olddata);
  452. end;
  453. end;
  454. end.