psub.pas 33 KB

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