psub.pas 32 KB

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