parser.pas 16 KB

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