psub.pas 32 KB

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