psub.pas 34 KB

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