psub.pas 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910
  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_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. { only generate the code if no type errors are found }
  306. if assigned(code) and
  307. (status.errorcount=0) then
  308. generatecode(code);
  309. { set switches to status at end of procedure }
  310. aktlocalswitches:=exitswitches;
  311. if assigned(code) then
  312. begin
  313. aktprocsym.definition.code:=code;
  314. { the procedure is now defined }
  315. aktprocsym.definition.forwarddef:=false;
  316. end;
  317. {$ifdef newcg}
  318. stackframe:=tg.gettempsize;
  319. {$else newcg}
  320. stackframe:=gettempsize;
  321. {$endif newcg}
  322. { first generate entry code with the correct position and switches }
  323. aktfilepos:=entrypos;
  324. aktlocalswitches:=entryswitches;
  325. {$ifdef newcg}
  326. if assigned(code) then
  327. cg^.g_entrycode(procinfo^.aktentrycode,proc_names,make_global,stackframe,parasize,nostackframe,false);
  328. {$else newcg}
  329. if assigned(code) then
  330. genentrycode(procinfo^.aktentrycode,make_global,stackframe,parasize,nostackframe,false);
  331. {$endif newcg}
  332. { FPC_POPADDRSTACK destroys all registers (JM) }
  333. if (procinfo^.flags and (pi_needs_implicit_finally or pi_uses_exceptions)) <> 0 then
  334. usedinproc := $ff;
  335. { now generate exit code with the correct position and switches }
  336. aktfilepos:=exitpos;
  337. aktlocalswitches:=exitswitches;
  338. if assigned(code) then
  339. begin
  340. {$ifdef newcg}
  341. cg^.g_exitcode(procinfo^.aktexitcode,parasize,nostackframe,false);
  342. {$else newcg}
  343. genexitcode(procinfo^.aktexitcode,parasize,nostackframe,false);
  344. {$endif newcg}
  345. { now all the registers used are known }
  346. {$ifdef newcg}
  347. aktprocsym.definition.usedregisters:=tg.usedinproc;
  348. {$else newcg}
  349. aktprocsym.definition.usedregisters:=usedinproc;
  350. {$endif newcg}
  351. procinfo^.aktproccode.insertlist(procinfo^.aktentrycode);
  352. procinfo^.aktproccode.concatlist(procinfo^.aktexitcode);
  353. {$ifdef i386}
  354. {$ifndef NoOpt}
  355. if (cs_optimize in aktglobalswitches) and
  356. { do not optimize pure assembler procedures }
  357. ((procinfo^.flags and pi_is_assembler)=0) then
  358. Optimize(procinfo^.aktproccode);
  359. {$endif NoOpt}
  360. {$endif i386}
  361. { save local data (casetable) also in the same file }
  362. if assigned(procinfo^.aktlocaldata) and
  363. (not procinfo^.aktlocaldata.empty) then
  364. begin
  365. procinfo^.aktproccode.concat(Tai_section.Create(sec_data));
  366. procinfo^.aktproccode.concatlist(procinfo^.aktlocaldata);
  367. procinfo^.aktproccode.concat(Tai_section.Create(sec_code));
  368. end;
  369. { now we can insert a cut }
  370. if (cs_create_smart in aktmoduleswitches) then
  371. codeSegment.concat(Tai_cut.Create);
  372. { add the procedure to the codesegment }
  373. codeSegment.concatlist(procinfo^.aktproccode);
  374. end;
  375. {$endif NOPASS2}
  376. { ... remove symbol tables, for the browser leave the static table }
  377. { if (cs_browser in aktmoduleswitches) and (symtablestack.symtabletype=staticsymtable) then
  378. symtablestack.next:=symtablestack.next^.next
  379. else }
  380. if lexlevel>=normal_function_level then
  381. symtablestack:=symtablestack.next.next
  382. else
  383. symtablestack:=symtablestack.next;
  384. { ... check for unused symbols }
  385. { but only if there is no asm block }
  386. if assigned(code) then
  387. begin
  388. if (Errorcount=0) then
  389. begin
  390. tstoredsymtable(aktprocsym.definition.localst).check_forwards;
  391. tstoredsymtable(aktprocsym.definition.localst).checklabels;
  392. end;
  393. if (procinfo^.flags and pi_uses_asm)=0 then
  394. begin
  395. { not for unit init, becuase the var can be used in finalize,
  396. it will be done in proc_unit }
  397. if not(aktprocsym.definition.proctypeoption
  398. in [potype_proginit,potype_unitinit,potype_unitfinalize]) then
  399. tstoredsymtable(aktprocsym.definition.localst).allsymbolsused;
  400. tstoredsymtable(aktprocsym.definition.parast).allsymbolsused;
  401. end;
  402. end;
  403. { the local symtables can be deleted, but the parast }
  404. { doesn't, (checking definitons when calling a }
  405. { function }
  406. { not for a inline procedure !! (PM) }
  407. { at lexlevel = 1 localst is the staticsymtable itself }
  408. { so no dispose here !! }
  409. if assigned(code) and
  410. not(cs_browser in aktmoduleswitches) and
  411. not(pocall_inline in aktprocsym.definition.proccalloptions) then
  412. begin
  413. if lexlevel>=normal_function_level then
  414. aktprocsym.definition.localst.free;
  415. aktprocsym.definition.localst:=nil;
  416. end;
  417. {$ifdef newcg}
  418. { all registers can be used again }
  419. tg.resetusableregisters;
  420. { only now we can remove the temps }
  421. tg.resettempgen;
  422. {$else newcg}
  423. { all registers can be used again }
  424. resetusableregisters;
  425. { only now we can remove the temps }
  426. resettempgen;
  427. {$endif newcg}
  428. { remove code tree, if not inline procedure }
  429. if assigned(code) and not(pocall_inline in aktprocsym.definition.proccalloptions) then
  430. code.free;
  431. { remove class member symbol tables }
  432. while symtablestack.symtabletype=objectsymtable do
  433. symtablestack:=symtablestack.next;
  434. aktmaxfpuregisters:=oldaktmaxfpuregisters;
  435. { restore filepos, the switches are already set }
  436. aktfilepos:=savepos;
  437. { restore labels }
  438. aktexitlabel:=oldexitlabel;
  439. aktexit2label:=oldexit2label;
  440. quickexitlabel:=oldquickexitlabel;
  441. faillabel:=oldfaillabel;
  442. { reset to normal non static function }
  443. if (lexlevel=normal_function_level) then
  444. allow_only_static:=false;
  445. { previous lexlevel }
  446. dec(lexlevel);
  447. end;
  448. {****************************************************************************
  449. PROCEDURE/FUNCTION PARSING
  450. ****************************************************************************}
  451. procedure checkvaluepara(p:tnamedindexitem);
  452. var
  453. vs : tvarsym;
  454. s : string;
  455. begin
  456. with tvarsym(p) do
  457. begin
  458. if copy(name,1,3)='val' then
  459. begin
  460. s:=Copy(name,4,255);
  461. if not(po_assembler in aktprocsym.definition.procoptions) then
  462. begin
  463. vs:=tvarsym.create(s,vartype);
  464. vs.fileinfo:=fileinfo;
  465. vs.varspez:=varspez;
  466. aktprocsym.definition.localst.insert(vs);
  467. include(vs.varoptions,vo_is_local_copy);
  468. vs.varstate:=vs_assigned;
  469. localvarsym:=vs;
  470. inc(refs); { the para was used to set the local copy ! }
  471. { warnings only on local copy ! }
  472. varstate:=vs_used;
  473. end
  474. else
  475. begin
  476. aktprocsym.definition.parast.rename(name,s);
  477. end;
  478. end;
  479. end;
  480. end;
  481. procedure read_proc;
  482. {
  483. Parses the procedure directives, then parses the procedure body, then
  484. generates the code for it
  485. }
  486. var
  487. oldprefix : string;
  488. oldprocsym : tprocsym;
  489. oldprocinfo : pprocinfo;
  490. oldconstsymtable : tsymtable;
  491. oldfilepos : tfileposinfo;
  492. pdflags : word;
  493. prevdef,stdef : tprocdef;
  494. begin
  495. { save old state }
  496. oldprocsym:=aktprocsym;
  497. oldprefix:=procprefix;
  498. oldconstsymtable:=constsymtable;
  499. oldprocinfo:=procinfo;
  500. { create a new procedure }
  501. codegen_newprocedure;
  502. with procinfo^ do
  503. begin
  504. parent:=oldprocinfo;
  505. { clear flags }
  506. flags:=0;
  507. { standard frame pointer }
  508. framepointer:=frame_pointer;
  509. { funcret_is_valid:=false; }
  510. funcret_state:=vs_declared;
  511. { is this a nested function of a method ? }
  512. if assigned(oldprocinfo) then
  513. _class:=oldprocinfo^._class;
  514. end;
  515. parse_proc_dec;
  516. procinfo^.sym:=aktprocsym;
  517. procinfo^.def:=aktprocsym.definition;
  518. { set the default function options }
  519. if parse_only then
  520. begin
  521. aktprocsym.definition.forwarddef:=true;
  522. { set also the interface flag, for better error message when the
  523. implementation doesn't much this header }
  524. aktprocsym.definition.interfacedef:=true;
  525. pdflags:=pd_interface;
  526. end
  527. else
  528. begin
  529. pdflags:=pd_body;
  530. if current_module.in_implementation then
  531. pdflags:=pdflags or pd_implemen;
  532. if (not current_module.is_unit) or (cs_create_smart in aktmoduleswitches) then
  533. pdflags:=pdflags or pd_global;
  534. procinfo^.exported:=false;
  535. aktprocsym.definition.forwarddef:=false;
  536. end;
  537. { parse the directives that may follow }
  538. inc(lexlevel);
  539. parse_proc_directives(pdflags);
  540. dec(lexlevel);
  541. { set aktfilepos to the beginning of the function declaration }
  542. oldfilepos:=aktfilepos;
  543. aktfilepos:=aktprocsym.definition.fileinfo;
  544. { search for forward declarations }
  545. if not check_identical_proc(prevdef) then
  546. begin
  547. { A method must be forward defined (in the object declaration) }
  548. if assigned(procinfo^._class) and (not assigned(oldprocinfo^._class)) then
  549. begin
  550. Message1(parser_e_header_dont_match_any_member,
  551. aktprocsym.definition.fullprocname);
  552. aktprocsym.write_parameter_lists(aktprocsym.definition);
  553. end
  554. else
  555. begin
  556. { Give a better error if there is a forward def in the interface and only
  557. a single implementation }
  558. if (not aktprocsym.definition.forwarddef) and
  559. assigned(aktprocsym.definition.nextoverloaded) and
  560. aktprocsym.definition.nextoverloaded.forwarddef and
  561. aktprocsym.definition.nextoverloaded.interfacedef and
  562. not(assigned(aktprocsym.definition.nextoverloaded.nextoverloaded)) then
  563. begin
  564. Message1(parser_e_header_dont_match_forward,
  565. aktprocsym.definition.fullprocname);
  566. aktprocsym.write_parameter_lists(aktprocsym.definition);
  567. end
  568. else
  569. begin
  570. { check the global flag }
  571. if (procinfo^.flags and pi_is_global)<>0 then
  572. Message(parser_e_overloaded_must_be_all_global);
  573. end;
  574. end;
  575. end;
  576. { set return type here, becuase the aktprocsym.definition can be
  577. changed by check_identical_proc (PFV) }
  578. procinfo^.returntype.def:=aktprocsym.definition.rettype.def;
  579. {$ifdef i386}
  580. if (po_interrupt in aktprocsym.definition.procoptions) then
  581. begin
  582. { we push Flags and CS as long
  583. to cope with the IRETD
  584. and we save 6 register + 4 selectors }
  585. inc(procinfo^.para_offset,8+6*4+4*2);
  586. end;
  587. {$endif i386}
  588. { pointer to the return value ? }
  589. if ret_in_param(procinfo^.returntype.def) then
  590. begin
  591. procinfo^.return_offset:=procinfo^.para_offset;
  592. inc(procinfo^.para_offset,target_os.size_of_pointer);
  593. end;
  594. { allows to access the parameters of main functions in nested functions }
  595. aktprocsym.definition.parast.address_fixup:=procinfo^.para_offset;
  596. { when it is a value para and it needs a local copy then rename
  597. the parameter and insert a copy in the localst. This is not done
  598. for assembler procedures }
  599. if (not parse_only) and (not aktprocsym.definition.forwarddef) then
  600. aktprocsym.definition.parast.foreach_static({$ifdef FPCPROCVAR}@{$endif}checkvaluepara);
  601. { restore file pos }
  602. aktfilepos:=oldfilepos;
  603. { compile procedure when a body is needed }
  604. if (pdflags and pd_body)<>0 then
  605. begin
  606. Message1(parser_p_procedure_start,
  607. aktprocsym.definition.fullprocname);
  608. aktprocsym.definition.aliasnames.insert(aktprocsym.definition.mangledname);
  609. { set _FAIL as keyword if constructor }
  610. if (aktprocsym.definition.proctypeoption=potype_constructor) then
  611. tokeninfo^[_FAIL].keyword:=m_all;
  612. if assigned(aktprocsym.definition._class) then
  613. tokeninfo^[_SELF].keyword:=m_all;
  614. compile_proc_body(((pdflags and pd_global)<>0),assigned(oldprocinfo^._class));
  615. { reset _FAIL as normal }
  616. if (aktprocsym.definition.proctypeoption=potype_constructor) then
  617. tokeninfo^[_FAIL].keyword:=m_none;
  618. if assigned(aktprocsym.definition._class) and (lexlevel=main_program_level) then
  619. tokeninfo^[_SELF].keyword:=m_none;
  620. consume(_SEMICOLON);
  621. end;
  622. { close }
  623. codegen_doneprocedure;
  624. { Restore old state }
  625. constsymtable:=oldconstsymtable;
  626. { from now on all refernece to mangledname means
  627. that the function is already used }
  628. aktprocsym.definition.count:=true;
  629. { restore the interface order to maintain CRC values PM }
  630. if assigned(prevdef) and assigned(aktprocsym.definition.nextoverloaded) then
  631. begin
  632. stdef:=aktprocsym.definition;
  633. aktprocsym.definition:=stdef.nextoverloaded;
  634. stdef.nextoverloaded:=prevdef.nextoverloaded;
  635. prevdef.nextoverloaded:=stdef;
  636. end;
  637. aktprocsym:=oldprocsym;
  638. procprefix:=oldprefix;
  639. procinfo:=oldprocinfo;
  640. otsym:=nil;
  641. end;
  642. {****************************************************************************
  643. DECLARATION PARSING
  644. ****************************************************************************}
  645. procedure read_declarations(islibrary : boolean);
  646. procedure Not_supported_for_inline(t : ttoken);
  647. begin
  648. if assigned(aktprocsym) and
  649. (pocall_inline in aktprocsym.definition.proccalloptions) then
  650. Begin
  651. Message1(parser_w_not_supported_for_inline,tokenstring(t));
  652. Message(parser_w_inlining_disabled);
  653. exclude(aktprocsym.definition.proccalloptions,pocall_inline);
  654. End;
  655. end;
  656. begin
  657. repeat
  658. case token of
  659. _LABEL:
  660. begin
  661. Not_supported_for_inline(token);
  662. label_dec;
  663. end;
  664. _CONST:
  665. begin
  666. Not_supported_for_inline(token);
  667. const_dec;
  668. end;
  669. _TYPE:
  670. begin
  671. Not_supported_for_inline(token);
  672. type_dec;
  673. end;
  674. _VAR:
  675. var_dec;
  676. _THREADVAR:
  677. threadvar_dec;
  678. _CONSTRUCTOR,_DESTRUCTOR,
  679. _FUNCTION,_PROCEDURE,_OPERATOR,_CLASS:
  680. begin
  681. Not_supported_for_inline(token);
  682. read_proc;
  683. end;
  684. _RESOURCESTRING:
  685. resourcestring_dec;
  686. _EXPORTS:
  687. begin
  688. Not_supported_for_inline(token);
  689. { here we should be at lexlevel 1, no ? PM }
  690. if (lexlevel<>main_program_level) or
  691. (current_module.is_unit) then
  692. begin
  693. Message(parser_e_syntax_error);
  694. consume_all_until(_SEMICOLON);
  695. end
  696. else if islibrary or (target_info.target=target_i386_WIN32)
  697. or (target_info.target=target_i386_Netware) then // AD
  698. read_exports;
  699. end
  700. else break;
  701. end;
  702. until false;
  703. end;
  704. procedure read_interface_declarations;
  705. begin
  706. {Since the body is now parsed at lexlevel 1, and the declarations
  707. must be parsed at the same lexlevel we increase the lexlevel.}
  708. inc(lexlevel);
  709. repeat
  710. case token of
  711. _CONST :
  712. const_dec;
  713. _TYPE :
  714. type_dec;
  715. _VAR :
  716. var_dec;
  717. _THREADVAR :
  718. threadvar_dec;
  719. _RESOURCESTRING:
  720. resourcestring_dec;
  721. _FUNCTION,
  722. _PROCEDURE,
  723. _OPERATOR :
  724. read_proc;
  725. else
  726. break;
  727. end;
  728. until false;
  729. dec(lexlevel);
  730. end;
  731. end.
  732. {
  733. $Log$
  734. Revision 1.28 2001-04-13 17:59:03 peter
  735. * don't generate code when there is already an error
  736. Revision 1.27 2001/04/13 01:22:13 peter
  737. * symtable change to classes
  738. * range check generation and errors fixed, make cycle DEBUG=1 works
  739. * memory leaks fixed
  740. Revision 1.26 2001/04/02 21:20:34 peter
  741. * resulttype rewrite
  742. Revision 1.25 2001/02/26 19:44:53 peter
  743. * merged generic m68k updates from fixes branch
  744. Revision 1.24 2000/12/25 00:07:27 peter
  745. + new tlinkedlist class (merge of old tstringqueue,tcontainer and
  746. tlinkedlist objects)
  747. Revision 1.23 2000/11/29 00:30:37 florian
  748. * unused units removed from uses clause
  749. * some changes for widestrings
  750. Revision 1.22 2000/11/08 16:38:24 jonas
  751. * if a procedure uses exceptions (be it implicit or explicit), the
  752. usedregisters are set to all (because FPC_POPADDRSTACK doesn't save
  753. any registers) ("merged", fixes make cycle woth -Or)
  754. Revision 1.21 2000/11/01 23:04:38 peter
  755. * tprocdef.fullprocname added for better casesensitve writing of
  756. procedures
  757. Revision 1.20 2000/10/31 22:02:50 peter
  758. * symtable splitted, no real code changes
  759. Revision 1.19 2000/10/24 22:21:25 peter
  760. * set usedregisters after writing entry and exit code (merged)
  761. Revision 1.18 2000/10/21 18:16:12 florian
  762. * a lot of changes:
  763. - basic dyn. array support
  764. - basic C++ support
  765. - some work for interfaces done
  766. ....
  767. Revision 1.17 2000/10/15 07:47:51 peter
  768. * unit names and procedure names are stored mixed case
  769. Revision 1.16 2000/10/14 10:14:52 peter
  770. * moehrendorf oct 2000 rewrite
  771. Revision 1.15 2000/09/24 21:33:47 peter
  772. * message updates merges
  773. Revision 1.14 2000/09/24 21:19:51 peter
  774. * delphi compile fixes
  775. Revision 1.13 2000/09/24 15:06:24 peter
  776. * use defines.inc
  777. Revision 1.12 2000/09/10 20:11:07 peter
  778. * overload checking in implementation removed (merged)
  779. Revision 1.11 2000/09/04 20:15:19 peter
  780. * fixed operator overloading
  781. Revision 1.10 2000/08/27 16:11:52 peter
  782. * moved some util functions from globals,cobjects to cutils
  783. * splitted files into finput,fmodule
  784. Revision 1.9 2000/08/16 18:33:54 peter
  785. * splitted namedobjectitem.next into indexnext and listnext so it
  786. can be used in both lists
  787. * don't allow "word = word" type definitions (merged)
  788. Revision 1.8 2000/08/13 12:54:56 peter
  789. * class member decl wrong then no other error after it
  790. * -vb has now also line numbering
  791. * -vb is also used for interface/implementation different decls and
  792. doesn't list the current function (merged)
  793. Revision 1.7 2000/08/08 19:28:57 peter
  794. * memdebug/memory patches (merged)
  795. * only once illegal directive (merged)
  796. Revision 1.6 2000/08/06 19:39:28 peter
  797. * default parameters working !
  798. Revision 1.5 2000/08/06 14:17:15 peter
  799. * overload fixes (merged)
  800. Revision 1.4 2000/07/30 17:04:43 peter
  801. * merged fixes
  802. Revision 1.3 2000/07/13 12:08:27 michael
  803. + patched to 1.1.0 with former 1.09patch from peter
  804. Revision 1.2 2000/07/13 11:32:46 michael
  805. + removed logs
  806. }