parser.pas 17 KB

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