2
0

parser.pas 17 KB

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