psub.pas 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 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 defines.inc}
  20. {$ifdef powerpc}
  21. {$define newcg}
  22. {$endif powerpc}
  23. interface
  24. procedure compile_proc_body(make_global,parent_has_class:boolean);
  25. { reads the declaration blocks }
  26. procedure read_declarations(islibrary : boolean);
  27. { reads declarations in the interface part of a unit }
  28. procedure read_interface_declarations;
  29. implementation
  30. uses
  31. { common }
  32. cutils,cclasses,
  33. { global }
  34. globtype,globals,tokens,verbose,comphook,
  35. systems,
  36. { aasm }
  37. cpubase,aasm,
  38. { symtable }
  39. symconst,symbase,symdef,symsym,symtype,symtable,types,
  40. ppu,fmodule,
  41. { pass 1 }
  42. node,
  43. nbas,
  44. { pass 2 }
  45. {$ifndef NOPASS2}
  46. pass_1,pass_2,
  47. {$endif}
  48. { parser }
  49. scanner,
  50. pbase,pstatmnt,pdecl,pdecsub,pexports,
  51. { codegen }
  52. tgcpu,cgbase,
  53. temp_gen,
  54. cga
  55. {$ifndef NOOPT}
  56. {$ifdef i386}
  57. ,aopt386
  58. {$else i386}
  59. ,aoptcpu
  60. {$endif i386}
  61. {$endif}
  62. {$ifdef newcg}
  63. ,cgobj
  64. {$endif newcg}
  65. ;
  66. {****************************************************************************
  67. PROCEDURE/FUNCTION BODY PARSING
  68. ****************************************************************************}
  69. function block(islibrary : boolean) : tnode;
  70. var
  71. storepos : tfileposinfo;
  72. begin
  73. { do we have an assembler block without the po_assembler?
  74. we should allow this for Delphi compatibility (PFV) }
  75. if (token=_ASM) and (m_delphi in aktmodeswitches) then
  76. include(aktprocdef.procoptions,po_assembler);
  77. { Handle assembler block different }
  78. if (po_assembler in aktprocdef.procoptions) then
  79. begin
  80. read_declarations(false);
  81. block:=assembler_block;
  82. exit;
  83. end;
  84. if not is_void(aktprocdef.rettype.def) then
  85. begin
  86. { if the current is a function aktprocsym is non nil }
  87. { and there is a local symtable set }
  88. storepos:=akttokenpos;
  89. akttokenpos:=aktprocsym.fileinfo;
  90. aktprocdef.funcretsym:=tfuncretsym.create(aktprocsym.name,aktprocdef.rettype);
  91. { insert in local symtable }
  92. symtablestack.insert(aktprocdef.funcretsym);
  93. akttokenpos:=storepos;
  94. if ret_in_acc(aktprocdef.rettype.def) or
  95. (aktprocdef.rettype.def.deftype=floatdef) then
  96. procinfo^.return_offset:=-tfuncretsym(aktprocdef.funcretsym).address;
  97. { insert result also if support is on }
  98. if (m_result in aktmodeswitches) then
  99. begin
  100. aktprocdef.resultfuncretsym:=tfuncretsym.create('RESULT',aktprocdef.rettype);
  101. symtablestack.insert(aktprocdef.resultfuncretsym);
  102. end;
  103. end;
  104. read_declarations(islibrary);
  105. { temporary space is set, while the BEGIN of the procedure }
  106. if (symtablestack.symtabletype=localsymtable) then
  107. procinfo^.firsttemp_offset := -symtablestack.datasize
  108. else
  109. procinfo^.firsttemp_offset := 0;
  110. { space for the return value }
  111. { !!!!! this means that we can not set the return value
  112. in a subfunction !!!!! }
  113. { because we don't know yet where the address is }
  114. if not is_void(aktprocdef.rettype.def) then
  115. begin
  116. if ret_in_acc(aktprocdef.rettype.def) or (aktprocdef.rettype.def.deftype=floatdef) then
  117. begin
  118. { the space has been set in the local symtable }
  119. procinfo^.return_offset:=-tfuncretsym(aktprocdef.funcretsym).address;
  120. if ((procinfo^.flags and pi_operator)<>0) and
  121. assigned(otsym) then
  122. otsym.address:=-procinfo^.return_offset;
  123. { eax is modified by a function }
  124. {$ifndef newcg}
  125. {$ifdef i386}
  126. usedinproc:=usedinproc or ($80 shr byte(R_EAX));
  127. if is_64bitint(aktprocdef.rettype.def) then
  128. usedinproc:=usedinproc or ($80 shr byte(R_EDX))
  129. {$endif}
  130. {$ifdef m68k}
  131. usedinproc:=usedinproc + [accumulator];
  132. if is_64bitint(aktprocdef.rettype.def) then
  133. usedinproc:=usedinproc + [scratch_reg];
  134. {$endif}
  135. {$endif newcg}
  136. end;
  137. end;
  138. {Unit initialization?.}
  139. if (lexlevel=unit_init_level) and (current_module.is_unit)
  140. or islibrary then
  141. begin
  142. if (token=_END) then
  143. begin
  144. consume(_END);
  145. { We need at least a node, else the entry/exit code is not
  146. generated and thus no PASCALMAIN symbol which we need (PFV) }
  147. if islibrary then
  148. block:=cnothingnode.create
  149. else
  150. block:=nil;
  151. end
  152. else
  153. begin
  154. if token=_INITIALIZATION then
  155. begin
  156. current_module.flags:=current_module.flags or uf_init;
  157. block:=statement_block(_INITIALIZATION);
  158. end
  159. else if (token=_FINALIZATION) then
  160. begin
  161. if (current_module.flags and uf_finalize)<>0 then
  162. block:=statement_block(_FINALIZATION)
  163. else
  164. begin
  165. { can we allow no INITIALIZATION for DLL ??
  166. I think it should work PM }
  167. block:=nil;
  168. exit;
  169. end;
  170. end
  171. else
  172. begin
  173. current_module.flags:=current_module.flags or uf_init;
  174. block:=statement_block(_BEGIN);
  175. end;
  176. end;
  177. end
  178. else
  179. block:=statement_block(_BEGIN);
  180. end;
  181. {****************************************************************************
  182. PROCEDURE/FUNCTION COMPILING
  183. ****************************************************************************}
  184. procedure compile_proc_body(make_global,parent_has_class:boolean);
  185. {
  186. Compile the body of a procedure
  187. }
  188. var
  189. oldexitlabel,oldexit2label : tasmlabel;
  190. oldfaillabel,oldquickexitlabel:tasmlabel;
  191. _class,hp:tobjectdef;
  192. { switches can change inside the procedure }
  193. entryswitches, exitswitches : tlocalswitches;
  194. oldaktmaxfpuregisters,localmaxfpuregisters : longint;
  195. { code for the subroutine as tree }
  196. code:tnode;
  197. { size of the local strackframe }
  198. stackframe:longint;
  199. { true when no stackframe is required }
  200. nostackframe:boolean;
  201. { number of bytes which have to be cleared by RET }
  202. parasize:longint;
  203. { filepositions }
  204. entrypos,
  205. savepos,
  206. exitpos : tfileposinfo;
  207. begin
  208. { calculate the lexical level }
  209. inc(lexlevel);
  210. if lexlevel>32 then
  211. Message(parser_e_too_much_lexlevel);
  212. { static is also important for local procedures !! }
  213. if (po_staticmethod in aktprocdef.procoptions) then
  214. allow_only_static:=true
  215. else if (lexlevel=normal_function_level) then
  216. allow_only_static:=false;
  217. { save old labels }
  218. oldexitlabel:=aktexitlabel;
  219. oldexit2label:=aktexit2label;
  220. oldquickexitlabel:=quickexitlabel;
  221. oldfaillabel:=faillabel;
  222. { get new labels }
  223. getlabel(aktexitlabel);
  224. getlabel(aktexit2label);
  225. { exit for fail in constructors }
  226. if (aktprocdef.proctypeoption=potype_constructor) then
  227. begin
  228. getlabel(faillabel);
  229. getlabel(quickexitlabel);
  230. end;
  231. { reset break and continue labels }
  232. block_type:=bt_general;
  233. aktbreaklabel:=nil;
  234. aktcontinuelabel:=nil;
  235. { insert symtables for the class, by only if it is no nested function }
  236. if assigned(procinfo^._class) and not(parent_has_class) then
  237. begin
  238. { insert them in the reverse order ! }
  239. hp:=nil;
  240. repeat
  241. _class:=procinfo^._class;
  242. while _class.childof<>hp do
  243. _class:=_class.childof;
  244. hp:=_class;
  245. _class.symtable.next:=symtablestack;
  246. symtablestack:=_class.symtable;
  247. until hp=procinfo^._class;
  248. end;
  249. { insert parasymtable in symtablestack}
  250. { only if lexlevel > 1 !!! global symtable should be right after staticsymtazble
  251. for checking of same names used in interface and implementation !! }
  252. if lexlevel>=normal_function_level then
  253. begin
  254. aktprocdef.parast.next:=symtablestack;
  255. symtablestack:=aktprocdef.parast;
  256. symtablestack.symtablelevel:=lexlevel;
  257. end;
  258. { insert localsymtable in symtablestack}
  259. aktprocdef.localst.next:=symtablestack;
  260. symtablestack:=aktprocdef.localst;
  261. symtablestack.symtablelevel:=lexlevel;
  262. { constant symbols are inserted in this symboltable }
  263. constsymtable:=symtablestack;
  264. { reset the temporary memory }
  265. cleartempgen;
  266. {$ifdef newcg}
  267. {$ifdef POWERPC}
  268. tgcpu.usedinproc:=0;
  269. {$else POWERPC}
  270. tg.usedinproc:=[];
  271. {$endif POWERPC}
  272. {$else newcg}
  273. {$ifdef i386}
  274. { no registers are used }
  275. usedinproc:=0;
  276. {$else}
  277. usedinproc := [];
  278. {$endif}
  279. {$endif newcg}
  280. { save entry info }
  281. entrypos:=aktfilepos;
  282. entryswitches:=aktlocalswitches;
  283. localmaxfpuregisters:=aktmaxfpuregisters;
  284. { parse the code ... }
  285. code:=block(current_module.islibrary);
  286. { get a better entry point }
  287. if assigned(code) then
  288. entrypos:=code.fileinfo;
  289. { save exit info }
  290. exitswitches:=aktlocalswitches;
  291. exitpos:=last_endtoken_filepos;
  292. { save current filepos }
  293. savepos:=aktfilepos;
  294. {When we are called to compile the body of a unit, aktprocsym should
  295. point to the unit initialization. If the unit has no initialization,
  296. aktprocsym=nil. But in that case code=nil. hus we should check for
  297. code=nil, when we use aktprocsym.}
  298. { set the framepointer to esp for assembler functions }
  299. { but only if the are no local variables }
  300. { already done in assembler_block }
  301. {$ifdef newcg}
  302. setfirsttemp(procinfo^.firsttemp_offset);
  303. {$else newcg}
  304. setfirsttemp(procinfo^.firsttemp_offset);
  305. {$endif newcg}
  306. { ... and generate assembler }
  307. { but set the right switches for entry !! }
  308. aktlocalswitches:=entryswitches;
  309. oldaktmaxfpuregisters:=aktmaxfpuregisters;
  310. aktmaxfpuregisters:=localmaxfpuregisters;
  311. if assigned(code) then
  312. begin
  313. { the procedure is now defined }
  314. aktprocdef.forwarddef:=false;
  315. { only generate the code if no type errors are found, else
  316. finish at least the type checking pass }
  317. {$ifndef NOPASS2}
  318. if (status.errorcount=0) then
  319. begin
  320. generatecode(code);
  321. aktprocdef.code:=code;
  322. {$ifdef newcg}
  323. stackframe:=gettempsize;
  324. {$else newcg}
  325. stackframe:=gettempsize;
  326. {$endif newcg}
  327. { first generate entry code with the correct position and switches }
  328. aktfilepos:=entrypos;
  329. aktlocalswitches:=entryswitches;
  330. {$ifdef newcg}
  331. cg^.g_entrycode(procinfo^.aktentrycode,proc_names,make_global,stackframe,parasize,nostackframe,false);
  332. {$else newcg}
  333. genentrycode(procinfo^.aktentrycode,make_global,stackframe,parasize,nostackframe,false);
  334. {$endif newcg}
  335. { FPC_POPADDRSTACK destroys all registers (JM) }
  336. if (procinfo^.flags and (pi_needs_implicit_finally or pi_uses_exceptions)) <> 0 then
  337. begin
  338. {$ifdef i386}
  339. usedinproc := $ff;
  340. {$else}
  341. usedinproc := ALL_REGISTERS;
  342. {$endif}
  343. end;
  344. { now generate exit code with the correct position and switches }
  345. aktfilepos:=exitpos;
  346. aktlocalswitches:=exitswitches;
  347. {$ifdef newcg}
  348. cg^.g_exitcode(procinfo^.aktexitcode,parasize,nostackframe,false);
  349. {$else newcg}
  350. genexitcode(procinfo^.aktexitcode,parasize,nostackframe,false);
  351. {$endif newcg}
  352. { now all the registers used are known }
  353. {$ifdef newcg}
  354. aktprocdef.usedregisters:=tg.usedinproc;
  355. {$else newcg}
  356. aktprocdef.usedregisters:=usedinproc;
  357. {$endif newcg}
  358. procinfo^.aktproccode.insertlist(procinfo^.aktentrycode);
  359. procinfo^.aktproccode.concatlist(procinfo^.aktexitcode);
  360. {$ifdef i386}
  361. {$ifndef NoOpt}
  362. if (cs_optimize in aktglobalswitches) and
  363. { do not optimize pure assembler procedures }
  364. ((procinfo^.flags and pi_is_assembler)=0) then
  365. Optimize(procinfo^.aktproccode);
  366. {$endif NoOpt}
  367. {$endif i386}
  368. { save local data (casetable) also in the same file }
  369. if assigned(procinfo^.aktlocaldata) and
  370. (not procinfo^.aktlocaldata.empty) then
  371. begin
  372. procinfo^.aktproccode.concat(Tai_section.Create(sec_data));
  373. procinfo^.aktproccode.concatlist(procinfo^.aktlocaldata);
  374. procinfo^.aktproccode.concat(Tai_section.Create(sec_code));
  375. end;
  376. { add the procedure to the codesegment }
  377. if (cs_create_smart in aktmoduleswitches) then
  378. codeSegment.concat(Tai_cut.Create);
  379. codeSegment.concatlist(procinfo^.aktproccode);
  380. end
  381. else
  382. do_resulttypepass(code);
  383. {$else NOPASS2}
  384. do_resulttypepass(code);
  385. {$endif NOPASS2}
  386. end;
  387. { ... remove symbol tables }
  388. if lexlevel>=normal_function_level then
  389. symtablestack:=symtablestack.next.next
  390. else
  391. symtablestack:=symtablestack.next;
  392. { ... check for unused symbols }
  393. { but only if there is no asm block }
  394. if assigned(code) then
  395. begin
  396. if (Errorcount=0) then
  397. begin
  398. { check if forwards are resolved }
  399. tstoredsymtable(aktprocdef.localst).check_forwards;
  400. { check if all labels are used }
  401. tstoredsymtable(aktprocdef.localst).checklabels;
  402. { remove cross unit overloads }
  403. tstoredsymtable(aktprocdef.localst).unchain_overloaded;
  404. end;
  405. if (procinfo^.flags and pi_uses_asm)=0 then
  406. begin
  407. { not for unit init, becuase the var can be used in finalize,
  408. it will be done in proc_unit }
  409. if not(aktprocdef.proctypeoption
  410. in [potype_proginit,potype_unitinit,potype_unitfinalize]) then
  411. tstoredsymtable(aktprocdef.localst).allsymbolsused;
  412. tstoredsymtable(aktprocdef.parast).allsymbolsused;
  413. end;
  414. end;
  415. { the local symtables can be deleted, but the parast }
  416. { doesn't, (checking definitons when calling a }
  417. { function }
  418. { not for a inline procedure !! (PM) }
  419. { at lexlevel = 1 localst is the staticsymtable itself }
  420. { so no dispose here !! }
  421. if assigned(code) and
  422. not(cs_browser in aktmoduleswitches) and
  423. (aktprocdef.proccalloption<>pocall_inline) then
  424. begin
  425. if lexlevel>=normal_function_level then
  426. aktprocdef.localst.free;
  427. aktprocdef.localst:=nil;
  428. end;
  429. {$ifdef newcg}
  430. { all registers can be used again }
  431. tg.resetusableregisters;
  432. { only now we can remove the temps }
  433. tg.resettempgen;
  434. {$else newcg}
  435. { all registers can be used again }
  436. resetusableregisters;
  437. { only now we can remove the temps }
  438. resettempgen;
  439. {$endif newcg}
  440. { remove code tree, if not inline procedure }
  441. if assigned(code) and (aktprocdef.proccalloption<>pocall_inline) then
  442. code.free;
  443. { remove class member symbol tables }
  444. while symtablestack.symtabletype=objectsymtable do
  445. symtablestack:=symtablestack.next;
  446. aktmaxfpuregisters:=oldaktmaxfpuregisters;
  447. { restore filepos, the switches are already set }
  448. aktfilepos:=savepos;
  449. { restore labels }
  450. aktexitlabel:=oldexitlabel;
  451. aktexit2label:=oldexit2label;
  452. quickexitlabel:=oldquickexitlabel;
  453. faillabel:=oldfaillabel;
  454. { reset to normal non static function }
  455. if (lexlevel=normal_function_level) then
  456. allow_only_static:=false;
  457. { previous lexlevel }
  458. dec(lexlevel);
  459. end;
  460. {****************************************************************************
  461. PROCEDURE/FUNCTION PARSING
  462. ****************************************************************************}
  463. procedure checkvaluepara(p:tnamedindexitem);
  464. var
  465. vs : tvarsym;
  466. s : string;
  467. begin
  468. with tvarsym(p) do
  469. begin
  470. if copy(name,1,3)='val' then
  471. begin
  472. s:=Copy(name,4,255);
  473. if not(po_assembler in aktprocdef.procoptions) then
  474. begin
  475. vs:=tvarsym.create(s,vartype);
  476. vs.fileinfo:=fileinfo;
  477. vs.varspez:=varspez;
  478. aktprocdef.localst.insert(vs);
  479. include(vs.varoptions,vo_is_local_copy);
  480. vs.varstate:=vs_assigned;
  481. localvarsym:=vs;
  482. inc(refs); { the para was used to set the local copy ! }
  483. { warnings only on local copy ! }
  484. varstate:=vs_used;
  485. end
  486. else
  487. begin
  488. aktprocdef.parast.rename(name,s);
  489. end;
  490. end;
  491. end;
  492. end;
  493. procedure read_proc;
  494. {
  495. Parses the procedure directives, then parses the procedure body, then
  496. generates the code for it
  497. }
  498. var
  499. oldprefix : string;
  500. oldprocsym : tprocsym;
  501. oldprocdef : tprocdef;
  502. oldprocinfo : pprocinfo;
  503. oldconstsymtable : tsymtable;
  504. oldfilepos : tfileposinfo;
  505. pdflags : word;
  506. begin
  507. { save old state }
  508. oldprocdef:=aktprocdef;
  509. oldprocsym:=aktprocsym;
  510. oldprefix:=procprefix;
  511. oldconstsymtable:=constsymtable;
  512. oldprocinfo:=procinfo;
  513. { create a new procedure }
  514. codegen_newprocedure;
  515. with procinfo^ do
  516. begin
  517. parent:=oldprocinfo;
  518. { clear flags }
  519. flags:=0;
  520. { standard frame pointer }
  521. framepointer:=frame_pointer;
  522. { is this a nested function of a method ? }
  523. if assigned(oldprocinfo) then
  524. _class:=oldprocinfo^._class;
  525. end;
  526. parse_proc_dec;
  527. procinfo^.procdef:=aktprocdef;
  528. { set the default function options }
  529. if parse_only then
  530. begin
  531. aktprocdef.forwarddef:=true;
  532. { set also the interface flag, for better error message when the
  533. implementation doesn't much this header }
  534. aktprocdef.interfacedef:=true;
  535. pdflags:=pd_interface;
  536. end
  537. else
  538. begin
  539. pdflags:=pd_body;
  540. if current_module.in_implementation then
  541. pdflags:=pdflags or pd_implemen;
  542. if (not current_module.is_unit) or (cs_create_smart in aktmoduleswitches) then
  543. pdflags:=pdflags or pd_global;
  544. procinfo^.exported:=false;
  545. aktprocdef.forwarddef:=false;
  546. end;
  547. { parse the directives that may follow }
  548. inc(lexlevel);
  549. parse_proc_directives(pdflags);
  550. dec(lexlevel);
  551. { hint directives, these can be separated by semicolons here,
  552. that need to be handled here with a loop (PFV) }
  553. while try_consume_hintdirective(aktprocsym.symoptions) do
  554. Consume(_SEMICOLON);
  555. { set aktfilepos to the beginning of the function declaration }
  556. oldfilepos:=aktfilepos;
  557. aktfilepos:=aktprocdef.fileinfo;
  558. { For varargs directive also cdecl and external must be defined }
  559. if (po_varargs in aktprocdef.procoptions) then
  560. begin
  561. { check first for external in the interface, if available there
  562. then the cdecl must also be there since there is no implementation
  563. available to contain it }
  564. if parse_only then
  565. begin
  566. { if external is available, then cdecl must also be available }
  567. if (po_external in aktprocdef.procoptions) and
  568. not(aktprocdef.proccalloption in [pocall_cdecl,pocall_cppdecl]) then
  569. Message(parser_e_varargs_need_cdecl_and_external);
  570. end
  571. else
  572. begin
  573. { both must be defined now }
  574. if not(po_external in aktprocdef.procoptions) or
  575. not(aktprocdef.proccalloption in [pocall_cdecl,pocall_cppdecl]) then
  576. Message(parser_e_varargs_need_cdecl_and_external);
  577. end;
  578. end;
  579. { search for forward declarations }
  580. if not proc_add_definition(aktprocsym,aktprocdef) then
  581. begin
  582. { A method must be forward defined (in the object declaration) }
  583. if assigned(procinfo^._class) and
  584. (not assigned(oldprocinfo^._class)) then
  585. begin
  586. Message1(parser_e_header_dont_match_any_member,aktprocdef.fullprocname);
  587. aktprocsym.write_parameter_lists(aktprocdef);
  588. end
  589. else
  590. begin
  591. { Give a better error if there is a forward def in the interface and only
  592. a single implementation }
  593. if (not aktprocdef.forwarddef) and
  594. assigned(aktprocsym.defs^.next) and
  595. aktprocsym.defs^.def.forwarddef and
  596. aktprocsym.defs^.def.interfacedef and
  597. not(assigned(aktprocsym.defs^.next^.next)) then
  598. begin
  599. Message1(parser_e_header_dont_match_forward,aktprocdef.fullprocname);
  600. aktprocsym.write_parameter_lists(aktprocdef);
  601. end
  602. else
  603. begin
  604. { check the global flag, for delphi this is not
  605. required }
  606. if not(m_delphi in aktmodeswitches) and
  607. ((procinfo^.flags and pi_is_global)<>0) then
  608. Message(parser_e_overloaded_must_be_all_global);
  609. end;
  610. end;
  611. end;
  612. { update procinfo, because the aktprocdef can be
  613. changed by check_identical_proc (PFV) }
  614. procinfo^.procdef:=aktprocdef;
  615. {$ifdef i386}
  616. { add implicit pushes for interrupt routines }
  617. if (po_interrupt in aktprocdef.procoptions) then
  618. begin
  619. { we push Flags and CS as long
  620. to cope with the IRETD
  621. and we save 6 register + 4 selectors }
  622. inc(procinfo^.para_offset,8+6*4+4*2);
  623. end;
  624. {$endif i386}
  625. { pointer to the return value ? }
  626. if ret_in_param(aktprocdef.rettype.def) then
  627. begin
  628. procinfo^.return_offset:=procinfo^.para_offset;
  629. inc(procinfo^.para_offset,target_info.size_of_pointer);
  630. end;
  631. { allows to access the parameters of main functions in nested functions }
  632. aktprocdef.parast.address_fixup:=procinfo^.para_offset;
  633. { when it is a value para and it needs a local copy then rename
  634. the parameter and insert a copy in the localst. This is not done
  635. for assembler procedures }
  636. if (not parse_only) and (not aktprocdef.forwarddef) then
  637. aktprocdef.parast.foreach_static({$ifdef FPCPROCVAR}@{$endif}checkvaluepara);
  638. { restore file pos }
  639. aktfilepos:=oldfilepos;
  640. { compile procedure when a body is needed }
  641. if (pdflags and pd_body)<>0 then
  642. begin
  643. Message1(parser_p_procedure_start,
  644. aktprocdef.fullprocname);
  645. aktprocdef.aliasnames.insert(aktprocdef.mangledname);
  646. { set _FAIL as keyword if constructor }
  647. if (aktprocdef.proctypeoption=potype_constructor) then
  648. tokeninfo^[_FAIL].keyword:=m_all;
  649. if assigned(aktprocdef._class) then
  650. tokeninfo^[_SELF].keyword:=m_all;
  651. compile_proc_body(((pdflags and pd_global)<>0),assigned(oldprocinfo^._class));
  652. { reset _FAIL as normal }
  653. if (aktprocdef.proctypeoption=potype_constructor) then
  654. tokeninfo^[_FAIL].keyword:=m_none;
  655. if assigned(aktprocdef._class) and (lexlevel=main_program_level) then
  656. tokeninfo^[_SELF].keyword:=m_none;
  657. consume(_SEMICOLON);
  658. end;
  659. { close }
  660. codegen_doneprocedure;
  661. { Restore old state }
  662. constsymtable:=oldconstsymtable;
  663. { from now on all refernece to mangledname means
  664. that the function is already used }
  665. aktprocdef.count:=true;
  666. {$ifdef notused}
  667. { restore the interface order to maintain CRC values PM }
  668. if assigned(prevdef) and assigned(aktprocdef.nextoverloaded) then
  669. begin
  670. stdef:=aktprocdef;
  671. aktprocdef:=stdef.nextoverloaded;
  672. stdef.nextoverloaded:=prevdef.nextoverloaded;
  673. prevdef.nextoverloaded:=stdef;
  674. end;
  675. {$endif notused}
  676. aktprocsym:=oldprocsym;
  677. aktprocdef:=oldprocdef;
  678. procprefix:=oldprefix;
  679. procinfo:=oldprocinfo;
  680. otsym:=nil;
  681. end;
  682. {****************************************************************************
  683. DECLARATION PARSING
  684. ****************************************************************************}
  685. { search in symtablestack for not complete classes }
  686. procedure check_forward_class(p : tnamedindexitem);
  687. begin
  688. if (tsym(p).typ=typesym) and
  689. (ttypesym(p).restype.def.deftype=objectdef) and
  690. (oo_is_forward in tobjectdef(ttypesym(p).restype.def).objectoptions) then
  691. MessagePos1(tsym(p).fileinfo,sym_e_forward_type_not_resolved,tsym(p).realname);
  692. end;
  693. procedure read_declarations(islibrary : boolean);
  694. procedure Not_supported_for_inline(t : ttoken);
  695. begin
  696. if assigned(aktprocsym) and
  697. (aktprocdef.proccalloption=pocall_inline) then
  698. Begin
  699. Message1(parser_w_not_supported_for_inline,tokenstring(t));
  700. Message(parser_w_inlining_disabled);
  701. aktprocdef.proccalloption:=pocall_fpccall;
  702. End;
  703. end;
  704. begin
  705. repeat
  706. case token of
  707. _LABEL:
  708. begin
  709. Not_supported_for_inline(token);
  710. label_dec;
  711. end;
  712. _CONST:
  713. begin
  714. Not_supported_for_inline(token);
  715. const_dec;
  716. end;
  717. _TYPE:
  718. begin
  719. Not_supported_for_inline(token);
  720. type_dec;
  721. end;
  722. _VAR:
  723. var_dec;
  724. _THREADVAR:
  725. threadvar_dec;
  726. _CONSTRUCTOR,_DESTRUCTOR,
  727. _FUNCTION,_PROCEDURE,_OPERATOR,_CLASS:
  728. begin
  729. Not_supported_for_inline(token);
  730. read_proc;
  731. end;
  732. _RESOURCESTRING:
  733. resourcestring_dec;
  734. _EXPORTS:
  735. begin
  736. Not_supported_for_inline(token);
  737. { here we should be at lexlevel 1, no ? PM }
  738. if (lexlevel<>main_program_level) or
  739. (current_module.is_unit) then
  740. begin
  741. Message(parser_e_syntax_error);
  742. consume_all_until(_SEMICOLON);
  743. end
  744. else if islibrary or (target_info.target=target_i386_WIN32)
  745. or (target_info.target=target_i386_Netware) then // AD
  746. read_exports;
  747. end
  748. else break;
  749. end;
  750. until false;
  751. { check for incomplete class definitions, this is only required
  752. for fpc modes }
  753. if (m_fpc in aktmodeswitches) then
  754. symtablestack.foreach_static({$ifdef FPCPROCVAR}@{$endif}check_forward_class);
  755. end;
  756. procedure read_interface_declarations;
  757. begin
  758. {Since the body is now parsed at lexlevel 1, and the declarations
  759. must be parsed at the same lexlevel we increase the lexlevel.}
  760. inc(lexlevel);
  761. repeat
  762. case token of
  763. _CONST :
  764. const_dec;
  765. _TYPE :
  766. type_dec;
  767. _VAR :
  768. var_dec;
  769. _THREADVAR :
  770. threadvar_dec;
  771. _RESOURCESTRING:
  772. resourcestring_dec;
  773. _FUNCTION,
  774. _PROCEDURE,
  775. _OPERATOR :
  776. read_proc;
  777. else
  778. break;
  779. end;
  780. until false;
  781. dec(lexlevel);
  782. { check for incomplete class definitions, this is only required
  783. for fpc modes }
  784. if (m_fpc in aktmodeswitches) then
  785. symtablestack.foreach_static({$ifdef FPCPROCVAR}@{$endif}check_forward_class);
  786. end;
  787. end.
  788. {
  789. $Log$
  790. Revision 1.44 2002-01-19 15:37:24 peter
  791. * commited the wrong file :(
  792. Revision 1.43 2002/01/19 15:20:09 peter
  793. * also check at the end of the implementation for incomplete classes
  794. Revision 1.42 2002/01/19 15:12:34 peter
  795. * check for unresolved forward classes in the interface
  796. Revision 1.41 2001/11/02 22:58:06 peter
  797. * procsym definition rewrite
  798. Revision 1.40 2001/10/25 21:22:37 peter
  799. * calling convention rewrite
  800. Revision 1.39 2001/10/22 21:20:46 peter
  801. * overloaded functions don't need to be global in kylix
  802. Revision 1.38 2001/10/01 13:38:45 jonas
  803. * allow self parameter for normal procedures again (because Kylix allows
  804. it too) ("merged")
  805. Revision 1.37 2001/09/10 10:26:26 jonas
  806. * fixed web bug 1593
  807. * writing of procvar headers is more complete (mention var/const/out for
  808. paras, add "of object" if applicable)
  809. + error if declaring explicit self para as var/const
  810. * fixed mangled name of procedures which contain an explicit self para
  811. * parsing para's should be slightly faster because mangled name of
  812. procedure is only updated once instead of after parsing each para
  813. (all merged from fixes)
  814. Revision 1.36 2001/08/26 13:36:46 florian
  815. * some cg reorganisation
  816. * some PPC updates
  817. Revision 1.35 2001/08/06 21:40:47 peter
  818. * funcret moved from tprocinfo to tprocdef
  819. Revision 1.34 2001/06/04 11:53:13 peter
  820. + varargs directive
  821. Revision 1.33 2001/06/03 21:57:37 peter
  822. + hint directive parsing support
  823. Revision 1.32 2001/04/21 12:03:12 peter
  824. * m68k updates merged from fixes branch
  825. Revision 1.31 2001/04/18 22:01:57 peter
  826. * registration of targets and assemblers
  827. Revision 1.30 2001/04/14 14:05:47 peter
  828. * better skipping of secondpass if error
  829. Revision 1.29 2001/04/13 23:49:24 peter
  830. * when errors are found don't generate code, but still run the
  831. resulttype pass
  832. Revision 1.28 2001/04/13 17:59:03 peter
  833. * don't generate code when there is already an error
  834. Revision 1.27 2001/04/13 01:22:13 peter
  835. * symtable change to classes
  836. * range check generation and errors fixed, make cycle DEBUG=1 works
  837. * memory leaks fixed
  838. Revision 1.26 2001/04/02 21:20:34 peter
  839. * resulttype rewrite
  840. Revision 1.25 2001/02/26 19:44:53 peter
  841. * merged generic m68k updates from fixes branch
  842. Revision 1.24 2000/12/25 00:07:27 peter
  843. + new tlinkedlist class (merge of old tstringqueue,tcontainer and
  844. tlinkedlist objects)
  845. Revision 1.23 2000/11/29 00:30:37 florian
  846. * unused units removed from uses clause
  847. * some changes for widestrings
  848. Revision 1.22 2000/11/08 16:38:24 jonas
  849. * if a procedure uses exceptions (be it implicit or explicit), the
  850. usedregisters are set to all (because FPC_POPADDRSTACK doesn't save
  851. any registers) ("merged", fixes make cycle woth -Or)
  852. Revision 1.21 2000/11/01 23:04:38 peter
  853. * tprocdef.fullprocname added for better casesensitve writing of
  854. procedures
  855. Revision 1.20 2000/10/31 22:02:50 peter
  856. * symtable splitted, no real code changes
  857. Revision 1.19 2000/10/24 22:21:25 peter
  858. * set usedregisters after writing entry and exit code (merged)
  859. Revision 1.18 2000/10/21 18:16:12 florian
  860. * a lot of changes:
  861. - basic dyn. array support
  862. - basic C++ support
  863. - some work for interfaces done
  864. ....
  865. Revision 1.17 2000/10/15 07:47:51 peter
  866. * unit names and procedure names are stored mixed case
  867. Revision 1.16 2000/10/14 10:14:52 peter
  868. * moehrendorf oct 2000 rewrite
  869. Revision 1.15 2000/09/24 21:33:47 peter
  870. * message updates merges
  871. Revision 1.14 2000/09/24 21:19:51 peter
  872. * delphi compile fixes
  873. Revision 1.13 2000/09/24 15:06:24 peter
  874. * use defines.inc
  875. Revision 1.12 2000/09/10 20:11:07 peter
  876. * overload checking in implementation removed (merged)
  877. Revision 1.11 2000/09/04 20:15:19 peter
  878. * fixed operator overloading
  879. Revision 1.10 2000/08/27 16:11:52 peter
  880. * moved some util functions from globals,cobjects to cutils
  881. * splitted files into finput,fmodule
  882. Revision 1.9 2000/08/16 18:33:54 peter
  883. * splitted namedobjectitem.next into indexnext and listnext so it
  884. can be used in both lists
  885. * don't allow "word = word" type definitions (merged)
  886. Revision 1.8 2000/08/13 12:54:56 peter
  887. * class member decl wrong then no other error after it
  888. * -vb has now also line numbering
  889. * -vb is also used for interface/implementation different decls and
  890. doesn't list the current function (merged)
  891. Revision 1.7 2000/08/08 19:28:57 peter
  892. * memdebug/memory patches (merged)
  893. * only once illegal directive (merged)
  894. Revision 1.6 2000/08/06 19:39:28 peter
  895. * default parameters working !
  896. Revision 1.5 2000/08/06 14:17:15 peter
  897. * overload fixes (merged)
  898. Revision 1.4 2000/07/30 17:04:43 peter
  899. * merged fixes
  900. Revision 1.3 2000/07/13 12:08:27 michael
  901. + patched to 1.1.0 with former 1.09patch from peter
  902. Revision 1.2 2000/07/13 11:32:46 michael
  903. + removed logs
  904. }