parser.pas 17 KB

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