parser.pas 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  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. 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. GenerateAsm(filename);
  256. if (cs_smartlink in aktswitches) then
  257. begin
  258. Linker.SetLibName(current_module^.libfilename^);
  259. Linker.MakeStaticLibrary(SmartLinkPath(FileName),SmartLinkFilesCnt);
  260. end;
  261. { add the files for the linker from current_module, this must be
  262. after the makestaticlibrary, because it will add the library
  263. name (PFV) }
  264. addlinkerfiles(current_module);
  265. { Check linking => we are at first level in compile }
  266. if (compile_level=1) then
  267. begin
  268. if gendeffile then
  269. deffile.writefile;
  270. if (not current_module^.is_unit) then
  271. begin
  272. if (cs_no_linking in initswitches) then
  273. externlink:=true;
  274. if Linker.ExeName='' then
  275. Linker.SetExeName(FileName);
  276. Linker.MakeExecutable;
  277. end;
  278. end;
  279. end
  280. else
  281. Message1(unit_f_errors_in_unit,tostr(status.errorcount));
  282. done:
  283. { clear memory }
  284. {$ifdef Splitheap}
  285. if testsplit then
  286. begin
  287. { temp heap should be empty after that !!!}
  288. codegen_donemodule;
  289. Releasetempheap;
  290. end;
  291. {$endif Splitheap}
  292. { restore old state, close trees }
  293. if dispose_asm_lists then
  294. codegen_donemodule;
  295. {$ifdef GDB}
  296. reset_gdb_info;
  297. {$endif GDB}
  298. { restore symtable state }
  299. {$ifdef UseBrowser}
  300. if (compile_level>1) then
  301. { we want to keep the current symtablestack }
  302. {$endif UseBrowser}
  303. begin
  304. refsymtable:=oldrefsymtable;
  305. symtablestack:=oldsymtablestack;
  306. end;
  307. procprefix:=oldprocprefix;
  308. { close the inputfiles }
  309. {$ifdef UseBrowser}
  310. { we need the names for the browser ! }
  311. current_module^.sourcefiles.close_all;
  312. {$else UseBrowser}
  313. current_module^.sourcefiles.done;
  314. {$endif not UseBrowser}
  315. { restore scanner state }
  316. pattern:=oldpattern;
  317. token:=oldtoken;
  318. tokenpos:=oldtokenpos;
  319. orgpattern:=oldorgpattern;
  320. block_type:=old_block_type;
  321. { call donescanner before restoring preprocstack, because }
  322. { donescanner tests for a empty preprocstack }
  323. { and can also check for unused macros }
  324. donescanner(current_module^.compiled);
  325. dispose(macros,done);
  326. macros:=oldmacros;
  327. { restore scanner }
  328. preprocstack:=oldpreprocstack;
  329. inputbuffer:=oldinputbuffer;
  330. inputpointer:=oldinputpointer;
  331. s_point:=olds_point;
  332. c:=oldc;
  333. comment_level:=oldcomment_level;
  334. nextlabelnr:=oldnextlabelnr;
  335. parse_only:=oldparse_only;
  336. { restore asmlists }
  337. exprasmlist:=oldexprasmlist;
  338. datasegment:=olddatasegment;
  339. bsssegment:=oldbsssegment;
  340. codesegment:=oldcodesegment;
  341. consts:=oldconsts;
  342. debuglist:=olddebuglist;
  343. externals:=oldexternals;
  344. internals:=oldinternals;
  345. importssection:=oldimports;
  346. exportssection:=oldexports;
  347. resourcesection:=oldresource;
  348. { restore current state }
  349. aktswitches:=oldswitches;
  350. aktpackrecords:=oldpackrecords;
  351. aktoutputformat:=oldoutputformat;
  352. aktoptprocessor:=oldoptprocessor;
  353. aktasmmode:=oldasmmode;
  354. if (compile_level=1) then
  355. begin
  356. if (not AsmRes.Empty) then
  357. begin
  358. Message1(exec_i_closing_script,AsmRes.Fn);
  359. AsmRes.WriteToDisk;
  360. end;
  361. end;
  362. dec(compile_level);
  363. end;
  364. end.
  365. {
  366. $Log$
  367. Revision 1.23 1998-06-08 22:59:48 peter
  368. * smartlinking works for win32
  369. * some defines to exclude some compiler parts
  370. Revision 1.22 1998/06/05 17:47:28 peter
  371. * some better uses clauses
  372. Revision 1.21 1998/06/04 23:51:49 peter
  373. * m68k compiles
  374. + .def file creation moved to gendef.pas so it could also be used
  375. for win32
  376. Revision 1.20 1998/06/03 22:48:55 peter
  377. + wordbool,longbool
  378. * rename bis,von -> high,low
  379. * moved some systemunit loading/creating to psystem.pas
  380. Revision 1.19 1998/05/27 19:45:04 peter
  381. * symtable.pas splitted into includefiles
  382. * symtable adapted for $ifdef NEWPPU
  383. Revision 1.18 1998/05/23 01:21:15 peter
  384. + aktasmmode, aktoptprocessor, aktoutputformat
  385. + smartlink per module $SMARTLINK-/+ (like MMX) and moved to aktswitches
  386. + $LIBNAME to set the library name where the unit will be put in
  387. * splitted cgi386 a bit (codeseg to large for bp7)
  388. * nasm, tasm works again. nasm moved to ag386nsm.pas
  389. Revision 1.17 1998/05/20 09:42:34 pierre
  390. + UseTokenInfo now default
  391. * unit in interface uses and implementation uses gives error now
  392. * only one error for unknown symbol (uses lastsymknown boolean)
  393. the problem came from the label code !
  394. + first inlined procedures and function work
  395. (warning there might be allowed cases were the result is still wrong !!)
  396. * UseBrower updated gives a global list of all position of all used symbols
  397. with switch -gb
  398. Revision 1.16 1998/05/12 10:47:00 peter
  399. * moved printstatus to verb_def
  400. + V_Normal which is between V_Error and V_Warning and doesn't have a
  401. prefix like error: warning: and is included in V_Default
  402. * fixed some messages
  403. * first time parameter scan is only for -v and -T
  404. - removed old style messages
  405. Revision 1.15 1998/05/11 13:07:54 peter
  406. + $ifdef NEWPPU for the new ppuformat
  407. + $define GDB not longer required
  408. * removed all warnings and stripped some log comments
  409. * no findfirst/findnext anymore to remove smartlink *.o files
  410. Revision 1.14 1998/05/06 18:36:53 peter
  411. * tai_section extended with code,data,bss sections and enumerated type
  412. * ident 'compiled by FPC' moved to pmodules
  413. * small fix for smartlink
  414. Revision 1.13 1998/05/06 08:38:42 pierre
  415. * better position info with UseTokenInfo
  416. UseTokenInfo greatly simplified
  417. + added check for changed tree after first time firstpass
  418. (if we could remove all the cases were it happen
  419. we could skip all firstpass if firstpasscount > 1)
  420. Only with ExtDebug
  421. Revision 1.12 1998/05/04 17:54:28 peter
  422. + smartlinking works (only case jumptable left todo)
  423. * redesign of systems.pas to support assemblers and linkers
  424. + Unitname is now also in the PPU-file, increased version to 14
  425. Revision 1.11 1998/05/01 16:38:45 florian
  426. * handling of private and protected fixed
  427. + change_keywords_to_tp implemented to remove
  428. keywords which aren't supported by tp
  429. * break and continue are now symbols of the system unit
  430. + widestring, longstring and ansistring type released
  431. Revision 1.10 1998/05/01 07:43:56 florian
  432. + basics for rtti implemented
  433. + switch $m (generate rtti for published sections)
  434. Revision 1.9 1998/04/30 15:59:40 pierre
  435. * GDB works again better :
  436. correct type info in one pass
  437. + UseTokenInfo for better source position
  438. * fixed one remaining bug in scanner for line counts
  439. * several little fixes
  440. Revision 1.8 1998/04/29 10:33:55 pierre
  441. + added some code for ansistring (not complete nor working yet)
  442. * corrected operator overloading
  443. * corrected nasm output
  444. + started inline procedures
  445. + added starstarn : use ** for exponentiation (^ gave problems)
  446. + started UseTokenInfo cond to get accurate positions
  447. Revision 1.7 1998/04/27 23:10:28 peter
  448. + new scanner
  449. * $makelib -> if smartlink
  450. * small filename fixes pmodule.setfilename
  451. * moved import from files.pas -> import.pas
  452. Revision 1.6 1998/04/21 10:16:48 peter
  453. * patches from strasbourg
  454. * objects is not used anymore in the fpc compiled version
  455. Revision 1.5 1998/04/10 14:41:43 peter
  456. * removed some Hints
  457. * small speed optimization for AsmLn
  458. Revision 1.4 1998/04/08 16:58:03 pierre
  459. * several bugfixes
  460. ADD ADC and AND are also sign extended
  461. nasm output OK (program still crashes at end
  462. and creates wrong assembler files !!)
  463. procsym types sym in tdef removed !!
  464. Revision 1.3 1998/04/07 22:45:04 florian
  465. * bug0092, bug0115 and bug0121 fixed
  466. + packed object/class/array
  467. }