parser.pas 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  1. {
  2. $Id$
  3. Copyright (c) 1993-98 by Florian Klaempfl
  4. This unit does the parsing process
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. {$ifdef tp}
  19. {$E+,N+,D+,F+}
  20. {$endif}
  21. unit parser;
  22. interface
  23. procedure compile(const filename:string;compile_system:boolean);
  24. procedure initparser;
  25. implementation
  26. uses
  27. systems,cobjects,globals,verbose,
  28. symtable,files,aasm,hcodegen,
  29. assemble,link,script,gendef,
  30. {$ifdef UseBrowser}
  31. browser,
  32. {$endif UseBrowser}
  33. scanner,pbase,pdecl,psystem,pmodules;
  34. procedure initparser;
  35. begin
  36. forwardsallowed:=false;
  37. { ^M means a string or a char, because we don't parse a }
  38. { type declaration }
  39. ignore_equal:=false;
  40. { we didn't parse a object or class declaration }
  41. { and no function header }
  42. testcurobject:=0;
  43. { create error defintion }
  44. generrordef:=new(perrordef,init);
  45. symtablestack:=nil;
  46. { a long time, this was forgotten }
  47. aktprocsym:=nil;
  48. current_module:=nil;
  49. loaded_units.init;
  50. usedunits.init;
  51. end;
  52. { moved out to save stack }
  53. var
  54. addparam : string;
  55. procedure compile(const filename:string;compile_system:boolean);
  56. var
  57. hp : pmodule;
  58. { some variables to save the compiler state }
  59. oldtoken : ttoken;
  60. oldtokenpos : tfileposinfo;
  61. oldpattern : stringid;
  62. oldpreprocstack : ppreprocstack;
  63. oldorgpattern,oldprocprefix : string;
  64. old_block_type : tblock_type;
  65. oldlastlinepos,
  66. oldinputbuffer,
  67. oldinputpointer : pchar;
  68. olds_point,oldparse_only : boolean;
  69. oldc : char;
  70. oldcomment_level : word;
  71. oldnextlabelnr : longint;
  72. oldmacros,oldrefsymtable,oldsymtablestack : psymtable;
  73. oldimports,oldexports,oldresource,oldrttilist,
  74. oldbsssegment,olddatasegment,oldcodesegment,
  75. oldexprasmlist,olddebuglist,
  76. oldinternals,oldexternals,oldconsts : paasmoutput;
  77. oldswitches : tcswitches;
  78. oldpackrecords : word;
  79. oldoutputformat : tasm;
  80. oldoptprocessor : tprocessors;
  81. oldasmmode : tasmmode;
  82. procedure def_macro(const s : string);
  83. var
  84. mac : pmacrosym;
  85. begin
  86. mac:=pmacrosym(macros^.search(s));
  87. if mac=nil then
  88. begin
  89. mac:=new(pmacrosym,init(s));
  90. Message1(parser_m_macro_defined,mac^.name);
  91. macros^.insert(mac);
  92. end;
  93. mac^.defined:=true;
  94. end;
  95. procedure set_macro(const s : string;value : string);
  96. var
  97. mac : pmacrosym;
  98. begin
  99. mac:=pmacrosym(macros^.search(s));
  100. if mac=nil then
  101. begin
  102. mac:=new(pmacrosym,init(s));
  103. macros^.insert(mac);
  104. end
  105. else
  106. begin
  107. if assigned(mac^.buftext) then
  108. freemem(mac^.buftext,mac^.buflen);
  109. end;
  110. Message2(parser_m_macro_set_to,mac^.name,value);
  111. mac^.buflen:=length(value);
  112. getmem(mac^.buftext,mac^.buflen);
  113. move(value[1],mac^.buftext^,mac^.buflen);
  114. mac^.defined:=true;
  115. end;
  116. procedure define_macros;
  117. var
  118. hp : pstring_item;
  119. begin
  120. hp:=pstring_item(initdefines.first);
  121. while assigned(hp) do
  122. begin
  123. def_macro(hp^.str^);
  124. hp:=pstring_item(hp^.next);
  125. end;
  126. { set macros for version checking }
  127. set_macro('FPC_VERSION',version_nr);
  128. set_macro('FPC_RELEASE',release_nr);
  129. set_macro('FPC_PATCH',patch_nr);
  130. end;
  131. label
  132. done;
  133. begin {compile}
  134. inc(compile_level);
  135. { save old state }
  136. { save symtable state }
  137. oldsymtablestack:=symtablestack;
  138. symtablestack:=nil;
  139. oldrefsymtable:=refsymtable;
  140. refsymtable:=nil;
  141. oldprocprefix:=procprefix;
  142. { a long time, this was only in init_parser
  143. but it should be reset to zero for each module }
  144. aktprocsym:=nil;
  145. { first, we assume a program }
  146. if not(assigned(current_module)) then
  147. begin
  148. current_module:=new(pmodule,init(filename,false));
  149. main_module:=current_module;
  150. end;
  151. { save scanner state }
  152. oldmacros:=macros;
  153. oldpattern:=pattern;
  154. oldtoken:=token;
  155. oldtokenpos:=tokenpos;
  156. oldorgpattern:=orgpattern;
  157. old_block_type:=block_type;
  158. oldpreprocstack:=preprocstack;
  159. oldinputbuffer:=inputbuffer;
  160. oldinputpointer:=inputpointer;
  161. oldlastlinepos:=lastlinepos;
  162. olds_point:=s_point;
  163. oldc:=c;
  164. oldcomment_level:=comment_level;
  165. oldnextlabelnr:=nextlabelnr;
  166. oldparse_only:=parse_only;
  167. { save assembler lists }
  168. olddatasegment:=datasegment;
  169. oldbsssegment:=bsssegment;
  170. oldcodesegment:=codesegment;
  171. olddebuglist:=debuglist;
  172. oldexternals:=externals;
  173. oldinternals:=internals;
  174. oldconsts:=consts;
  175. oldrttilist:=rttilist;
  176. oldexprasmlist:=exprasmlist;
  177. oldimports:=importssection;
  178. oldexports:=exportssection;
  179. oldresource:=resourcesection;
  180. { save the current state }
  181. oldswitches:=aktswitches;
  182. oldpackrecords:=aktpackrecords;
  183. oldoutputformat:=aktoutputformat;
  184. oldoptprocessor:=aktoptprocessor;
  185. oldasmmode:=aktasmmode;
  186. Message1(parser_i_compiling,filename);
  187. InitScanner(filename);
  188. { Load current state from the init values }
  189. aktswitches:=initswitches;
  190. aktpackrecords:=initpackrecords;
  191. aktoutputformat:=initoutputformat;
  192. aktoptprocessor:=initoptprocessor;
  193. aktasmmode:=initasmmode;
  194. { we need this to make the system unit }
  195. if compile_system then
  196. aktswitches:=aktswitches+[cs_compilesystem];
  197. { macros }
  198. macros:=new(psymtable,init(macrosymtable));
  199. macros^.name:=stringdup('Conditionals for '+filename);
  200. define_macros;
  201. { startup scanner }
  202. token:=yylex;
  203. { init code generator for a new module }
  204. codegen_newmodule;
  205. {$ifdef GDB}
  206. reset_gdb_info;
  207. {$endif GDB}
  208. { global switches are read, so further changes aren't allowed }
  209. current_module^.in_main:=true;
  210. { Handle things which need to be once }
  211. if (compile_level=1) then
  212. begin
  213. { open assembler response }
  214. AsmRes.Init('ppas');
  215. {$ifdef UseBrowser}
  216. { open browser if set }
  217. if cs_browser in initswitches then
  218. Browse.CreateLog;
  219. {$endif UseBrowser}
  220. end;
  221. { if the current file isn't a system unit }
  222. { the the system unit will be loaded }
  223. if not(cs_compilesystem in aktswitches) then
  224. begin
  225. { should be done in unit system (changing the field system_unit)
  226. FK
  227. }
  228. hp:=loadunit(upper(target_info.system_unit),true,true);
  229. systemunit:=hp^.symtable;
  230. make_ref:=false;
  231. readconstdefs;
  232. { we could try to overload caret by default }
  233. symtablestack:=systemunit;
  234. { if POWER is defined in the RTL then use it for starstar overloading }
  235. getsym('POWER',false);
  236. if assigned(srsym) and (srsym^.typ=procsym) and
  237. (overloaded_operators[STARSTAR]=nil) then
  238. begin
  239. overloaded_operators[STARSTAR]:=
  240. new(pprocsym,init(overloaded_names[STARSTAR]));
  241. overloaded_operators[STARSTAR]^.definition:=pprocsym(srsym)^.definition;
  242. end;
  243. make_ref:=true;
  244. end
  245. else
  246. begin
  247. createconstdefs;
  248. systemunit:=nil;
  249. end;
  250. registerdef:=true;
  251. make_ref:=true;
  252. { current return type is void }
  253. procinfo.retdef:=voiddef;
  254. { reset lexical level }
  255. lexlevel:=0;
  256. { parse source }
  257. if (token=_UNIT) or (compile_level>1) then
  258. begin
  259. current_module^.is_unit:=true;
  260. { If the compile level > 1 we get a nice "unit expected" error
  261. message if we are trying to use a program as unit.}
  262. proc_unit;
  263. if current_module^.compiled then
  264. goto done;
  265. end
  266. else
  267. begin
  268. proc_program(token=_LIBRARY);
  269. end;
  270. if status.errorcount=0 then
  271. begin
  272. GenerateAsm(filename);
  273. if (cs_smartlink in aktswitches) then
  274. begin
  275. Linker.SetLibName(current_module^.libfilename^);
  276. Linker.MakeStaticLibrary(SmartLinkPath(FileName),SmartLinkFilesCnt);
  277. end;
  278. { add the files for the linker from current_module, this must be
  279. after the makestaticlibrary, because it will add the library
  280. name (PFV) }
  281. addlinkerfiles(current_module);
  282. { Check linking => we are at first level in compile }
  283. if (compile_level=1) then
  284. begin
  285. if gendeffile then
  286. deffile.writefile;
  287. if (not current_module^.is_unit) then
  288. begin
  289. if (cs_no_linking in initswitches) then
  290. externlink:=true;
  291. if Linker.ExeName='' then
  292. Linker.SetExeName(FileName);
  293. Linker.MakeExecutable;
  294. end;
  295. end;
  296. end
  297. else
  298. Message1(unit_f_errors_in_unit,tostr(status.errorcount));
  299. done:
  300. { clear memory }
  301. {$ifdef Splitheap}
  302. if testsplit then
  303. begin
  304. { temp heap should be empty after that !!!}
  305. codegen_donemodule;
  306. Releasetempheap;
  307. end;
  308. {$endif Splitheap}
  309. { restore old state, close trees }
  310. if dispose_asm_lists then
  311. codegen_donemodule;
  312. {$ifdef GDB}
  313. reset_gdb_info;
  314. {$endif GDB}
  315. { restore symtable state }
  316. {$ifdef UseBrowser}
  317. if (compile_level>1) then
  318. { we want to keep the current symtablestack }
  319. {$endif UseBrowser}
  320. begin
  321. refsymtable:=oldrefsymtable;
  322. symtablestack:=oldsymtablestack;
  323. end;
  324. procprefix:=oldprocprefix;
  325. {$ifdef UseBrowser}
  326. { close input files, but dont remove if we use the browser ! }
  327. if cs_browser in initswitches then
  328. current_module^.sourcefiles.close_all
  329. else
  330. current_module^.sourcefiles.done;
  331. {$else UseBrowser}
  332. { close the inputfiles }
  333. current_module^.sourcefiles.done;
  334. {$endif not UseBrowser}
  335. { restore scanner state }
  336. pattern:=oldpattern;
  337. token:=oldtoken;
  338. tokenpos:=oldtokenpos;
  339. orgpattern:=oldorgpattern;
  340. block_type:=old_block_type;
  341. { call donescanner before restoring preprocstack, because }
  342. { donescanner tests for a empty preprocstack }
  343. { and can also check for unused macros }
  344. donescanner(current_module^.compiled);
  345. dispose(macros,done);
  346. macros:=oldmacros;
  347. { restore scanner }
  348. preprocstack:=oldpreprocstack;
  349. inputbuffer:=oldinputbuffer;
  350. inputpointer:=oldinputpointer;
  351. lastlinepos:=oldlastlinepos;
  352. s_point:=olds_point;
  353. c:=oldc;
  354. comment_level:=oldcomment_level;
  355. nextlabelnr:=oldnextlabelnr;
  356. parse_only:=oldparse_only;
  357. { restore asmlists }
  358. exprasmlist:=oldexprasmlist;
  359. datasegment:=olddatasegment;
  360. bsssegment:=oldbsssegment;
  361. codesegment:=oldcodesegment;
  362. consts:=oldconsts;
  363. debuglist:=olddebuglist;
  364. externals:=oldexternals;
  365. internals:=oldinternals;
  366. importssection:=oldimports;
  367. exportssection:=oldexports;
  368. resourcesection:=oldresource;
  369. rttilist:=oldrttilist;
  370. { restore current state }
  371. aktswitches:=oldswitches;
  372. aktpackrecords:=oldpackrecords;
  373. aktoutputformat:=oldoutputformat;
  374. aktoptprocessor:=oldoptprocessor;
  375. aktasmmode:=oldasmmode;
  376. { Shut down things when the last file is compiled }
  377. if (compile_level=1) then
  378. begin
  379. { Close script }
  380. if (not AsmRes.Empty) then
  381. begin
  382. Message1(exec_i_closing_script,AsmRes.Fn);
  383. AsmRes.WriteToDisk;
  384. end;
  385. {$ifdef UseBrowser}
  386. { Write Browser }
  387. if cs_browser in initswitches then
  388. begin
  389. Comment(V_Info,'Writing Browser '+Browse.Fname);
  390. write_browser_log;
  391. end;
  392. {$endif UseBrowser}
  393. end;
  394. dec(compile_level);
  395. end;
  396. end.
  397. {
  398. $Log$
  399. Revision 1.25 1998-06-15 15:38:07 pierre
  400. * small bug in systems.pas corrected
  401. + operators in different units better hanlded
  402. Revision 1.24 1998/06/13 00:10:08 peter
  403. * working browser and newppu
  404. * some small fixes against crashes which occured in bp7 (but not in
  405. fpc?!)
  406. Revision 1.23 1998/06/08 22:59:48 peter
  407. * smartlinking works for win32
  408. * some defines to exclude some compiler parts
  409. Revision 1.22 1998/06/05 17:47:28 peter
  410. * some better uses clauses
  411. Revision 1.21 1998/06/04 23:51:49 peter
  412. * m68k compiles
  413. + .def file creation moved to gendef.pas so it could also be used
  414. for win32
  415. Revision 1.20 1998/06/03 22:48:55 peter
  416. + wordbool,longbool
  417. * rename bis,von -> high,low
  418. * moved some systemunit loading/creating to psystem.pas
  419. Revision 1.19 1998/05/27 19:45:04 peter
  420. * symtable.pas splitted into includefiles
  421. * symtable adapted for $ifdef NEWPPU
  422. Revision 1.18 1998/05/23 01:21:15 peter
  423. + aktasmmode, aktoptprocessor, aktoutputformat
  424. + smartlink per module $SMARTLINK-/+ (like MMX) and moved to aktswitches
  425. + $LIBNAME to set the library name where the unit will be put in
  426. * splitted cgi386 a bit (codeseg to large for bp7)
  427. * nasm, tasm works again. nasm moved to ag386nsm.pas
  428. Revision 1.17 1998/05/20 09:42:34 pierre
  429. + UseTokenInfo now default
  430. * unit in interface uses and implementation uses gives error now
  431. * only one error for unknown symbol (uses lastsymknown boolean)
  432. the problem came from the label code !
  433. + first inlined procedures and function work
  434. (warning there might be allowed cases were the result is still wrong !!)
  435. * UseBrower updated gives a global list of all position of all used symbols
  436. with switch -gb
  437. Revision 1.16 1998/05/12 10:47:00 peter
  438. * moved printstatus to verb_def
  439. + V_Normal which is between V_Error and V_Warning and doesn't have a
  440. prefix like error: warning: and is included in V_Default
  441. * fixed some messages
  442. * first time parameter scan is only for -v and -T
  443. - removed old style messages
  444. Revision 1.15 1998/05/11 13:07:54 peter
  445. + $ifdef NEWPPU for the new ppuformat
  446. + $define GDB not longer required
  447. * removed all warnings and stripped some log comments
  448. * no findfirst/findnext anymore to remove smartlink *.o files
  449. Revision 1.14 1998/05/06 18:36:53 peter
  450. * tai_section extended with code,data,bss sections and enumerated type
  451. * ident 'compiled by FPC' moved to pmodules
  452. * small fix for smartlink
  453. Revision 1.13 1998/05/06 08:38:42 pierre
  454. * better position info with UseTokenInfo
  455. UseTokenInfo greatly simplified
  456. + added check for changed tree after first time firstpass
  457. (if we could remove all the cases were it happen
  458. we could skip all firstpass if firstpasscount > 1)
  459. Only with ExtDebug
  460. Revision 1.12 1998/05/04 17:54:28 peter
  461. + smartlinking works (only case jumptable left todo)
  462. * redesign of systems.pas to support assemblers and linkers
  463. + Unitname is now also in the PPU-file, increased version to 14
  464. Revision 1.11 1998/05/01 16:38:45 florian
  465. * handling of private and protected fixed
  466. + change_keywords_to_tp implemented to remove
  467. keywords which aren't supported by tp
  468. * break and continue are now symbols of the system unit
  469. + widestring, longstring and ansistring type released
  470. Revision 1.10 1998/05/01 07:43:56 florian
  471. + basics for rtti implemented
  472. + switch $m (generate rtti for published sections)
  473. Revision 1.9 1998/04/30 15:59:40 pierre
  474. * GDB works again better :
  475. correct type info in one pass
  476. + UseTokenInfo for better source position
  477. * fixed one remaining bug in scanner for line counts
  478. * several little fixes
  479. Revision 1.8 1998/04/29 10:33:55 pierre
  480. + added some code for ansistring (not complete nor working yet)
  481. * corrected operator overloading
  482. * corrected nasm output
  483. + started inline procedures
  484. + added starstarn : use ** for exponentiation (^ gave problems)
  485. + started UseTokenInfo cond to get accurate positions
  486. Revision 1.7 1998/04/27 23:10:28 peter
  487. + new scanner
  488. * $makelib -> if smartlink
  489. * small filename fixes pmodule.setfilename
  490. * moved import from files.pas -> import.pas
  491. Revision 1.6 1998/04/21 10:16:48 peter
  492. * patches from strasbourg
  493. * objects is not used anymore in the fpc compiled version
  494. Revision 1.5 1998/04/10 14:41:43 peter
  495. * removed some Hints
  496. * small speed optimization for AsmLn
  497. Revision 1.4 1998/04/08 16:58:03 pierre
  498. * several bugfixes
  499. ADD ADC and AND are also sign extended
  500. nasm output OK (program still crashes at end
  501. and creates wrong assembler files !!)
  502. procsym types sym in tdef removed !!
  503. Revision 1.3 1998/04/07 22:45:04 florian
  504. * bug0092, bug0115 and bug0121 fixed
  505. + packed object/class/array
  506. }