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. uses
  22. cobjects;
  23. procedure compile_proc_body(make_global,parent_has_class:boolean);
  24. { reads the declaration blocks }
  25. procedure read_declarations(islibrary : boolean);
  26. { reads declarations in the interface part of a unit }
  27. procedure read_interface_declarations;
  28. implementation
  29. uses
  30. { common }
  31. cutils,
  32. { global }
  33. globtype,globals,tokens,verbose,
  34. systems,
  35. { aasm }
  36. cpubase,aasm,
  37. { symtable }
  38. symconst,symbase,symtype,symdef,symsym,symtable,types,
  39. ppu,fmodule,
  40. { pass 1 }
  41. node,
  42. nbas,
  43. { pass 2 }
  44. {$ifndef NOPASS2}
  45. pass_2,
  46. {$endif}
  47. { parser }
  48. scanner,
  49. pbase,pstatmnt,pdecl,pdecsub,pexports,
  50. { codegen }
  51. tgcpu,hcodegen
  52. {$ifdef newcg}
  53. ,cgbase
  54. ,cgobj
  55. {$ifndef NOOPT}
  56. ,aopt
  57. {$endif}
  58. {$else}
  59. ,temp_gen
  60. {$ifdef i386}
  61. ,cgai386
  62. {$ifndef NOOPT}
  63. ,aopt386
  64. {$endif}
  65. {$endif}
  66. {$endif}
  67. ;
  68. {****************************************************************************
  69. PROCEDURE/FUNCTION BODY PARSING
  70. ****************************************************************************}
  71. function block(islibrary : boolean) : tnode;
  72. var
  73. funcretsym : pfuncretsym;
  74. storepos : tfileposinfo;
  75. begin
  76. { do we have an assembler block without the po_assembler?
  77. we should allow this for Delphi compatibility (PFV) }
  78. if (token=_ASM) and (m_delphi in aktmodeswitches) then
  79. include(aktprocsym^.definition^.procoptions,po_assembler);
  80. { Handle assembler block different }
  81. if (po_assembler in aktprocsym^.definition^.procoptions) then
  82. begin
  83. read_declarations(false);
  84. block:=assembler_block;
  85. exit;
  86. end;
  87. if procinfo^.returntype.def<>pdef(voiddef) then
  88. begin
  89. { if the current is a function aktprocsym is non nil }
  90. { and there is a local symtable set }
  91. storepos:=akttokenpos;
  92. akttokenpos:=aktprocsym^.fileinfo;
  93. funcretsym:=new(pfuncretsym,init(aktprocsym^.name,procinfo));
  94. { insert in local symtable }
  95. symtablestack^.insert(funcretsym);
  96. akttokenpos:=storepos;
  97. if ret_in_acc(procinfo^.returntype.def) or (procinfo^.returntype.def^.deftype=floatdef) then
  98. procinfo^.return_offset:=-funcretsym^.address;
  99. procinfo^.funcretsym:=funcretsym;
  100. { insert result also if support is on }
  101. if (m_result in aktmodeswitches) then
  102. begin
  103. procinfo^.resultfuncretsym:=new(pfuncretsym,init('RESULT',procinfo));
  104. symtablestack^.insert(procinfo^.resultfuncretsym);
  105. end;
  106. end;
  107. read_declarations(islibrary);
  108. { temporary space is set, while the BEGIN of the procedure }
  109. if (symtablestack^.symtabletype=localsymtable) then
  110. procinfo^.firsttemp_offset := -symtablestack^.datasize
  111. else
  112. procinfo^.firsttemp_offset := 0;
  113. { space for the return value }
  114. { !!!!! this means that we can not set the return value
  115. in a subfunction !!!!! }
  116. { because we don't know yet where the address is }
  117. if procinfo^.returntype.def<>pdef(voiddef) then
  118. begin
  119. if ret_in_acc(procinfo^.returntype.def) or (procinfo^.returntype.def^.deftype=floatdef) then
  120. { if (procinfo^.retdef^.deftype=orddef) or
  121. (procinfo^.retdef^.deftype=pointerdef) or
  122. (procinfo^.retdef^.deftype=enumdef) or
  123. (procinfo^.retdef^.deftype=procvardef) or
  124. (procinfo^.retdef^.deftype=floatdef) or
  125. (
  126. (procinfo^.retdef^.deftype=setdef) and
  127. (psetdef(procinfo^.retdef)^.settype=smallset)
  128. ) then }
  129. begin
  130. { the space has been set in the local symtable }
  131. procinfo^.return_offset:=-funcretsym^.address;
  132. if ((procinfo^.flags and pi_operator)<>0) and
  133. assigned(opsym) then
  134. {opsym^.address:=procinfo^.para_offset; is wrong PM }
  135. opsym^.address:=-procinfo^.return_offset;
  136. { eax is modified by a function }
  137. {$ifndef newcg}
  138. {$ifdef i386}
  139. usedinproc:=usedinproc or ($80 shr byte(R_EAX));
  140. if is_64bitint(procinfo^.returntype.def) then
  141. usedinproc:=usedinproc or ($80 shr byte(R_EDX))
  142. {$endif}
  143. {$ifdef m68k}
  144. usedinproc:=usedinproc or ($800 shr word(R_D0));
  145. if is_64bitint(procinfo^.retdef) then
  146. usedinproc:=usedinproc or ($800 shr byte(R_D1))
  147. {$endif}
  148. {$endif newcg}
  149. end;
  150. end;
  151. {Unit initialization?.}
  152. if (lexlevel=unit_init_level) and (current_module.is_unit)
  153. or islibrary then
  154. begin
  155. if (token=_END) then
  156. begin
  157. consume(_END);
  158. { We need at least a node, else the entry/exit code is not
  159. generated and thus no PASCALMAIN symbol which we need (PFV) }
  160. if islibrary then
  161. block:=cnothingnode.create
  162. else
  163. block:=nil;
  164. end
  165. else
  166. begin
  167. if token=_INITIALIZATION then
  168. begin
  169. current_module.flags:=current_module.flags or uf_init;
  170. block:=statement_block(_INITIALIZATION);
  171. end
  172. else if (token=_FINALIZATION) then
  173. begin
  174. if (current_module.flags and uf_finalize)<>0 then
  175. block:=statement_block(_FINALIZATION)
  176. else
  177. begin
  178. { can we allow no INITIALIZATION for DLL ??
  179. I think it should work PM }
  180. block:=nil;
  181. exit;
  182. end;
  183. end
  184. else
  185. begin
  186. current_module.flags:=current_module.flags or uf_init;
  187. block:=statement_block(_BEGIN);
  188. end;
  189. end;
  190. end
  191. else
  192. block:=statement_block(_BEGIN);
  193. end;
  194. {****************************************************************************
  195. PROCEDURE/FUNCTION COMPILING
  196. ****************************************************************************}
  197. procedure compile_proc_body(make_global,parent_has_class:boolean);
  198. {
  199. Compile the body of a procedure
  200. }
  201. var
  202. oldexitlabel,oldexit2label : pasmlabel;
  203. oldfaillabel,oldquickexitlabel:Pasmlabel;
  204. _class,hp:Pobjectdef;
  205. { switches can change inside the procedure }
  206. entryswitches, exitswitches : tlocalswitches;
  207. oldaktmaxfpuregisters,localmaxfpuregisters : longint;
  208. { code for the subroutine as tree }
  209. code:tnode;
  210. { size of the local strackframe }
  211. stackframe:longint;
  212. { true when no stackframe is required }
  213. nostackframe:boolean;
  214. { number of bytes which have to be cleared by RET }
  215. parasize:longint;
  216. { filepositions }
  217. entrypos,
  218. savepos,
  219. exitpos : tfileposinfo;
  220. begin
  221. { calculate the lexical level }
  222. inc(lexlevel);
  223. if lexlevel>32 then
  224. Message(parser_e_too_much_lexlevel);
  225. { static is also important for local procedures !! }
  226. if (po_staticmethod in aktprocsym^.definition^.procoptions) then
  227. allow_only_static:=true
  228. else if (lexlevel=normal_function_level) then
  229. allow_only_static:=false;
  230. { save old labels }
  231. oldexitlabel:=aktexitlabel;
  232. oldexit2label:=aktexit2label;
  233. oldquickexitlabel:=quickexitlabel;
  234. oldfaillabel:=faillabel;
  235. { get new labels }
  236. getlabel(aktexitlabel);
  237. getlabel(aktexit2label);
  238. { exit for fail in constructors }
  239. if (aktprocsym^.definition^.proctypeoption=potype_constructor) then
  240. begin
  241. getlabel(faillabel);
  242. getlabel(quickexitlabel);
  243. end;
  244. { reset break and continue labels }
  245. block_type:=bt_general;
  246. aktbreaklabel:=nil;
  247. aktcontinuelabel:=nil;
  248. { insert symtables for the class, by only if it is no nested function }
  249. if assigned(procinfo^._class) and not(parent_has_class) then
  250. begin
  251. { insert them in the reverse order ! }
  252. hp:=nil;
  253. repeat
  254. _class:=procinfo^._class;
  255. while _class^.childof<>hp do
  256. _class:=_class^.childof;
  257. hp:=_class;
  258. _class^.symtable^.next:=symtablestack;
  259. symtablestack:=_class^.symtable;
  260. until hp=procinfo^._class;
  261. end;
  262. { insert parasymtable in symtablestack}
  263. { only if lexlevel > 1 !!! global symtable should be right after staticsymtazble
  264. for checking of same names used in interface and implementation !! }
  265. if lexlevel>=normal_function_level then
  266. begin
  267. aktprocsym^.definition^.parast^.next:=symtablestack;
  268. symtablestack:=aktprocsym^.definition^.parast;
  269. symtablestack^.symtablelevel:=lexlevel;
  270. end;
  271. { insert localsymtable in symtablestack}
  272. aktprocsym^.definition^.localst^.next:=symtablestack;
  273. symtablestack:=aktprocsym^.definition^.localst;
  274. symtablestack^.symtablelevel:=lexlevel;
  275. { constant symbols are inserted in this symboltable }
  276. constsymtable:=symtablestack;
  277. { reset the temporary memory }
  278. cleartempgen;
  279. {$ifdef newcg}
  280. tg.usedinproc:=[];
  281. {$else newcg}
  282. { no registers are used }
  283. usedinproc:=0;
  284. {$endif newcg}
  285. { save entry info }
  286. entrypos:=aktfilepos;
  287. entryswitches:=aktlocalswitches;
  288. localmaxfpuregisters:=aktmaxfpuregisters;
  289. { parse the code ... }
  290. code:=block(current_module.islibrary);
  291. { get a better entry point }
  292. if assigned(code) then
  293. entrypos:=code.fileinfo;
  294. { save exit info }
  295. exitswitches:=aktlocalswitches;
  296. exitpos:=last_endtoken_filepos;
  297. { save current filepos }
  298. savepos:=aktfilepos;
  299. {When we are called to compile the body of a unit, aktprocsym should
  300. point to the unit initialization. If the unit has no initialization,
  301. aktprocsym=nil. But in that case code=nil. hus we should check for
  302. code=nil, when we use aktprocsym.}
  303. { set the framepointer to esp for assembler functions }
  304. { but only if the are no local variables }
  305. { already done in assembler_block }
  306. {$ifdef newcg}
  307. tg.setfirsttemp(procinfo^.firsttemp_offset);
  308. {$else newcg}
  309. setfirsttemp(procinfo^.firsttemp_offset);
  310. {$endif newcg}
  311. { ... and generate assembler }
  312. { but set the right switches for entry !! }
  313. aktlocalswitches:=entryswitches;
  314. oldaktmaxfpuregisters:=aktmaxfpuregisters;
  315. aktmaxfpuregisters:=localmaxfpuregisters;
  316. {$ifndef NOPASS2}
  317. if assigned(code) then
  318. generatecode(code);
  319. { set switches to status at end of procedure }
  320. aktlocalswitches:=exitswitches;
  321. if assigned(code) then
  322. begin
  323. aktprocsym^.definition^.code:=code;
  324. { the procedure is now defined }
  325. aktprocsym^.definition^.forwarddef:=false;
  326. end;
  327. {$ifdef newcg}
  328. stackframe:=tg.gettempsize;
  329. {$else newcg}
  330. stackframe:=gettempsize;
  331. {$endif newcg}
  332. { first generate entry code with the correct position and switches }
  333. aktfilepos:=entrypos;
  334. aktlocalswitches:=entryswitches;
  335. {$ifdef newcg}
  336. if assigned(code) then
  337. cg^.g_entrycode(procinfo^.aktentrycode,proc_names,make_global,stackframe,parasize,nostackframe,false);
  338. {$else newcg}
  339. if assigned(code) then
  340. genentrycode(procinfo^.aktentrycode,make_global,stackframe,parasize,nostackframe,false);
  341. {$endif newcg}
  342. { FPC_POPADDRSTACK destroys all registers (JM) }
  343. if (procinfo^.flags and (pi_needs_implicit_finally or pi_uses_exceptions)) <> 0 then
  344. usedinproc := $ff;
  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(Tai_section.Create(sec_data));
  376. procinfo^.aktproccode.concatlist(procinfo^.aktlocaldata);
  377. procinfo^.aktproccode.concat(Tai_section.Create(sec_code));
  378. end;
  379. { now we can insert a cut }
  380. if (cs_create_smart in aktmoduleswitches) then
  381. codeSegment.concat(Tai_cut.Create);
  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. pdflags : word;
  506. prevdef,stdef : pprocdef;
  507. begin
  508. { save old state }
  509. oldprocsym:=aktprocsym;
  510. oldprefix:=procprefix;
  511. oldconstsymtable:=constsymtable;
  512. oldprocinfo:=procinfo;
  513. { create a new procedure }
  514. codegen_newprocedure;
  515. with procinfo^ do
  516. begin
  517. parent:=oldprocinfo;
  518. { clear flags }
  519. flags:=0;
  520. { standard frame pointer }
  521. framepointer:=frame_pointer;
  522. { funcret_is_valid:=false; }
  523. funcret_state:=vs_declared;
  524. { is this a nested function of a method ? }
  525. if assigned(oldprocinfo) then
  526. _class:=oldprocinfo^._class;
  527. end;
  528. parse_proc_dec;
  529. procinfo^.sym:=aktprocsym;
  530. procinfo^.def:=aktprocsym^.definition;
  531. { set the default function options }
  532. if parse_only then
  533. begin
  534. aktprocsym^.definition^.forwarddef:=true;
  535. { set also the interface flag, for better error message when the
  536. implementation doesn't much this header }
  537. aktprocsym^.definition^.interfacedef:=true;
  538. pdflags:=pd_interface;
  539. end
  540. else
  541. begin
  542. pdflags:=pd_body;
  543. if current_module.in_implementation then
  544. pdflags:=pdflags or pd_implemen;
  545. if (not current_module.is_unit) or (cs_create_smart in aktmoduleswitches) then
  546. pdflags:=pdflags or pd_global;
  547. procinfo^.exported:=false;
  548. aktprocsym^.definition^.forwarddef:=false;
  549. end;
  550. { parse the directives that may follow }
  551. inc(lexlevel);
  552. parse_proc_directives(pdflags);
  553. dec(lexlevel);
  554. { set aktfilepos to the beginning of the function declaration }
  555. oldfilepos:=aktfilepos;
  556. aktfilepos:=aktprocsym^.definition^.fileinfo;
  557. { search for forward declarations }
  558. if not check_identical_proc(prevdef) then
  559. begin
  560. { A method must be forward defined (in the object declaration) }
  561. if assigned(procinfo^._class) and (not assigned(oldprocinfo^._class)) then
  562. begin
  563. Message1(parser_e_header_dont_match_any_member,
  564. aktprocsym^.definition^.fullprocname);
  565. aktprocsym^.write_parameter_lists(aktprocsym^.definition);
  566. end
  567. else
  568. begin
  569. { Give a better error if there is a forward def in the interface and only
  570. a single implementation }
  571. if (not aktprocsym^.definition^.forwarddef) and
  572. assigned(aktprocsym^.definition^.nextoverloaded) and
  573. aktprocsym^.definition^.nextoverloaded^.forwarddef and
  574. aktprocsym^.definition^.nextoverloaded^.interfacedef and
  575. not(assigned(aktprocsym^.definition^.nextoverloaded^.nextoverloaded)) then
  576. begin
  577. Message1(parser_e_header_dont_match_forward,
  578. aktprocsym^.definition^.fullprocname);
  579. aktprocsym^.write_parameter_lists(aktprocsym^.definition);
  580. end
  581. else
  582. begin
  583. { check the global flag }
  584. if (procinfo^.flags and pi_is_global)<>0 then
  585. Message(parser_e_overloaded_must_be_all_global);
  586. end;
  587. end;
  588. end;
  589. { set return type here, becuase the aktprocsym^.definition can be
  590. changed by check_identical_proc (PFV) }
  591. procinfo^.returntype.def:=aktprocsym^.definition^.rettype.def;
  592. {$ifdef i386}
  593. if (po_interrupt in aktprocsym^.definition^.procoptions) then
  594. begin
  595. { we push Flags and CS as long
  596. to cope with the IRETD
  597. and we save 6 register + 4 selectors }
  598. inc(procinfo^.para_offset,8+6*4+4*2);
  599. end;
  600. {$endif i386}
  601. { pointer to the return value ? }
  602. if ret_in_param(procinfo^.returntype.def) then
  603. begin
  604. procinfo^.return_offset:=procinfo^.para_offset;
  605. inc(procinfo^.para_offset,target_os.size_of_pointer);
  606. end;
  607. { allows to access the parameters of main functions in nested functions }
  608. aktprocsym^.definition^.parast^.address_fixup:=procinfo^.para_offset;
  609. { when it is a value para and it needs a local copy then rename
  610. the parameter and insert a copy in the localst. This is not done
  611. for assembler procedures }
  612. if (not parse_only) and (not aktprocsym^.definition^.forwarddef) then
  613. aktprocsym^.definition^.parast^.foreach({$ifdef FPCPROCVAR}@{$endif}checkvaluepara);
  614. { restore file pos }
  615. aktfilepos:=oldfilepos;
  616. { compile procedure when a body is needed }
  617. if (pdflags and pd_body)<>0 then
  618. begin
  619. Message1(parser_p_procedure_start,
  620. aktprocsym^.definition^.fullprocname);
  621. aktprocsym^.definition^.aliasnames.insert(aktprocsym^.definition^.mangledname);
  622. { set _FAIL as keyword if constructor }
  623. if (aktprocsym^.definition^.proctypeoption=potype_constructor) then
  624. tokeninfo^[_FAIL].keyword:=m_all;
  625. if assigned(aktprocsym^.definition^._class) then
  626. tokeninfo^[_SELF].keyword:=m_all;
  627. compile_proc_body(((pdflags and pd_global)<>0),assigned(oldprocinfo^._class));
  628. { reset _FAIL as normal }
  629. if (aktprocsym^.definition^.proctypeoption=potype_constructor) then
  630. tokeninfo^[_FAIL].keyword:=m_none;
  631. if assigned(aktprocsym^.definition^._class) and (lexlevel=main_program_level) then
  632. tokeninfo^[_SELF].keyword:=m_none;
  633. consume(_SEMICOLON);
  634. end;
  635. { close }
  636. codegen_doneprocedure;
  637. { Restore old state }
  638. constsymtable:=oldconstsymtable;
  639. { from now on all refernece to mangledname means
  640. that the function is already used }
  641. aktprocsym^.definition^.count:=true;
  642. { restore the interface order to maintain CRC values PM }
  643. if assigned(prevdef) and assigned(aktprocsym^.definition^.nextoverloaded) then
  644. begin
  645. stdef:=aktprocsym^.definition;
  646. aktprocsym^.definition:=stdef^.nextoverloaded;
  647. stdef^.nextoverloaded:=prevdef^.nextoverloaded;
  648. prevdef^.nextoverloaded:=stdef;
  649. end;
  650. aktprocsym:=oldprocsym;
  651. procprefix:=oldprefix;
  652. procinfo:=oldprocinfo;
  653. opsym:=nil;
  654. end;
  655. {****************************************************************************
  656. DECLARATION PARSING
  657. ****************************************************************************}
  658. procedure read_declarations(islibrary : boolean);
  659. procedure Not_supported_for_inline(t : ttoken);
  660. begin
  661. if assigned(aktprocsym) and
  662. (pocall_inline in aktprocsym^.definition^.proccalloptions) then
  663. Begin
  664. Message1(parser_w_not_supported_for_inline,tokenstring(t));
  665. Message(parser_w_inlining_disabled);
  666. exclude(aktprocsym^.definition^.proccalloptions,pocall_inline);
  667. End;
  668. end;
  669. begin
  670. repeat
  671. case token of
  672. _LABEL:
  673. begin
  674. Not_supported_for_inline(token);
  675. label_dec;
  676. end;
  677. _CONST:
  678. begin
  679. Not_supported_for_inline(token);
  680. const_dec;
  681. end;
  682. _TYPE:
  683. begin
  684. Not_supported_for_inline(token);
  685. type_dec;
  686. end;
  687. _VAR:
  688. var_dec;
  689. _THREADVAR:
  690. threadvar_dec;
  691. _CONSTRUCTOR,_DESTRUCTOR,
  692. _FUNCTION,_PROCEDURE,_OPERATOR,_CLASS:
  693. begin
  694. Not_supported_for_inline(token);
  695. read_proc;
  696. end;
  697. _RESOURCESTRING:
  698. resourcestring_dec;
  699. _EXPORTS:
  700. begin
  701. Not_supported_for_inline(token);
  702. { here we should be at lexlevel 1, no ? PM }
  703. if (lexlevel<>main_program_level) or
  704. (current_module.is_unit) then
  705. begin
  706. Message(parser_e_syntax_error);
  707. consume_all_until(_SEMICOLON);
  708. end
  709. else if islibrary or (target_info.target=target_i386_WIN32)
  710. or (target_info.target=target_i386_Netware) then // AD
  711. read_exports;
  712. end
  713. else break;
  714. end;
  715. until false;
  716. end;
  717. procedure read_interface_declarations;
  718. begin
  719. {Since the body is now parsed at lexlevel 1, and the declarations
  720. must be parsed at the same lexlevel we increase the lexlevel.}
  721. inc(lexlevel);
  722. repeat
  723. case token of
  724. _CONST :
  725. const_dec;
  726. _TYPE :
  727. type_dec;
  728. _VAR :
  729. var_dec;
  730. _THREADVAR :
  731. threadvar_dec;
  732. _RESOURCESTRING:
  733. resourcestring_dec;
  734. _FUNCTION,
  735. _PROCEDURE,
  736. _OPERATOR :
  737. read_proc;
  738. else
  739. break;
  740. end;
  741. until false;
  742. dec(lexlevel);
  743. end;
  744. end.
  745. {
  746. $Log$
  747. Revision 1.24 2000-12-25 00:07:27 peter
  748. + new tlinkedlist class (merge of old tstringqueue,tcontainer and
  749. tlinkedlist objects)
  750. Revision 1.23 2000/11/29 00:30:37 florian
  751. * unused units removed from uses clause
  752. * some changes for widestrings
  753. Revision 1.22 2000/11/08 16:38:24 jonas
  754. * if a procedure uses exceptions (be it implicit or explicit), the
  755. usedregisters are set to all (because FPC_POPADDRSTACK doesn't save
  756. any registers) ("merged", fixes make cycle woth -Or)
  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. }