parser.pas 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  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,
  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. { open assembler response }
  78. if cs_link_on_target in current_settings.globalswitches then
  79. GenerateAsmRes(outputexedir+ChangeFileExt(inputfilename,'_ppas'))
  80. else
  81. GenerateAsmRes(outputexedir+'ppas');
  82. { open deffile }
  83. DefFile:=TDefFile.Create(outputexedir+ChangeFileExt(inputfilename,target_info.defext));
  84. { list of generated .o files, so the linker can remove them }
  85. SmartLinkOFiles:=TStringList.Create;
  86. { codegen }
  87. if paraprintnodetree<>0 then
  88. printnode_reset;
  89. { target specific stuff }
  90. case target_info.system of
  91. system_powerpc_amiga:
  92. include(supported_calling_conventions,pocall_syscall);
  93. system_powerpc_morphos:
  94. include(supported_calling_conventions,pocall_syscall);
  95. system_m68k_amiga:
  96. include(supported_calling_conventions,pocall_syscall);
  97. end;
  98. end;
  99. procedure doneparser;
  100. begin
  101. { Reset current compiling info, so destroy routines can't
  102. reference the data that might already be destroyed }
  103. current_module:=nil;
  104. compiled_module:=nil;
  105. current_procinfo:=nil;
  106. current_asmdata:=nil;
  107. SetCompileModule(nil);
  108. { unload units }
  109. if assigned(loaded_units) then
  110. begin
  111. loaded_units.free;
  112. loaded_units:=nil;
  113. end;
  114. if assigned(usedunits) then
  115. begin
  116. usedunits.free;
  117. usedunits:=nil;
  118. end;
  119. { if there was an error in the scanner, the scanner is
  120. still assinged }
  121. if assigned(current_scanner) then
  122. begin
  123. current_scanner.free;
  124. current_scanner:=nil;
  125. end;
  126. { close scanner }
  127. DoneScanner;
  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. begin
  236. inc(compile_level);
  237. parser_current_file:=filename;
  238. { Uses heap memory instead of placing everything on the
  239. stack. This is needed because compile() can be called
  240. recursively }
  241. new(olddata);
  242. with olddata^ do
  243. begin
  244. old_compiled_module:=compiled_module;
  245. { save symtable state }
  246. oldsymtablestack:=symtablestack;
  247. oldmacrosymtablestack:=macrosymtablestack;
  248. oldcurrent_procinfo:=current_procinfo;
  249. { save scanner state }
  250. oldc:=c;
  251. oldpattern:=pattern;
  252. oldorgpattern:=orgpattern;
  253. oldtoken:=token;
  254. oldidtoken:=idtoken;
  255. old_block_type:=block_type;
  256. oldtokenpos:=current_tokenpos;
  257. { save cg }
  258. oldparse_only:=parse_only;
  259. { save akt... state }
  260. { handle the postponed case first }
  261. if localswitcheschanged then
  262. begin
  263. current_settings.localswitches:=nextlocalswitches;
  264. localswitcheschanged:=false;
  265. end;
  266. oldcurrent_filepos:=current_filepos;
  267. old_settings:=current_settings;
  268. end;
  269. { reset parser, a previous fatal error could have left these variables in an unreliable state, this is
  270. important for the IDE }
  271. afterassignment:=false;
  272. in_args:=false;
  273. named_args_allowed:=false;
  274. got_addrn:=false;
  275. getprocvardef:=nil;
  276. { show info }
  277. Message1(parser_i_compiling,filename);
  278. { reset symtable }
  279. symtablestack:=TSymtablestack.create;
  280. macrosymtablestack:=TSymtablestack.create;
  281. systemunit:=nil;
  282. current_settings.defproccall:=init_settings.defproccall;
  283. aktexceptblock:=0;
  284. exceptblockcounter:=0;
  285. current_settings.maxfpuregisters:=-1;
  286. { reset the unit or create a new program }
  287. { a unit compiled at command line must be inside the loaded_unit list }
  288. if (compile_level=1) then
  289. begin
  290. if assigned(current_module) then
  291. internalerror(200501158);
  292. current_module:=tppumodule.create(nil,filename,'',false);
  293. addloadedunit(current_module);
  294. main_module:=current_module;
  295. current_module.state:=ms_compile;
  296. end;
  297. if not(assigned(current_module) and
  298. (current_module.state in [ms_compile,ms_second_compile])) then
  299. internalerror(200212281);
  300. { Set the module to use for verbose }
  301. compiled_module:=current_module;
  302. SetCompileModule(current_module);
  303. Fillchar(current_filepos,0,sizeof(current_filepos));
  304. { Load current state from the init values }
  305. current_settings:=init_settings;
  306. { load current asmdata from current_module }
  307. current_asmdata:=TAsmData(current_module.asmdata);
  308. { startup scanner and load the first file }
  309. current_scanner:=tscannerfile.Create(filename);
  310. current_scanner.firstfile;
  311. current_module.scanner:=current_scanner;
  312. { init macros before anything in the file is parsed.}
  313. current_module.localmacrosymtable:= tmacrosymtable.create(false);
  314. macrosymtablestack.push(initialmacrosymtable);
  315. macrosymtablestack.push(current_module.localmacrosymtable);
  316. { read the first token }
  317. current_scanner.readtoken(false);
  318. { If the compile level > 1 we get a nice "unit expected" error
  319. message if we are trying to use a program as unit.}
  320. try
  321. try
  322. if (token=_UNIT) or (compile_level>1) then
  323. begin
  324. current_module.is_unit:=true;
  325. proc_unit;
  326. end
  327. else
  328. proc_program(token=_LIBRARY);
  329. except
  330. on ECompilerAbort do
  331. raise;
  332. on Exception do
  333. begin
  334. { Increase errorcounter to prevent some
  335. checks during cleanup }
  336. inc(status.errorcount);
  337. raise;
  338. end;
  339. end;
  340. finally
  341. if assigned(current_module) then
  342. begin
  343. { module is now compiled }
  344. tppumodule(current_module).state:=ms_compiled;
  345. { free ppu }
  346. if assigned(tppumodule(current_module).ppufile) then
  347. begin
  348. tppumodule(current_module).ppufile.free;
  349. tppumodule(current_module).ppufile:=nil;
  350. end;
  351. { free asmdata }
  352. if assigned(current_module.asmdata) then
  353. begin
  354. current_module.asmdata.free;
  355. current_module.asmdata:=nil;
  356. end;
  357. { free scanner }
  358. if assigned(current_module.scanner) then
  359. begin
  360. if current_scanner=tscannerfile(current_module.scanner) then
  361. current_scanner:=nil;
  362. tscannerfile(current_module.scanner).free;
  363. current_module.scanner:=nil;
  364. end;
  365. { free symtable stack }
  366. if assigned(symtablestack) then
  367. begin
  368. symtablestack.free;
  369. symtablestack:=nil;
  370. end;
  371. if assigned(macrosymtablestack) then
  372. begin
  373. macrosymtablestack.free;
  374. macrosymtablestack:=nil;
  375. end;
  376. end;
  377. with olddata^ do
  378. begin
  379. { restore scanner }
  380. c:=oldc;
  381. pattern:=oldpattern;
  382. orgpattern:=oldorgpattern;
  383. token:=oldtoken;
  384. idtoken:=oldidtoken;
  385. current_tokenpos:=oldtokenpos;
  386. block_type:=old_block_type;
  387. { restore cg }
  388. parse_only:=oldparse_only;
  389. { asm data }
  390. if assigned(old_compiled_module) then
  391. current_asmdata:=tasmdata(old_compiled_module.asmdata)
  392. else
  393. current_asmdata:=nil;
  394. { restore previous scanner }
  395. if assigned(old_compiled_module) then
  396. current_scanner:=tscannerfile(old_compiled_module.scanner)
  397. else
  398. current_scanner:=nil;
  399. if assigned(current_scanner) then
  400. parser_current_file:=current_scanner.inputfile.name^;
  401. { restore symtable state }
  402. symtablestack:=oldsymtablestack;
  403. macrosymtablestack:=oldmacrosymtablestack;
  404. current_procinfo:=oldcurrent_procinfo;
  405. current_filepos:=oldcurrent_filepos;
  406. current_settings:=old_settings;
  407. aktexceptblock:=0;
  408. exceptblockcounter:=0;
  409. end;
  410. { Shut down things when the last file is compiled succesfull }
  411. if (compile_level=1) and
  412. (status.errorcount=0) then
  413. begin
  414. parser_current_file:='';
  415. { Close script }
  416. if (not AsmRes.Empty) then
  417. begin
  418. Message1(exec_i_closing_script,AsmRes.Fn);
  419. AsmRes.WriteToDisk;
  420. end;
  421. end;
  422. dec(compile_level);
  423. compiled_module:=olddata^.old_compiled_module;
  424. SetCompileModule(compiled_module);
  425. dispose(olddata);
  426. end;
  427. end;
  428. end.