psub.pas 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001
  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. {$ifdef powerpc}
  21. {$define newcg}
  22. {$endif powerpc}
  23. interface
  24. procedure compile_proc_body(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,cclasses,
  33. { global }
  34. globtype,globals,tokens,verbose,comphook,
  35. systems,
  36. { aasm }
  37. cpubase,aasm,
  38. { symtable }
  39. symconst,symbase,symdef,symsym,symtable,types,
  40. ppu,fmodule,
  41. { pass 1 }
  42. node,
  43. nbas,
  44. { pass 2 }
  45. {$ifndef NOPASS2}
  46. pass_1,pass_2,
  47. {$endif}
  48. { parser }
  49. scanner,
  50. pbase,pstatmnt,pdecl,pdecsub,pexports,
  51. { codegen }
  52. tgcpu,cgbase,
  53. temp_gen,
  54. cga
  55. {$ifndef NOOPT}
  56. {$ifdef i386}
  57. ,aopt386
  58. {$else i386}
  59. ,aoptcpu
  60. {$endif i386}
  61. {$endif}
  62. {$ifdef newcg}
  63. ,cgobj
  64. {$endif newcg}
  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(aktprocdef.procoptions,po_assembler);
  77. { Handle assembler block different }
  78. if (po_assembler in aktprocdef.procoptions) then
  79. begin
  80. read_declarations(false);
  81. block:=assembler_block;
  82. exit;
  83. end;
  84. if not is_void(aktprocdef.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. aktprocdef.funcretsym:=tfuncretsym.create(aktprocsym.name,aktprocdef.rettype);
  91. { insert in local symtable }
  92. symtablestack.insert(aktprocdef.funcretsym);
  93. akttokenpos:=storepos;
  94. if ret_in_acc(aktprocdef.rettype.def) or
  95. (aktprocdef.rettype.def.deftype=floatdef) then
  96. procinfo^.return_offset:=-tfuncretsym(aktprocdef.funcretsym).address;
  97. { insert result also if support is on }
  98. if (m_result in aktmodeswitches) then
  99. begin
  100. aktprocdef.resultfuncretsym:=tfuncretsym.create('RESULT',aktprocdef.rettype);
  101. symtablestack.insert(aktprocdef.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(aktprocdef.rettype.def) then
  115. begin
  116. if ret_in_acc(aktprocdef.rettype.def) or (aktprocdef.rettype.def.deftype=floatdef) then
  117. begin
  118. { the space has been set in the local symtable }
  119. procinfo^.return_offset:=-tfuncretsym(aktprocdef.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(aktprocdef.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(aktprocdef.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 aktprocdef.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 (aktprocdef.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. aktprocdef.parast.next:=symtablestack;
  255. symtablestack:=aktprocdef.parast;
  256. symtablestack.symtablelevel:=lexlevel;
  257. end;
  258. { insert localsymtable in symtablestack}
  259. aktprocdef.localst.next:=symtablestack;
  260. symtablestack:=aktprocdef.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. {$ifdef POWERPC}
  268. tgcpu.usedinproc:=0;
  269. {$else POWERPC}
  270. tg.usedinproc:=[];
  271. {$endif POWERPC}
  272. {$else newcg}
  273. {$ifdef i386}
  274. { no registers are used }
  275. usedinproc:=0;
  276. {$else}
  277. usedinproc := [];
  278. {$endif}
  279. {$endif newcg}
  280. { save entry info }
  281. entrypos:=aktfilepos;
  282. entryswitches:=aktlocalswitches;
  283. localmaxfpuregisters:=aktmaxfpuregisters;
  284. { parse the code ... }
  285. code:=block(current_module.islibrary);
  286. { get a better entry point }
  287. if assigned(code) then
  288. entrypos:=code.fileinfo;
  289. { save exit info }
  290. exitswitches:=aktlocalswitches;
  291. exitpos:=last_endtoken_filepos;
  292. { save current filepos }
  293. savepos:=aktfilepos;
  294. {When we are called to compile the body of a unit, aktprocsym should
  295. point to the unit initialization. If the unit has no initialization,
  296. aktprocsym=nil. But in that case code=nil. hus we should check for
  297. code=nil, when we use aktprocsym.}
  298. { set the framepointer to esp for assembler functions }
  299. { but only if the are no local variables }
  300. { already done in assembler_block }
  301. {$ifdef newcg}
  302. setfirsttemp(procinfo^.firsttemp_offset);
  303. {$else newcg}
  304. setfirsttemp(procinfo^.firsttemp_offset);
  305. {$endif newcg}
  306. { ... and generate assembler }
  307. { but set the right switches for entry !! }
  308. aktlocalswitches:=entryswitches;
  309. oldaktmaxfpuregisters:=aktmaxfpuregisters;
  310. aktmaxfpuregisters:=localmaxfpuregisters;
  311. if assigned(code) then
  312. begin
  313. { the procedure is now defined }
  314. aktprocdef.forwarddef:=false;
  315. { only generate the code if no type errors are found, else
  316. finish at least the type checking pass }
  317. {$ifndef NOPASS2}
  318. if (status.errorcount=0) then
  319. begin
  320. generatecode(code);
  321. aktprocdef.code:=code;
  322. {$ifdef newcg}
  323. stackframe:=gettempsize;
  324. {$else newcg}
  325. stackframe:=gettempsize;
  326. {$endif newcg}
  327. { first generate entry code with the correct position and switches }
  328. aktfilepos:=entrypos;
  329. aktlocalswitches:=entryswitches;
  330. {$ifdef newcg}
  331. cg^.g_entrycode(procinfo^.aktentrycode,proc_names,make_global,stackframe,parasize,nostackframe,false);
  332. {$else newcg}
  333. genentrycode(procinfo^.aktentrycode,make_global,stackframe,parasize,nostackframe,false);
  334. {$endif newcg}
  335. { FPC_POPADDRSTACK destroys all registers (JM) }
  336. if (procinfo^.flags and (pi_needs_implicit_finally or pi_uses_exceptions)) <> 0 then
  337. begin
  338. {$ifdef i386}
  339. usedinproc := $ff;
  340. {$else}
  341. usedinproc := ALL_REGISTERS;
  342. {$endif}
  343. end;
  344. { now generate exit code with the correct position and switches }
  345. aktfilepos:=exitpos;
  346. aktlocalswitches:=exitswitches;
  347. {$ifdef newcg}
  348. cg^.g_exitcode(procinfo^.aktexitcode,parasize,nostackframe,false);
  349. {$else newcg}
  350. genexitcode(procinfo^.aktexitcode,parasize,nostackframe,false);
  351. {$endif newcg}
  352. { now all the registers used are known }
  353. {$ifdef newcg}
  354. aktprocdef.usedregisters:=tg.usedinproc;
  355. {$else newcg}
  356. aktprocdef.usedregisters:=usedinproc;
  357. {$endif newcg}
  358. procinfo^.aktproccode.insertlist(procinfo^.aktentrycode);
  359. procinfo^.aktproccode.concatlist(procinfo^.aktexitcode);
  360. {$ifdef i386}
  361. {$ifndef NoOpt}
  362. if (cs_optimize in aktglobalswitches) and
  363. { do not optimize pure assembler procedures }
  364. ((procinfo^.flags and pi_is_assembler)=0) then
  365. Optimize(procinfo^.aktproccode);
  366. {$endif NoOpt}
  367. {$endif i386}
  368. { save local data (casetable) also in the same file }
  369. if assigned(procinfo^.aktlocaldata) and
  370. (not procinfo^.aktlocaldata.empty) then
  371. begin
  372. procinfo^.aktproccode.concat(Tai_section.Create(sec_data));
  373. procinfo^.aktproccode.concatlist(procinfo^.aktlocaldata);
  374. procinfo^.aktproccode.concat(Tai_section.Create(sec_code));
  375. end;
  376. { add the procedure to the codesegment }
  377. if (cs_create_smart in aktmoduleswitches) then
  378. codeSegment.concat(Tai_cut.Create);
  379. codeSegment.concatlist(procinfo^.aktproccode);
  380. end
  381. else
  382. do_resulttypepass(code);
  383. {$else NOPASS2}
  384. do_resulttypepass(code);
  385. {$endif NOPASS2}
  386. end;
  387. { ... remove symbol tables }
  388. if lexlevel>=normal_function_level then
  389. symtablestack:=symtablestack.next.next
  390. else
  391. symtablestack:=symtablestack.next;
  392. { ... check for unused symbols }
  393. { but only if there is no asm block }
  394. if assigned(code) then
  395. begin
  396. if (Errorcount=0) then
  397. begin
  398. { check if forwards are resolved }
  399. tstoredsymtable(aktprocdef.localst).check_forwards;
  400. { check if all labels are used }
  401. tstoredsymtable(aktprocdef.localst).checklabels;
  402. { remove cross unit overloads }
  403. tstoredsymtable(aktprocdef.localst).unchain_overloaded;
  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(aktprocdef.proctypeoption
  410. in [potype_proginit,potype_unitinit,potype_unitfinalize]) then
  411. tstoredsymtable(aktprocdef.localst).allsymbolsused;
  412. tstoredsymtable(aktprocdef.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. (aktprocdef.proccalloption<>pocall_inline) then
  424. begin
  425. if lexlevel>=normal_function_level then
  426. aktprocdef.localst.free;
  427. aktprocdef.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 (aktprocdef.proccalloption<>pocall_inline) 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:tnamedindexitem);
  464. var
  465. vs : tvarsym;
  466. s : string;
  467. begin
  468. with tvarsym(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 aktprocdef.procoptions) then
  474. begin
  475. vs:=tvarsym.create(s,vartype);
  476. vs.fileinfo:=fileinfo;
  477. vs.varspez:=varspez;
  478. aktprocdef.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. aktprocdef.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 : tprocsym;
  501. oldprocdef : tprocdef;
  502. oldprocinfo : pprocinfo;
  503. oldconstsymtable : tsymtable;
  504. oldfilepos : tfileposinfo;
  505. pdflags : word;
  506. prevdef,stdef : tprocdef;
  507. begin
  508. { save old state }
  509. oldprocdef:=aktprocdef;
  510. oldprocsym:=aktprocsym;
  511. oldprefix:=procprefix;
  512. oldconstsymtable:=constsymtable;
  513. oldprocinfo:=procinfo;
  514. { create a new procedure }
  515. codegen_newprocedure;
  516. with procinfo^ do
  517. begin
  518. parent:=oldprocinfo;
  519. { clear flags }
  520. flags:=0;
  521. { standard frame pointer }
  522. framepointer:=frame_pointer;
  523. { is this a nested function of a method ? }
  524. if assigned(oldprocinfo) then
  525. _class:=oldprocinfo^._class;
  526. end;
  527. parse_proc_dec;
  528. procinfo^.procdef:=aktprocdef;
  529. { set the default function options }
  530. if parse_only then
  531. begin
  532. aktprocdef.forwarddef:=true;
  533. { set also the interface flag, for better error message when the
  534. implementation doesn't much this header }
  535. aktprocdef.interfacedef:=true;
  536. pdflags:=pd_interface;
  537. end
  538. else
  539. begin
  540. pdflags:=pd_body;
  541. if current_module.in_implementation then
  542. pdflags:=pdflags or pd_implemen;
  543. if (not current_module.is_unit) or (cs_create_smart in aktmoduleswitches) then
  544. pdflags:=pdflags or pd_global;
  545. procinfo^.exported:=false;
  546. aktprocdef.forwarddef:=false;
  547. end;
  548. { parse the directives that may follow }
  549. inc(lexlevel);
  550. parse_proc_directives(pdflags);
  551. dec(lexlevel);
  552. { hint directives, these can be separated by semicolons here,
  553. that need to be handled here with a loop (PFV) }
  554. while try_consume_hintdirective(aktprocsym.symoptions) do
  555. Consume(_SEMICOLON);
  556. { set aktfilepos to the beginning of the function declaration }
  557. oldfilepos:=aktfilepos;
  558. aktfilepos:=aktprocdef.fileinfo;
  559. { For varargs directive also cdecl and external must be defined }
  560. if (po_varargs in aktprocdef.procoptions) then
  561. begin
  562. { check first for external in the interface, if available there
  563. then the cdecl must also be there since there is no implementation
  564. available to contain it }
  565. if parse_only then
  566. begin
  567. { if external is available, then cdecl must also be available }
  568. if (po_external in aktprocdef.procoptions) and
  569. not(aktprocdef.proccalloption in [pocall_cdecl,pocall_cppdecl]) then
  570. Message(parser_e_varargs_need_cdecl_and_external);
  571. end
  572. else
  573. begin
  574. { both must be defined now }
  575. if not(po_external in aktprocdef.procoptions) or
  576. not(aktprocdef.proccalloption in [pocall_cdecl,pocall_cppdecl]) then
  577. Message(parser_e_varargs_need_cdecl_and_external);
  578. end;
  579. end;
  580. { search for forward declarations }
  581. if not proc_add_definition(aktprocsym,aktprocdef) then
  582. begin
  583. { A method must be forward defined (in the object declaration) }
  584. if assigned(procinfo^._class) and
  585. (not assigned(oldprocinfo^._class)) then
  586. begin
  587. Message1(parser_e_header_dont_match_any_member,aktprocdef.fullprocname);
  588. aktprocsym.write_parameter_lists(aktprocdef);
  589. end
  590. else
  591. begin
  592. { Give a better error if there is a forward def in the interface and only
  593. a single implementation }
  594. if (not aktprocdef.forwarddef) and
  595. assigned(aktprocsym.defs^.next) and
  596. aktprocsym.defs^.def.forwarddef and
  597. aktprocsym.defs^.def.interfacedef and
  598. not(assigned(aktprocsym.defs^.next^.next)) then
  599. begin
  600. Message1(parser_e_header_dont_match_forward,aktprocdef.fullprocname);
  601. aktprocsym.write_parameter_lists(aktprocdef);
  602. end
  603. else
  604. begin
  605. { check the global flag, for delphi this is not
  606. required }
  607. if not(m_delphi in aktmodeswitches) and
  608. ((procinfo^.flags and pi_is_global)<>0) then
  609. Message(parser_e_overloaded_must_be_all_global);
  610. end;
  611. end;
  612. end;
  613. { update procinfo, because the aktprocdef can be
  614. changed by check_identical_proc (PFV) }
  615. procinfo^.procdef:=aktprocdef;
  616. {$ifdef i386}
  617. { add implicit pushes for interrupt routines }
  618. if (po_interrupt in aktprocdef.procoptions) then
  619. begin
  620. { we push Flags and CS as long
  621. to cope with the IRETD
  622. and we save 6 register + 4 selectors }
  623. inc(procinfo^.para_offset,8+6*4+4*2);
  624. end;
  625. {$endif i386}
  626. { pointer to the return value ? }
  627. if ret_in_param(aktprocdef.rettype.def) then
  628. begin
  629. procinfo^.return_offset:=procinfo^.para_offset;
  630. inc(procinfo^.para_offset,target_info.size_of_pointer);
  631. end;
  632. { allows to access the parameters of main functions in nested functions }
  633. aktprocdef.parast.address_fixup:=procinfo^.para_offset;
  634. { when it is a value para and it needs a local copy then rename
  635. the parameter and insert a copy in the localst. This is not done
  636. for assembler procedures }
  637. if (not parse_only) and (not aktprocdef.forwarddef) then
  638. aktprocdef.parast.foreach_static({$ifdef FPCPROCVAR}@{$endif}checkvaluepara);
  639. { restore file pos }
  640. aktfilepos:=oldfilepos;
  641. { compile procedure when a body is needed }
  642. if (pdflags and pd_body)<>0 then
  643. begin
  644. Message1(parser_p_procedure_start,
  645. aktprocdef.fullprocname);
  646. aktprocdef.aliasnames.insert(aktprocdef.mangledname);
  647. { set _FAIL as keyword if constructor }
  648. if (aktprocdef.proctypeoption=potype_constructor) then
  649. tokeninfo^[_FAIL].keyword:=m_all;
  650. if assigned(aktprocdef._class) then
  651. tokeninfo^[_SELF].keyword:=m_all;
  652. compile_proc_body(((pdflags and pd_global)<>0),assigned(oldprocinfo^._class));
  653. { reset _FAIL as normal }
  654. if (aktprocdef.proctypeoption=potype_constructor) then
  655. tokeninfo^[_FAIL].keyword:=m_none;
  656. if assigned(aktprocdef._class) and (lexlevel=main_program_level) then
  657. tokeninfo^[_SELF].keyword:=m_none;
  658. consume(_SEMICOLON);
  659. end;
  660. { close }
  661. codegen_doneprocedure;
  662. { Restore old state }
  663. constsymtable:=oldconstsymtable;
  664. { from now on all refernece to mangledname means
  665. that the function is already used }
  666. aktprocdef.count:=true;
  667. {$ifdef notused}
  668. { restore the interface order to maintain CRC values PM }
  669. if assigned(prevdef) and assigned(aktprocdef.nextoverloaded) then
  670. begin
  671. stdef:=aktprocdef;
  672. aktprocdef:=stdef.nextoverloaded;
  673. stdef.nextoverloaded:=prevdef.nextoverloaded;
  674. prevdef.nextoverloaded:=stdef;
  675. end;
  676. {$endif notused}
  677. aktprocsym:=oldprocsym;
  678. aktprocdef:=oldprocdef;
  679. procprefix:=oldprefix;
  680. procinfo:=oldprocinfo;
  681. otsym:=nil;
  682. end;
  683. {****************************************************************************
  684. DECLARATION PARSING
  685. ****************************************************************************}
  686. procedure read_declarations(islibrary : boolean);
  687. procedure Not_supported_for_inline(t : ttoken);
  688. begin
  689. if assigned(aktprocsym) and
  690. (aktprocdef.proccalloption=pocall_inline) then
  691. Begin
  692. Message1(parser_w_not_supported_for_inline,tokenstring(t));
  693. Message(parser_w_inlining_disabled);
  694. aktprocdef.proccalloption:=pocall_fpccall;
  695. End;
  696. end;
  697. begin
  698. repeat
  699. case token of
  700. _LABEL:
  701. begin
  702. Not_supported_for_inline(token);
  703. label_dec;
  704. end;
  705. _CONST:
  706. begin
  707. Not_supported_for_inline(token);
  708. const_dec;
  709. end;
  710. _TYPE:
  711. begin
  712. Not_supported_for_inline(token);
  713. type_dec;
  714. end;
  715. _VAR:
  716. var_dec;
  717. _THREADVAR:
  718. threadvar_dec;
  719. _CONSTRUCTOR,_DESTRUCTOR,
  720. _FUNCTION,_PROCEDURE,_OPERATOR,_CLASS:
  721. begin
  722. Not_supported_for_inline(token);
  723. read_proc;
  724. end;
  725. _RESOURCESTRING:
  726. resourcestring_dec;
  727. _EXPORTS:
  728. begin
  729. Not_supported_for_inline(token);
  730. { here we should be at lexlevel 1, no ? PM }
  731. if (lexlevel<>main_program_level) or
  732. (current_module.is_unit) then
  733. begin
  734. Message(parser_e_syntax_error);
  735. consume_all_until(_SEMICOLON);
  736. end
  737. else if islibrary or (target_info.target=target_i386_WIN32)
  738. or (target_info.target=target_i386_Netware) then // AD
  739. read_exports;
  740. end
  741. else break;
  742. end;
  743. until false;
  744. end;
  745. procedure read_interface_declarations;
  746. begin
  747. {Since the body is now parsed at lexlevel 1, and the declarations
  748. must be parsed at the same lexlevel we increase the lexlevel.}
  749. inc(lexlevel);
  750. repeat
  751. case token of
  752. _CONST :
  753. const_dec;
  754. _TYPE :
  755. type_dec;
  756. _VAR :
  757. var_dec;
  758. _THREADVAR :
  759. threadvar_dec;
  760. _RESOURCESTRING:
  761. resourcestring_dec;
  762. _FUNCTION,
  763. _PROCEDURE,
  764. _OPERATOR :
  765. read_proc;
  766. else
  767. break;
  768. end;
  769. until false;
  770. dec(lexlevel);
  771. end;
  772. end.
  773. {
  774. $Log$
  775. Revision 1.41 2001-11-02 22:58:06 peter
  776. * procsym definition rewrite
  777. Revision 1.40 2001/10/25 21:22:37 peter
  778. * calling convention rewrite
  779. Revision 1.39 2001/10/22 21:20:46 peter
  780. * overloaded functions don't need to be global in kylix
  781. Revision 1.38 2001/10/01 13:38:45 jonas
  782. * allow self parameter for normal procedures again (because Kylix allows
  783. it too) ("merged")
  784. Revision 1.37 2001/09/10 10:26:26 jonas
  785. * fixed web bug 1593
  786. * writing of procvar headers is more complete (mention var/const/out for
  787. paras, add "of object" if applicable)
  788. + error if declaring explicit self para as var/const
  789. * fixed mangled name of procedures which contain an explicit self para
  790. * parsing para's should be slightly faster because mangled name of
  791. procedure is only updated once instead of after parsing each para
  792. (all merged from fixes)
  793. Revision 1.36 2001/08/26 13:36:46 florian
  794. * some cg reorganisation
  795. * some PPC updates
  796. Revision 1.35 2001/08/06 21:40:47 peter
  797. * funcret moved from tprocinfo to tprocdef
  798. Revision 1.34 2001/06/04 11:53:13 peter
  799. + varargs directive
  800. Revision 1.33 2001/06/03 21:57:37 peter
  801. + hint directive parsing support
  802. Revision 1.32 2001/04/21 12:03:12 peter
  803. * m68k updates merged from fixes branch
  804. Revision 1.31 2001/04/18 22:01:57 peter
  805. * registration of targets and assemblers
  806. Revision 1.30 2001/04/14 14:05:47 peter
  807. * better skipping of secondpass if error
  808. Revision 1.29 2001/04/13 23:49:24 peter
  809. * when errors are found don't generate code, but still run the
  810. resulttype pass
  811. Revision 1.28 2001/04/13 17:59:03 peter
  812. * don't generate code when there is already an error
  813. Revision 1.27 2001/04/13 01:22:13 peter
  814. * symtable change to classes
  815. * range check generation and errors fixed, make cycle DEBUG=1 works
  816. * memory leaks fixed
  817. Revision 1.26 2001/04/02 21:20:34 peter
  818. * resulttype rewrite
  819. Revision 1.25 2001/02/26 19:44:53 peter
  820. * merged generic m68k updates from fixes branch
  821. Revision 1.24 2000/12/25 00:07:27 peter
  822. + new tlinkedlist class (merge of old tstringqueue,tcontainer and
  823. tlinkedlist objects)
  824. Revision 1.23 2000/11/29 00:30:37 florian
  825. * unused units removed from uses clause
  826. * some changes for widestrings
  827. Revision 1.22 2000/11/08 16:38:24 jonas
  828. * if a procedure uses exceptions (be it implicit or explicit), the
  829. usedregisters are set to all (because FPC_POPADDRSTACK doesn't save
  830. any registers) ("merged", fixes make cycle woth -Or)
  831. Revision 1.21 2000/11/01 23:04:38 peter
  832. * tprocdef.fullprocname added for better casesensitve writing of
  833. procedures
  834. Revision 1.20 2000/10/31 22:02:50 peter
  835. * symtable splitted, no real code changes
  836. Revision 1.19 2000/10/24 22:21:25 peter
  837. * set usedregisters after writing entry and exit code (merged)
  838. Revision 1.18 2000/10/21 18:16:12 florian
  839. * a lot of changes:
  840. - basic dyn. array support
  841. - basic C++ support
  842. - some work for interfaces done
  843. ....
  844. Revision 1.17 2000/10/15 07:47:51 peter
  845. * unit names and procedure names are stored mixed case
  846. Revision 1.16 2000/10/14 10:14:52 peter
  847. * moehrendorf oct 2000 rewrite
  848. Revision 1.15 2000/09/24 21:33:47 peter
  849. * message updates merges
  850. Revision 1.14 2000/09/24 21:19:51 peter
  851. * delphi compile fixes
  852. Revision 1.13 2000/09/24 15:06:24 peter
  853. * use defines.inc
  854. Revision 1.12 2000/09/10 20:11:07 peter
  855. * overload checking in implementation removed (merged)
  856. Revision 1.11 2000/09/04 20:15:19 peter
  857. * fixed operator overloading
  858. Revision 1.10 2000/08/27 16:11:52 peter
  859. * moved some util functions from globals,cobjects to cutils
  860. * splitted files into finput,fmodule
  861. Revision 1.9 2000/08/16 18:33:54 peter
  862. * splitted namedobjectitem.next into indexnext and listnext so it
  863. can be used in both lists
  864. * don't allow "word = word" type definitions (merged)
  865. Revision 1.8 2000/08/13 12:54:56 peter
  866. * class member decl wrong then no other error after it
  867. * -vb has now also line numbering
  868. * -vb is also used for interface/implementation different decls and
  869. doesn't list the current function (merged)
  870. Revision 1.7 2000/08/08 19:28:57 peter
  871. * memdebug/memory patches (merged)
  872. * only once illegal directive (merged)
  873. Revision 1.6 2000/08/06 19:39:28 peter
  874. * default parameters working !
  875. Revision 1.5 2000/08/06 14:17:15 peter
  876. * overload fixes (merged)
  877. Revision 1.4 2000/07/30 17:04:43 peter
  878. * merged fixes
  879. Revision 1.3 2000/07/13 12:08:27 michael
  880. + patched to 1.1.0 with former 1.09patch from peter
  881. Revision 1.2 2000/07/13 11:32:46 michael
  882. + removed logs
  883. }