psub.pas 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Florian Klaempfl, Daniel Mantione
  4. Does the parsing and codegeneration at subroutine level
  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. unit psub;
  19. {$i fpcdefs.inc}
  20. interface
  21. procedure compile_proc_body(make_global,parent_has_class:boolean);
  22. { reads the declaration blocks }
  23. procedure read_declarations(islibrary : boolean);
  24. { reads declarations in the interface part of a unit }
  25. procedure read_interface_declarations;
  26. implementation
  27. uses
  28. { common }
  29. cutils,cclasses,
  30. { global }
  31. globtype,globals,tokens,verbose,comphook,
  32. systems,
  33. { aasm }
  34. cpubase,cpuinfo,aasmbase,aasmtai,
  35. { symtable }
  36. symconst,symbase,symdef,symsym,symtype,symtable,defutil,
  37. paramgr,
  38. ppu,fmodule,
  39. { pass 1 }
  40. node,
  41. nbas,nld,
  42. pass_1,
  43. {$ifdef state_tracking}
  44. nstate,
  45. {$endif state_tracking}
  46. { pass 2 }
  47. {$ifndef NOPASS2}
  48. pass_2,
  49. {$endif}
  50. { parser }
  51. scanner,
  52. pbase,pstatmnt,pdecl,pdecsub,pexports,
  53. { codegen }
  54. tgobj,cgbase,rgobj,rgcpu,
  55. ncgutil
  56. {$ifndef NOOPT}
  57. {$ifdef i386}
  58. ,aopt386
  59. {$else i386}
  60. ,aoptcpu
  61. {$endif i386}
  62. {$endif}
  63. ;
  64. {****************************************************************************
  65. PROCEDURE/FUNCTION BODY PARSING
  66. ****************************************************************************}
  67. procedure initializevars(p:tnamedindexitem;arg:pointer);
  68. var
  69. b : tblocknode;
  70. begin
  71. if tsym(p).typ<>varsym then
  72. exit;
  73. with tvarsym(p) do
  74. begin
  75. if assigned(defaultconstsym) then
  76. begin
  77. b:=tblocknode(arg);
  78. b.left:=cstatementnode.create(
  79. cassignmentnode.create(
  80. cloadnode.create(tsym(p),tsym(p).owner),
  81. cloadnode.create(defaultconstsym,defaultconstsym.owner)),
  82. b.left);
  83. end;
  84. end;
  85. end;
  86. function block(islibrary : boolean) : tnode;
  87. var
  88. storepos : tfileposinfo;
  89. begin
  90. if not is_void(aktprocdef.rettype.def) then
  91. begin
  92. { if the current is a function aktprocsym is non nil }
  93. { and there is a local symtable set }
  94. storepos:=akttokenpos;
  95. akttokenpos:=aktprocsym.fileinfo;
  96. aktprocdef.funcretsym:=tfuncretsym.create(aktprocsym.name,aktprocdef.rettype);
  97. { insert in local symtable }
  98. symtablestack.insert(aktprocdef.funcretsym);
  99. symtablestack.insertvardata(aktprocdef.funcretsym);
  100. akttokenpos:=storepos;
  101. procinfo.set_result_offset;
  102. { insert result also if support is on }
  103. if (m_result in aktmodeswitches) then
  104. begin
  105. aktprocdef.resultfuncretsym:=tfuncretsym.create('RESULT',aktprocdef.rettype);
  106. symtablestack.insert(aktprocdef.resultfuncretsym);
  107. end;
  108. end;
  109. { parse const,types and vars }
  110. read_declarations(islibrary);
  111. procinfo.handle_body_start;
  112. { do we have an assembler block without the po_assembler?
  113. we should allow this for Delphi compatibility (PFV) }
  114. if (token=_ASM) and (m_delphi in aktmodeswitches) then
  115. include(aktprocdef.procoptions,po_assembler);
  116. { Handle assembler block different }
  117. if (po_assembler in aktprocdef.procoptions) then
  118. begin
  119. block:=assembler_block;
  120. exit;
  121. end;
  122. {Unit initialization?.}
  123. if (lexlevel=unit_init_level) and (current_module.is_unit)
  124. or islibrary then
  125. begin
  126. if (token=_END) then
  127. begin
  128. consume(_END);
  129. { We need at least a node, else the entry/exit code is not
  130. generated and thus no PASCALMAIN symbol which we need (PFV) }
  131. if islibrary then
  132. block:=cnothingnode.create
  133. else
  134. block:=nil;
  135. end
  136. else
  137. begin
  138. if token=_INITIALIZATION then
  139. begin
  140. { The library init code is already called and does not
  141. need to be in the initfinal table (PFV) }
  142. if not islibrary then
  143. current_module.flags:=current_module.flags or uf_init;
  144. block:=statement_block(_INITIALIZATION);
  145. end
  146. else if (token=_FINALIZATION) then
  147. begin
  148. if (current_module.flags and uf_finalize)<>0 then
  149. block:=statement_block(_FINALIZATION)
  150. else
  151. begin
  152. { can we allow no INITIALIZATION for DLL ??
  153. I think it should work PM }
  154. block:=nil;
  155. exit;
  156. end;
  157. end
  158. else
  159. begin
  160. { The library init code is already called and does not
  161. need to be in the initfinal table (PFV) }
  162. if not islibrary then
  163. current_module.flags:=current_module.flags or uf_init;
  164. block:=statement_block(_BEGIN);
  165. end;
  166. end;
  167. end
  168. else
  169. begin
  170. block:=statement_block(_BEGIN);
  171. if symtablestack.symtabletype=localsymtable then
  172. symtablestack.foreach_static({$ifdef FPCPROCVAR}@{$endif}initializevars,block);
  173. end;
  174. end;
  175. {****************************************************************************
  176. PROCEDURE/FUNCTION COMPILING
  177. ****************************************************************************}
  178. procedure compile_proc_body(make_global,parent_has_class:boolean);
  179. {
  180. Compile the body of a procedure
  181. }
  182. var
  183. oldexitlabel,oldexit2label : tasmlabel;
  184. oldfaillabel,oldquickexitlabel:tasmlabel;
  185. _class,hp:tobjectdef;
  186. { switches can change inside the procedure }
  187. entryswitches, exitswitches : tlocalswitches;
  188. oldaktmaxfpuregisters,localmaxfpuregisters : longint;
  189. { code for the subroutine as tree }
  190. code:tnode;
  191. { true when no stackframe is required }
  192. nostackframe:boolean;
  193. { number of bytes which have to be cleared by RET }
  194. parasize:longint;
  195. { filepositions }
  196. entrypos,
  197. savepos,
  198. exitpos : tfileposinfo;
  199. begin
  200. { calculate the lexical level }
  201. inc(lexlevel);
  202. if lexlevel>maxnesting then
  203. Message(parser_e_too_much_lexlevel);
  204. { static is also important for local procedures !! }
  205. if (po_staticmethod in aktprocdef.procoptions) then
  206. allow_only_static:=true
  207. else if (lexlevel=normal_function_level) then
  208. allow_only_static:=false;
  209. { save old labels }
  210. oldexitlabel:=aktexitlabel;
  211. oldexit2label:=aktexit2label;
  212. oldquickexitlabel:=quickexitlabel;
  213. oldfaillabel:=faillabel;
  214. { get new labels }
  215. objectlibrary.getlabel(aktexitlabel);
  216. objectlibrary.getlabel(aktexit2label);
  217. { exit for fail in constructors }
  218. if (aktprocdef.proctypeoption=potype_constructor) then
  219. begin
  220. objectlibrary.getlabel(faillabel);
  221. objectlibrary.getlabel(quickexitlabel);
  222. end;
  223. { reset break and continue labels }
  224. block_type:=bt_general;
  225. aktbreaklabel:=nil;
  226. aktcontinuelabel:=nil;
  227. {$ifdef state_tracking}
  228. { aktstate:=Tstate_storage.create;}
  229. {$endif state_tracking}
  230. { insert symtables for the class, by only if it is no nested function }
  231. if assigned(procinfo._class) and not(parent_has_class) then
  232. begin
  233. { insert them in the reverse order ! }
  234. hp:=nil;
  235. repeat
  236. _class:=procinfo._class;
  237. while _class.childof<>hp do
  238. _class:=_class.childof;
  239. hp:=_class;
  240. _class.symtable.next:=symtablestack;
  241. symtablestack:=_class.symtable;
  242. until hp=procinfo._class;
  243. end;
  244. { insert parasymtable in symtablestack}
  245. { only if lexlevel > 1 !!! global symtable should be right after staticsymtazble
  246. for checking of same names used in interface and implementation !! }
  247. if lexlevel>=normal_function_level then
  248. begin
  249. aktprocdef.parast.next:=symtablestack;
  250. symtablestack:=aktprocdef.parast;
  251. symtablestack.symtablelevel:=lexlevel;
  252. end;
  253. { create a local symbol table for this routine }
  254. if not assigned(aktprocdef.localst) then
  255. aktprocdef.insert_localst;
  256. { insert localsymtable in symtablestack}
  257. aktprocdef.localst.next:=symtablestack;
  258. symtablestack:=aktprocdef.localst;
  259. symtablestack.symtablelevel:=lexlevel;
  260. { constant symbols are inserted in this symboltable }
  261. constsymtable:=symtablestack;
  262. { reset the temporary memory }
  263. rg.cleartempgen;
  264. rg.usedinproc:=[];
  265. rg.usedbyproc:=[];
  266. { save entry info }
  267. entrypos:=aktfilepos;
  268. entryswitches:=aktlocalswitches;
  269. localmaxfpuregisters:=aktmaxfpuregisters;
  270. { parse the code ... }
  271. code:=block(current_module.islibrary);
  272. { store a copy of the original tree for inline, for
  273. normal procedures only store a reference to the
  274. current tree }
  275. if (aktprocdef.proccalloption=pocall_inline) then
  276. aktprocdef.code:=code.getcopy
  277. else
  278. aktprocdef.code:=code;
  279. { get a better entry point }
  280. if assigned(code) then
  281. entrypos:=code.fileinfo;
  282. { save exit info }
  283. exitswitches:=aktlocalswitches;
  284. exitpos:=last_endtoken_filepos;
  285. { save current filepos }
  286. savepos:=aktfilepos;
  287. {When we are called to compile the body of a unit, aktprocsym should
  288. point to the unit initialization. If the unit has no initialization,
  289. aktprocsym=nil. But in that case code=nil. hus we should check for
  290. code=nil, when we use aktprocsym.}
  291. { set the start offset to the start of the temp area in the stack }
  292. tg.setfirsttemp(procinfo.firsttemp_offset);
  293. { ... and generate assembler }
  294. { but set the right switches for entry !! }
  295. aktlocalswitches:=entryswitches;
  296. oldaktmaxfpuregisters:=aktmaxfpuregisters;
  297. aktmaxfpuregisters:=localmaxfpuregisters;
  298. if assigned(code) then
  299. begin
  300. { the procedure is now defined }
  301. aktprocdef.forwarddef:=false;
  302. { only generate the code if no type errors are found, else
  303. finish at least the type checking pass }
  304. {$ifndef NOPASS2}
  305. if (status.errorcount=0) then
  306. begin
  307. generatecode(code);
  308. { first generate entry code with the correct position and switches }
  309. aktfilepos:=entrypos;
  310. aktlocalswitches:=entryswitches;
  311. genentrycode(procinfo.aktentrycode,make_global,0,parasize,nostackframe,false);
  312. { FPC_POPADDRSTACK destroys all registers (JM) }
  313. if (procinfo.flags and (pi_needs_implicit_finally or pi_uses_exceptions)) <> 0 then
  314. begin
  315. rg.usedinproc := ALL_REGISTERS;
  316. end;
  317. { now generate exit code with the correct position and switches }
  318. aktfilepos:=exitpos;
  319. aktlocalswitches:=exitswitches;
  320. genexitcode(procinfo.aktexitcode,parasize,nostackframe,false);
  321. { now all the registers used are known }
  322. aktprocdef.usedregisters:=rg.usedinproc;
  323. procinfo.aktproccode.insertlist(procinfo.aktentrycode);
  324. procinfo.aktproccode.concatlist(procinfo.aktexitcode);
  325. {$ifndef NoOpt}
  326. if (cs_optimize in aktglobalswitches) and
  327. { do not optimize pure assembler procedures }
  328. ((procinfo.flags and pi_is_assembler)=0) then
  329. Optimize(procinfo.aktproccode);
  330. {$endif NoOpt}
  331. { save local data (casetable) also in the same file }
  332. if assigned(procinfo.aktlocaldata) and
  333. (not procinfo.aktlocaldata.empty) then
  334. begin
  335. procinfo.aktproccode.concat(Tai_section.Create(sec_data));
  336. procinfo.aktproccode.concatlist(procinfo.aktlocaldata);
  337. procinfo.aktproccode.concat(Tai_section.Create(sec_code));
  338. end;
  339. { add the procedure to the codesegment }
  340. if (cs_create_smart in aktmoduleswitches) then
  341. codeSegment.concat(Tai_cut.Create);
  342. codeSegment.concatlist(procinfo.aktproccode);
  343. end
  344. else
  345. do_resulttypepass(code);
  346. {$else NOPASS2}
  347. do_resulttypepass(code);
  348. {$endif NOPASS2}
  349. end;
  350. { ... remove symbol tables }
  351. if lexlevel>=normal_function_level then
  352. symtablestack:=symtablestack.next.next
  353. else
  354. symtablestack:=symtablestack.next;
  355. { ... check for unused symbols }
  356. { but only if there is no asm block }
  357. if assigned(code) then
  358. begin
  359. if (Errorcount=0) then
  360. begin
  361. { check if forwards are resolved }
  362. tstoredsymtable(aktprocdef.localst).check_forwards;
  363. { check if all labels are used }
  364. tstoredsymtable(aktprocdef.localst).checklabels;
  365. { remove cross unit overloads }
  366. tstoredsymtable(aktprocdef.localst).unchain_overloaded;
  367. end;
  368. if (procinfo.flags and pi_uses_asm)=0 then
  369. begin
  370. { not for unit init, becuase the var can be used in finalize,
  371. it will be done in proc_unit }
  372. if not(aktprocdef.proctypeoption
  373. in [potype_proginit,potype_unitinit,potype_unitfinalize]) then
  374. tstoredsymtable(aktprocdef.localst).allsymbolsused;
  375. tstoredsymtable(aktprocdef.parast).allsymbolsused;
  376. end;
  377. end;
  378. { the local symtables can be deleted, but the parast }
  379. { doesn't, (checking definitons when calling a }
  380. { function }
  381. { not for a inline procedure !! (PM) }
  382. { at lexlevel = 1 localst is the staticsymtable itself }
  383. { so no dispose here !! }
  384. if assigned(code) and
  385. not(cs_browser in aktmoduleswitches) and
  386. (aktprocdef.proccalloption<>pocall_inline) then
  387. begin
  388. if lexlevel>=normal_function_level then
  389. begin
  390. aktprocdef.localst.free;
  391. end;
  392. aktprocdef.localst:=nil;
  393. end;
  394. { all registers can be used again }
  395. rg.resetusableregisters;
  396. { only now we can remove the temps }
  397. tg.resettempgen;
  398. { remove code tree, if not inline procedure }
  399. if assigned(code) then
  400. begin
  401. { the inline procedure has already got a copy of the tree
  402. stored in aktprocdef.code }
  403. code.free;
  404. if (aktprocdef.proccalloption<>pocall_inline) then
  405. aktprocdef.code:=nil;
  406. end;
  407. { remove class member symbol tables }
  408. while symtablestack.symtabletype=objectsymtable do
  409. symtablestack:=symtablestack.next;
  410. aktmaxfpuregisters:=oldaktmaxfpuregisters;
  411. {$ifdef state_tracking}
  412. { aktstate.destroy;}
  413. {$endif state_tracking}
  414. { restore filepos, the switches are already set }
  415. aktfilepos:=savepos;
  416. { restore labels }
  417. aktexitlabel:=oldexitlabel;
  418. aktexit2label:=oldexit2label;
  419. quickexitlabel:=oldquickexitlabel;
  420. faillabel:=oldfaillabel;
  421. { reset to normal non static function }
  422. if (lexlevel=normal_function_level) then
  423. allow_only_static:=false;
  424. { previous lexlevel }
  425. dec(lexlevel);
  426. end;
  427. {****************************************************************************
  428. PROCEDURE/FUNCTION PARSING
  429. ****************************************************************************}
  430. procedure checkvaluepara(p:tnamedindexitem;arg:pointer);
  431. var
  432. vs : tvarsym;
  433. s : string;
  434. begin
  435. if tsym(p).typ<>varsym then
  436. exit;
  437. with tvarsym(p) do
  438. begin
  439. if copy(name,1,3)='val' then
  440. begin
  441. s:=Copy(name,4,255);
  442. if not(po_assembler in aktprocdef.procoptions) then
  443. begin
  444. vs:=tvarsym.create(s,vartype);
  445. vs.fileinfo:=fileinfo;
  446. vs.varspez:=varspez;
  447. if not assigned(aktprocdef.localst) then
  448. aktprocdef.insert_localst;
  449. aktprocdef.localst.insert(vs);
  450. aktprocdef.localst.insertvardata(vs);
  451. include(vs.varoptions,vo_is_local_copy);
  452. vs.varstate:=vs_assigned;
  453. localvarsym:=vs;
  454. inc(refs); { the para was used to set the local copy ! }
  455. { warnings only on local copy ! }
  456. varstate:=vs_used;
  457. end
  458. else
  459. begin
  460. aktprocdef.parast.rename(name,s);
  461. end;
  462. end;
  463. end;
  464. end;
  465. procedure read_proc;
  466. {
  467. Parses the procedure directives, then parses the procedure body, then
  468. generates the code for it
  469. }
  470. var
  471. oldprocsym : tprocsym;
  472. oldprocdef : tprocdef;
  473. oldprocinfo : tprocinfo;
  474. oldconstsymtable : tsymtable;
  475. oldfilepos : tfileposinfo;
  476. pdflags : word;
  477. begin
  478. { save old state }
  479. oldprocdef:=aktprocdef;
  480. oldprocsym:=aktprocsym;
  481. oldconstsymtable:=constsymtable;
  482. oldprocinfo:=procinfo;
  483. { create a new procedure }
  484. codegen_newprocedure;
  485. with procinfo do
  486. begin
  487. parent:=oldprocinfo;
  488. { clear flags }
  489. flags:=0;
  490. { standard frame pointer }
  491. framepointer:=frame_pointer_reg;
  492. { is this a nested function of a method ? }
  493. if assigned(oldprocinfo) then
  494. _class:=oldprocinfo._class;
  495. end;
  496. parse_proc_dec;
  497. procinfo.procdef:=aktprocdef;
  498. { set the default function options }
  499. if parse_only then
  500. begin
  501. aktprocdef.forwarddef:=true;
  502. { set also the interface flag, for better error message when the
  503. implementation doesn't much this header }
  504. aktprocdef.interfacedef:=true;
  505. pdflags:=pd_interface;
  506. end
  507. else
  508. begin
  509. pdflags:=pd_body;
  510. if (not current_module.in_interface) then
  511. pdflags:=pdflags or pd_implemen;
  512. if (not current_module.is_unit) or (cs_create_smart in aktmoduleswitches) then
  513. pdflags:=pdflags or pd_global;
  514. procinfo.exported:=false;
  515. aktprocdef.forwarddef:=false;
  516. end;
  517. { parse the directives that may follow }
  518. inc(lexlevel);
  519. parse_proc_directives(pdflags);
  520. dec(lexlevel);
  521. { hint directives, these can be separated by semicolons here,
  522. that need to be handled here with a loop (PFV) }
  523. while try_consume_hintdirective(aktprocsym.symoptions) do
  524. Consume(_SEMICOLON);
  525. { set aktfilepos to the beginning of the function declaration }
  526. oldfilepos:=aktfilepos;
  527. aktfilepos:=aktprocdef.fileinfo;
  528. { For varargs directive also cdecl and external must be defined }
  529. if (po_varargs in aktprocdef.procoptions) then
  530. begin
  531. { check first for external in the interface, if available there
  532. then the cdecl must also be there since there is no implementation
  533. available to contain it }
  534. if parse_only then
  535. begin
  536. { if external is available, then cdecl must also be available }
  537. if (po_external in aktprocdef.procoptions) and
  538. not(aktprocdef.proccalloption in [pocall_cdecl,pocall_cppdecl]) then
  539. Message(parser_e_varargs_need_cdecl_and_external);
  540. end
  541. else
  542. begin
  543. { both must be defined now }
  544. if not(po_external in aktprocdef.procoptions) or
  545. not(aktprocdef.proccalloption in [pocall_cdecl,pocall_cppdecl]) then
  546. Message(parser_e_varargs_need_cdecl_and_external);
  547. end;
  548. end;
  549. { search for forward declarations }
  550. if not proc_add_definition(aktprocsym,aktprocdef) then
  551. begin
  552. { A method must be forward defined (in the object declaration) }
  553. if assigned(procinfo._class) and
  554. (not assigned(oldprocinfo._class)) then
  555. begin
  556. Message1(parser_e_header_dont_match_any_member,aktprocdef.fullprocname);
  557. aktprocsym.write_parameter_lists(aktprocdef);
  558. end
  559. else
  560. begin
  561. { Give a better error if there is a forward def in the interface and only
  562. a single implementation }
  563. if (not aktprocdef.forwarddef) and
  564. (aktprocsym.procdef_count>1) and
  565. aktprocsym.first_procdef.forwarddef and
  566. aktprocsym.first_procdef.interfacedef and
  567. not(aktprocsym.procdef_count>2) then
  568. begin
  569. Message1(parser_e_header_dont_match_forward,aktprocdef.fullprocname);
  570. aktprocsym.write_parameter_lists(aktprocdef);
  571. end
  572. else
  573. begin
  574. { check the global flag, for delphi this is not
  575. required }
  576. if not(m_delphi in aktmodeswitches) and
  577. ((procinfo.flags and pi_is_global)<>0) then
  578. Message(parser_e_overloaded_must_be_all_global);
  579. end;
  580. end;
  581. end;
  582. { update procinfo, because the aktprocdef can be
  583. changed by check_identical_proc (PFV) }
  584. procinfo.procdef:=aktprocdef;
  585. {$ifdef i386}
  586. { add implicit pushes for interrupt routines }
  587. if (po_interrupt in aktprocdef.procoptions) then
  588. procinfo.allocate_interrupt_stackframe;
  589. {$endif i386}
  590. { pointer to the return value ? }
  591. if paramanager.ret_in_param(aktprocdef.rettype.def,aktprocdef.proccalloption)
  592. {$ifdef m68k}
  593. and not (aktprocdef.proccalloption in [pocall_cdecl])
  594. {$endif m68k}
  595. then
  596. begin
  597. procinfo.return_offset:=procinfo.para_offset;
  598. inc(procinfo.para_offset,pointer_size);
  599. end;
  600. { allows to access the parameters of main functions in nested functions }
  601. aktprocdef.parast.address_fixup:=procinfo.para_offset;
  602. { when it is a value para and it needs a local copy then rename
  603. the parameter and insert a copy in the localst. This is not done
  604. for assembler procedures }
  605. if (not parse_only) and (not aktprocdef.forwarddef) then
  606. aktprocdef.parast.foreach_static({$ifdef FPCPROCVAR}@{$endif}checkvaluepara,nil);
  607. procinfo.after_header;
  608. { restore file pos }
  609. aktfilepos:=oldfilepos;
  610. { compile procedure when a body is needed }
  611. if (pdflags and pd_body)<>0 then
  612. begin
  613. Message1(parser_p_procedure_start,
  614. aktprocdef.fullprocname);
  615. if assigned(aktprocsym.owner) then
  616. aktprocdef.aliasnames.insert(aktprocdef.mangledname);
  617. { set _FAIL as keyword if constructor }
  618. if (aktprocdef.proctypeoption=potype_constructor) then
  619. tokeninfo^[_FAIL].keyword:=m_all;
  620. if assigned(aktprocdef._class) then
  621. tokeninfo^[_SELF].keyword:=m_all;
  622. compile_proc_body(((pdflags and pd_global)<>0),assigned(oldprocinfo._class));
  623. { reset _FAIL as normal }
  624. if (aktprocdef.proctypeoption=potype_constructor) then
  625. tokeninfo^[_FAIL].keyword:=m_none;
  626. if assigned(aktprocdef._class) and (lexlevel=main_program_level) then
  627. tokeninfo^[_SELF].keyword:=m_none;
  628. consume(_SEMICOLON);
  629. end;
  630. { close }
  631. codegen_doneprocedure;
  632. { Restore old state }
  633. constsymtable:=oldconstsymtable;
  634. {$ifdef notused}
  635. { restore the interface order to maintain CRC values PM }
  636. if assigned(prevdef) and assigned(aktprocdef.nextoverloaded) then
  637. begin
  638. stdef:=aktprocdef;
  639. aktprocdef:=stdef.nextoverloaded;
  640. stdef.nextoverloaded:=prevdef.nextoverloaded;
  641. prevdef.nextoverloaded:=stdef;
  642. end;
  643. {$endif notused}
  644. { release procsym when it was not stored in the symtable }
  645. if not assigned(aktprocsym.owner) then
  646. begin
  647. aktprocsym.free;
  648. aktprocdef.procsym:=nil;
  649. end;
  650. aktprocsym:=oldprocsym;
  651. aktprocdef:=oldprocdef;
  652. procinfo:=oldprocinfo;
  653. otsym:=nil;
  654. end;
  655. {****************************************************************************
  656. DECLARATION PARSING
  657. ****************************************************************************}
  658. { search in symtablestack for not complete classes }
  659. procedure check_forward_class(p : tnamedindexitem;arg:pointer);
  660. begin
  661. if (tsym(p).typ=typesym) and
  662. (ttypesym(p).restype.def.deftype=objectdef) and
  663. (oo_is_forward in tobjectdef(ttypesym(p).restype.def).objectoptions) then
  664. MessagePos1(tsym(p).fileinfo,sym_e_forward_type_not_resolved,tsym(p).realname);
  665. end;
  666. procedure read_declarations(islibrary : boolean);
  667. procedure Not_supported_for_inline(t : ttoken);
  668. begin
  669. if assigned(aktprocsym) and
  670. (aktprocdef.proccalloption=pocall_inline) then
  671. Begin
  672. Message1(parser_w_not_supported_for_inline,tokenstring(t));
  673. Message(parser_w_inlining_disabled);
  674. aktprocdef.proccalloption:=pocall_fpccall;
  675. End;
  676. end;
  677. begin
  678. repeat
  679. case token of
  680. _LABEL:
  681. begin
  682. Not_supported_for_inline(token);
  683. label_dec;
  684. end;
  685. _CONST:
  686. begin
  687. Not_supported_for_inline(token);
  688. const_dec;
  689. end;
  690. _TYPE:
  691. begin
  692. Not_supported_for_inline(token);
  693. type_dec;
  694. end;
  695. _VAR:
  696. var_dec;
  697. _THREADVAR:
  698. threadvar_dec;
  699. _CONSTRUCTOR,_DESTRUCTOR,
  700. _FUNCTION,_PROCEDURE,_OPERATOR,_CLASS:
  701. begin
  702. Not_supported_for_inline(token);
  703. read_proc;
  704. end;
  705. _RESOURCESTRING:
  706. resourcestring_dec;
  707. _EXPORTS:
  708. begin
  709. Not_supported_for_inline(token);
  710. { here we should be at lexlevel 1, no ? PM }
  711. if (lexlevel<>main_program_level) or
  712. (current_module.is_unit) then
  713. begin
  714. Message(parser_e_syntax_error);
  715. consume_all_until(_SEMICOLON);
  716. end
  717. else if islibrary or (target_info.system in [system_i386_WIN32,system_i386_wdosx,system_i386_Netware])
  718. then // AD
  719. read_exports;
  720. end
  721. else break;
  722. end;
  723. until false;
  724. { check for incomplete class definitions, this is only required
  725. for fpc modes }
  726. if (m_fpc in aktmodeswitches) then
  727. symtablestack.foreach_static({$ifdef FPCPROCVAR}@{$endif}check_forward_class,nil);
  728. end;
  729. procedure read_interface_declarations;
  730. begin
  731. {Since the body is now parsed at lexlevel 1, and the declarations
  732. must be parsed at the same lexlevel we increase the lexlevel.}
  733. inc(lexlevel);
  734. repeat
  735. case token of
  736. _CONST :
  737. const_dec;
  738. _TYPE :
  739. type_dec;
  740. _VAR :
  741. var_dec;
  742. _THREADVAR :
  743. threadvar_dec;
  744. _RESOURCESTRING:
  745. resourcestring_dec;
  746. _FUNCTION,
  747. _PROCEDURE,
  748. _OPERATOR :
  749. read_proc;
  750. else
  751. break;
  752. end;
  753. until false;
  754. dec(lexlevel);
  755. { check for incomplete class definitions, this is only required
  756. for fpc modes }
  757. if (m_fpc in aktmodeswitches) then
  758. symtablestack.foreach_static({$ifdef FPCPROCVAR}@{$endif}check_forward_class,nil);
  759. end;
  760. end.
  761. {
  762. $Log$
  763. Revision 1.86 2003-01-02 11:14:02 michael
  764. + Patch from peter to support initial values for local variables
  765. Revision 1.85 2002/12/29 18:59:34 peter
  766. * fixed parsing of declarations before asm statement
  767. Revision 1.84 2002/12/29 18:25:18 peter
  768. * parse declarations before check _ASM token
  769. Revision 1.83 2002/12/29 14:57:50 peter
  770. * unit loading changed to first register units and load them
  771. afterwards. This is needed to support uses xxx in yyy correctly
  772. * unit dependency check fixed
  773. Revision 1.82 2002/12/25 01:26:56 peter
  774. * duplicate procsym-unitsym fix
  775. Revision 1.81 2002/12/15 13:37:15 peter
  776. * don't include uf_init for library. The code is already called and
  777. does not need to be in the initfinal table
  778. Revision 1.80 2002/12/07 14:27:09 carl
  779. * 3% memory optimization
  780. * changed some types
  781. + added type checking with different size for call node and for
  782. parameters
  783. Revision 1.79 2002/11/25 18:43:32 carl
  784. - removed the invalid if <> checking (Delphi is strange on this)
  785. + implemented abstract warning on instance creation of class with
  786. abstract methods.
  787. * some error message cleanups
  788. Revision 1.78 2002/11/25 17:43:23 peter
  789. * splitted defbase in defutil,symutil,defcmp
  790. * merged isconvertable and is_equal into compare_defs(_ext)
  791. * made operator search faster by walking the list only once
  792. Revision 1.77 2002/11/23 22:50:06 carl
  793. * some small speed optimizations
  794. + added several new warnings/hints
  795. Revision 1.76 2002/11/18 17:31:58 peter
  796. * pass proccalloption to ret_in_xxx and push_xxx functions
  797. Revision 1.75 2002/11/17 16:31:57 carl
  798. * memory optimization (3-4%) : cleanup of tai fields,
  799. cleanup of tdef and tsym fields.
  800. * make it work for m68k
  801. Revision 1.74 2002/11/15 01:58:53 peter
  802. * merged changes from 1.0.7 up to 04-11
  803. - -V option for generating bug report tracing
  804. - more tracing for option parsing
  805. - errors for cdecl and high()
  806. - win32 import stabs
  807. - win32 records<=8 are returned in eax:edx (turned off by default)
  808. - heaptrc update
  809. - more info for temp management in .s file with EXTDEBUG
  810. Revision 1.73 2002/11/09 15:32:30 carl
  811. * noopt for non-i386 targets
  812. Revision 1.72 2002/09/10 20:31:48 florian
  813. * call to procinfo.after_header added
  814. Revision 1.71 2002/09/07 15:25:07 peter
  815. * old logs removed and tabs fixed
  816. Revision 1.70 2002/09/03 16:26:27 daniel
  817. * Make Tprocdef.defs protected
  818. Revision 1.69 2002/08/25 19:25:20 peter
  819. * sym.insert_in_data removed
  820. * symtable.insertvardata/insertconstdata added
  821. * removed insert_in_data call from symtable.insert, it needs to be
  822. called separatly. This allows to deref the address calculation
  823. * procedures now calculate the parast addresses after the procedure
  824. directives are parsed. This fixes the cdecl parast problem
  825. * push_addr_param has an extra argument that specifies if cdecl is used
  826. or not
  827. Revision 1.68 2002/08/17 09:23:41 florian
  828. * first part of procinfo rewrite
  829. Revision 1.67 2002/08/16 14:24:59 carl
  830. * issameref() to test if two references are the same (then emit no opcodes)
  831. + ret_in_reg to replace ret_in_acc
  832. (fix some register allocation bugs at the same time)
  833. + save_std_register now has an extra parameter which is the
  834. usedinproc registers
  835. Revision 1.66 2002/08/11 14:32:27 peter
  836. * renamed current_library to objectlibrary
  837. Revision 1.65 2002/08/11 13:24:13 peter
  838. * saving of asmsymbols in ppu supported
  839. * asmsymbollist global is removed and moved into a new class
  840. tasmlibrarydata that will hold the info of a .a file which
  841. corresponds with a single module. Added librarydata to tmodule
  842. to keep the library info stored for the module. In the future the
  843. objectfiles will also be stored to the tasmlibrarydata class
  844. * all getlabel/newasmsymbol and friends are moved to the new class
  845. Revision 1.64 2002/08/09 19:14:28 carl
  846. * fixed stackframe parameter (should only contain local size),
  847. set to zero currently
  848. Revision 1.63 2002/08/06 20:55:22 florian
  849. * first part of ppc calling conventions fix
  850. Revision 1.62 2002/07/26 21:15:41 florian
  851. * rewrote the system handling
  852. Revision 1.61 2002/07/20 11:57:56 florian
  853. * types.pas renamed to defbase.pas because D6 contains a types
  854. unit so this would conflicts if D6 programms are compiled
  855. + Willamette/SSE2 instructions to assembler added
  856. Revision 1.60 2002/07/19 11:41:36 daniel
  857. * State tracker work
  858. * The whilen and repeatn are now completely unified into whilerepeatn. This
  859. allows the state tracker to change while nodes automatically into
  860. repeat nodes.
  861. * Resulttypepass improvements to the notn. 'not not a' is optimized away and
  862. 'not(a>b)' is optimized into 'a<=b'.
  863. * Resulttypepass improvements to the whilerepeatn. 'while not a' is optimized
  864. by removing the notn and later switchting the true and falselabels. The
  865. same is done with 'repeat until not a'.
  866. Revision 1.59 2002/07/15 18:03:15 florian
  867. * readded removed changes
  868. Revision 1.57 2002/07/11 14:41:28 florian
  869. * start of the new generic parameter handling
  870. Revision 1.58 2002/07/14 18:00:44 daniel
  871. + Added the beginning of a state tracker. This will track the values of
  872. variables through procedures and optimize things away.
  873. Revision 1.56 2002/07/07 09:52:32 florian
  874. * powerpc target fixed, very simple units can be compiled
  875. * some basic stuff for better callparanode handling, far from being finished
  876. Revision 1.55 2002/07/04 20:43:01 florian
  877. * first x86-64 patches
  878. Revision 1.54 2002/07/01 18:46:25 peter
  879. * internal linker
  880. * reorganized aasm layer
  881. Revision 1.53 2002/05/18 13:34:14 peter
  882. * readded missing revisions
  883. Revision 1.52 2002/05/16 19:46:44 carl
  884. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  885. + try to fix temp allocation (still in ifdef)
  886. + generic constructor calls
  887. + start of tassembler / tmodulebase class cleanup
  888. Revision 1.51 2002/05/14 19:34:49 peter
  889. * removed old logs and updated copyright year
  890. }