parser.pas 16 KB

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