parser.pas 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  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. {$ifdef BrowserCol}
  41. browcol,
  42. {$endif BrowserCol}
  43. {$ifdef BrowserLog}
  44. browlog,
  45. {$endif BrowserLog}
  46. comphook,
  47. scanner,scandir,
  48. pbase,ptype,psystem,pmodules,psub,
  49. cresstr,cpuinfo,procinfo;
  50. procedure initparser;
  51. begin
  52. { ^M means a string or a char, because we don't parse a }
  53. { type declaration }
  54. ignore_equal:=false;
  55. { we didn't parse a object or class declaration }
  56. { and no function header }
  57. testcurobject:=0;
  58. { Current compiled module/proc }
  59. current_module:=nil;
  60. compiled_module:=nil;
  61. current_asmdata:=nil;
  62. current_procinfo:=nil;
  63. SetCompileModule(nil);
  64. loaded_units:=TLinkedList.Create;
  65. usedunits:=TLinkedList.Create;
  66. { global switches }
  67. current_settings.globalswitches:=init_settings.globalswitches;
  68. current_settings.sourcecodepage:=init_settings.sourcecodepage;
  69. { initialize scanner }
  70. InitScanner;
  71. InitScannerDirectives;
  72. { scanner }
  73. c:=#0;
  74. pattern:='';
  75. orgpattern:='';
  76. current_scanner:=nil;
  77. { register all nodes and tais }
  78. registernodes;
  79. registertais;
  80. { memory sizes }
  81. if stacksize=0 then
  82. stacksize:=target_info.stacksize;
  83. { open assembler response }
  84. if cs_link_on_target in current_settings.globalswitches then
  85. GenerateAsmRes(outputexedir+ChangeFileExt(inputfilename,'_ppas'))
  86. else
  87. GenerateAsmRes(outputexedir+'ppas');
  88. { open deffile }
  89. DefFile:=TDefFile.Create(outputexedir+ChangeFileExt(inputfilename,target_info.defext));
  90. { list of generated .o files, so the linker can remove them }
  91. SmartLinkOFiles:=TStringList.Create;
  92. { codegen }
  93. if paraprintnodetree<>0 then
  94. printnode_reset;
  95. { target specific stuff }
  96. case target_info.system of
  97. system_powerpc_amiga:
  98. include(supported_calling_conventions,pocall_syscall);
  99. system_powerpc_morphos:
  100. include(supported_calling_conventions,pocall_syscall);
  101. system_m68k_amiga:
  102. include(supported_calling_conventions,pocall_syscall);
  103. end;
  104. end;
  105. procedure doneparser;
  106. begin
  107. { Reset current compiling info, so destroy routines can't
  108. reference the data that might already be destroyed }
  109. current_module:=nil;
  110. compiled_module:=nil;
  111. current_procinfo:=nil;
  112. current_asmdata:=nil;
  113. SetCompileModule(nil);
  114. { unload units }
  115. if assigned(loaded_units) then
  116. begin
  117. loaded_units.free;
  118. loaded_units:=nil;
  119. end;
  120. if assigned(usedunits) then
  121. begin
  122. usedunits.free;
  123. usedunits:=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. { 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. begin
  242. inc(compile_level);
  243. parser_current_file:=filename;
  244. { Uses heap memory instead of placing everything on the
  245. stack. This is needed because compile() can be called
  246. recursively }
  247. new(olddata);
  248. with olddata^ do
  249. begin
  250. old_compiled_module:=compiled_module;
  251. { save symtable state }
  252. oldsymtablestack:=symtablestack;
  253. oldmacrosymtablestack:=macrosymtablestack;
  254. oldcurrent_procinfo:=current_procinfo;
  255. { save scanner state }
  256. oldc:=c;
  257. oldpattern:=pattern;
  258. oldorgpattern:=orgpattern;
  259. oldtoken:=token;
  260. oldidtoken:=idtoken;
  261. old_block_type:=block_type;
  262. oldtokenpos:=current_tokenpos;
  263. { save cg }
  264. oldparse_only:=parse_only;
  265. { save akt... state }
  266. { handle the postponed case first }
  267. if localswitcheschanged then
  268. begin
  269. current_settings.localswitches:=nextlocalswitches;
  270. localswitcheschanged:=false;
  271. end;
  272. oldcurrent_filepos:=current_filepos;
  273. old_settings:=current_settings;
  274. end;
  275. { reset parser, a previous fatal error could have left these variables in an unreliable state, this is
  276. important for the IDE }
  277. afterassignment:=false;
  278. in_args:=false;
  279. got_addrn:=false;
  280. getprocvardef:=nil;
  281. { show info }
  282. Message1(parser_i_compiling,filename);
  283. { reset symtable }
  284. symtablestack:=tsymtablestack.create;
  285. macrosymtablestack:=tsymtablestack.create;
  286. systemunit:=nil;
  287. current_settings.defproccall:=init_settings.defproccall;
  288. aktexceptblock:=0;
  289. exceptblockcounter:=0;
  290. current_settings.maxfpuregisters:=-1;
  291. { reset the unit or create a new program }
  292. { a unit compiled at command line must be inside the loaded_unit list }
  293. if (compile_level=1) then
  294. begin
  295. if assigned(current_module) then
  296. internalerror(200501158);
  297. current_module:=tppumodule.create(nil,filename,'',false);
  298. addloadedunit(current_module);
  299. main_module:=current_module;
  300. current_module.state:=ms_compile;
  301. end;
  302. if not(assigned(current_module) and
  303. (current_module.state in [ms_compile,ms_second_compile])) then
  304. internalerror(200212281);
  305. { Set the module to use for verbose }
  306. compiled_module:=current_module;
  307. SetCompileModule(current_module);
  308. Fillchar(current_filepos,0,sizeof(current_filepos));
  309. { Load current state from the init values }
  310. current_settings:=init_settings;
  311. { load current asmdata from current_module }
  312. current_asmdata:=TAsmData(current_module.asmdata);
  313. { startup scanner and load the first file }
  314. current_scanner:=tscannerfile.Create(filename);
  315. current_scanner.firstfile;
  316. current_module.scanner:=current_scanner;
  317. { init macros before anything in the file is parsed.}
  318. current_module.localmacrosymtable:= tmacrosymtable.create(false);
  319. macrosymtablestack.push(initialmacrosymtable);
  320. macrosymtablestack.push(current_module.localmacrosymtable);
  321. { read the first token }
  322. current_scanner.readtoken(false);
  323. { If the compile level > 1 we get a nice "unit expected" error
  324. message if we are trying to use a program as unit.}
  325. try
  326. try
  327. if (token=_UNIT) or (compile_level>1) then
  328. begin
  329. current_module.is_unit:=true;
  330. proc_unit;
  331. end
  332. else
  333. proc_program(token=_LIBRARY);
  334. except
  335. on ECompilerAbort do
  336. raise;
  337. on Exception do
  338. begin
  339. { Increase errorcounter to prevent some
  340. checks during cleanup }
  341. inc(status.errorcount);
  342. raise;
  343. end;
  344. end;
  345. finally
  346. if assigned(current_module) then
  347. begin
  348. { module is now compiled }
  349. tppumodule(current_module).state:=ms_compiled;
  350. { free ppu }
  351. if assigned(tppumodule(current_module).ppufile) then
  352. begin
  353. tppumodule(current_module).ppufile.free;
  354. tppumodule(current_module).ppufile:=nil;
  355. end;
  356. { free asmdata }
  357. if assigned(current_module.asmdata) then
  358. begin
  359. current_module.asmdata.free;
  360. current_module.asmdata:=nil;
  361. end;
  362. { free scanner }
  363. if assigned(current_module.scanner) then
  364. begin
  365. if current_scanner=tscannerfile(current_module.scanner) then
  366. current_scanner:=nil;
  367. tscannerfile(current_module.scanner).free;
  368. current_module.scanner:=nil;
  369. end;
  370. { free symtable stack }
  371. if assigned(symtablestack) then
  372. begin
  373. symtablestack.free;
  374. symtablestack:=nil;
  375. end;
  376. if assigned(macrosymtablestack) then
  377. begin
  378. macrosymtablestack.free;
  379. macrosymtablestack:=nil;
  380. end;
  381. end;
  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. { do not create browsers on errors !! }
  427. if status.errorcount=0 then
  428. begin
  429. {$ifdef BrowserLog}
  430. { Write Browser Log }
  431. if (cs_browser_log in current_settings.globalswitches) and
  432. (cs_browser in current_settings.moduleswitches) then
  433. begin
  434. if browserlog.elements_to_list.empty then
  435. begin
  436. Message1(parser_i_writing_browser_log,browserlog.Fname);
  437. WriteBrowserLog;
  438. end
  439. else
  440. browserlog.list_elements;
  441. end;
  442. {$endif BrowserLog}
  443. { Write Browser Collections, also used by the TextMode IDE to
  444. retrieve a list of sourcefiles }
  445. do_extractsymbolinfo();
  446. end;
  447. end;
  448. dec(compile_level);
  449. compiled_module:=olddata^.old_compiled_module;
  450. SetCompileModule(compiled_module);
  451. dispose(olddata);
  452. end;
  453. end;
  454. end.