pparautl.pas 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl, Daniel Mantione
  3. Helpers for dealing with subroutine parameters during parsing
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit pparautl;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. symtype,symconst,symdef;
  22. procedure insert_funcret_para(pd:tabstractprocdef);
  23. procedure insert_parentfp_para(pd:tabstractprocdef);
  24. procedure insert_self_and_vmt_para(pd:tabstractprocdef);
  25. procedure insert_funcret_local(pd:tprocdef);
  26. procedure insert_hidden_para(p:TObject;arg:pointer);
  27. procedure check_c_para(pd:Tabstractprocdef);
  28. procedure insert_struct_hidden_paras(astruct: tabstractrecorddef);
  29. type
  30. // flags of the *handle_calling_convention routines
  31. thccflag=(
  32. hcc_declaration, // declaration (as opposed to definition, i.e. interface rather than implementation)
  33. hcc_check, // perform checks and outup errors if found
  34. hcc_insert_hidden_paras // insert hidden parameters
  35. );
  36. thccflags=set of thccflag;
  37. const
  38. hcc_default_actions_intf=[hcc_declaration,hcc_check,hcc_insert_hidden_paras];
  39. hcc_default_actions_intf_struct=hcc_default_actions_intf-[hcc_insert_hidden_paras];
  40. hcc_default_actions_impl=[hcc_check,hcc_insert_hidden_paras];
  41. hcc_default_actions_parse=[hcc_check,hcc_insert_hidden_paras];
  42. PD_VIRTUAL_MUTEXCLPO = [po_interrupt,po_exports,po_overridingmethod,po_inline,po_staticmethod];
  43. { may take procdef, procvardef or defs for which is_funcref is true }
  44. procedure handle_calling_convention(pd_or_invkdef:tdef;flags:thccflags);
  45. function proc_add_definition(var currpd:tprocdef):boolean;
  46. { create "parent frame pointer" record skeleton for procdef, in which local
  47. variables and parameters from pd accessed from nested routines can be
  48. stored }
  49. procedure build_parentfpstruct(pd: tprocdef);
  50. implementation
  51. uses
  52. globals,globtype,cclasses,cutils,verbose,systems,fmodule,
  53. tokens,
  54. symbase,symsym,symtable,symutil,defutil,defcmp,blockutl,
  55. {$ifdef jvm}
  56. jvmdef,
  57. {$endif jvm}
  58. node,nbas,
  59. aasmbase,
  60. paramgr;
  61. procedure insert_funcret_para(pd:tabstractprocdef);
  62. var
  63. storepos : tfileposinfo;
  64. vs : tparavarsym;
  65. paranr : word;
  66. begin
  67. if not(pd.proctypeoption in [potype_constructor,potype_destructor]) and
  68. not is_void(pd.returndef) and
  69. not (df_generic in pd.defoptions) and
  70. paramanager.ret_in_param(pd.returndef,pd) then
  71. begin
  72. storepos:=current_tokenpos;
  73. if pd.typ=procdef then
  74. current_tokenpos:=tprocdef(pd).fileinfo;
  75. {$if defined(i386)}
  76. { For left to right add it at the end to be delphi compatible.
  77. In the case of safecalls with safecal-exceptions support the
  78. funcret-para is (from the 'c'-point of view) a normal parameter
  79. which has to be added to the end of the parameter-list }
  80. if (pd.proccalloption in (pushleftright_pocalls)) or
  81. ((tf_safecall_exceptions in target_info.flags) and
  82. (pd.proccalloption=pocall_safecall)) then
  83. paranr:=paranr_result_leftright
  84. else
  85. {$elseif defined(SUPPORT_SAFECALL)}
  86. if (tf_safecall_exceptions in target_info.flags) and
  87. (pd.proccalloption = pocall_safecall) then
  88. paranr:=paranr_result_leftright
  89. else
  90. {$endif}
  91. if is_managed_type(pd.returndef) then
  92. paranr:=paranr_result_managed
  93. else
  94. paranr:=paranr_result;
  95. { Generate result variable accessing function result }
  96. vs:=cparavarsym.create('$result',paranr,vs_var,pd.returndef,[vo_is_funcret,vo_is_hidden_para]);
  97. pd.parast.insertsym(vs);
  98. { Store this symbol as funcretsym for procedures }
  99. if pd.typ=procdef then
  100. tprocdef(pd).funcretsym:=vs;
  101. current_tokenpos:=storepos;
  102. end;
  103. end;
  104. procedure insert_parentfp_para(pd:tabstractprocdef);
  105. var
  106. storepos : tfileposinfo;
  107. vs : tparavarsym;
  108. paranr : longint;
  109. begin
  110. if pd.parast.symtablelevel>normal_function_level then
  111. begin
  112. storepos:=current_tokenpos;
  113. if pd.typ=procdef then
  114. current_tokenpos:=tprocdef(pd).fileinfo;
  115. { if no support for nested procvars is activated, use the old
  116. calling convention to pass the parent frame pointer for backwards
  117. compatibility }
  118. if not(m_nested_procvars in current_settings.modeswitches) then
  119. paranr:=paranr_parentfp
  120. { nested procvars require Delphi-style parentfp passing, see
  121. po_delphi_nested_cc declaration for more info }
  122. {$if defined(i386) or defined(i8086)}
  123. else if (pd.proccalloption in pushleftright_pocalls) then
  124. paranr:=paranr_parentfp_delphi_cc_leftright
  125. {$endif i386 or i8086}
  126. else
  127. paranr:=paranr_parentfp_delphi_cc;
  128. { Generate frame pointer. It can't be put in a register since it
  129. must be accessable from nested routines }
  130. if not(target_info.system in systems_fpnestedstruct) or
  131. { in case of errors or declared procvardef types, prevent invalid
  132. type cast and possible nil pointer dereference }
  133. not assigned(pd.owner.defowner) or
  134. (pd.owner.defowner.typ<>procdef) then
  135. begin
  136. vs:=cparavarsym.create('$parentfp',paranr,vs_value
  137. ,parentfpvoidpointertype,[vo_is_parentfp,vo_is_hidden_para]);
  138. end
  139. else
  140. begin
  141. if not assigned(tprocdef(pd.owner.defowner).parentfpstruct) then
  142. build_parentfpstruct(tprocdef(pd.owner.defowner));
  143. vs:=cparavarsym.create('$parentfp',paranr,vs_value,
  144. tprocdef(pd.owner.defowner).parentfpstructptrtype,[vo_is_parentfp,vo_is_hidden_para]);
  145. end;
  146. pd.parast.insertsym(vs);
  147. current_tokenpos:=storepos;
  148. end;
  149. end;
  150. procedure insert_self_and_vmt_para(pd:tabstractprocdef);
  151. var
  152. storepos : tfileposinfo;
  153. vs : tparavarsym;
  154. hdef : tdef;
  155. selfdef : tdef;
  156. vsp : tvarspez;
  157. aliasvs : tabsolutevarsym;
  158. sl : tpropaccesslist;
  159. begin
  160. if (pd.typ=procdef) and
  161. is_objc_class_or_protocol(tprocdef(pd).struct) and
  162. (pd.parast.symtablelevel=normal_function_level) then
  163. begin
  164. { insert Objective-C self and selector parameters }
  165. vs:=cparavarsym.create('$_cmd',paranr_objc_cmd,vs_value,objc_seltype,[vo_is_msgsel,vo_is_hidden_para]);
  166. pd.parast.insertsym(vs);
  167. { make accessible to code }
  168. sl:=tpropaccesslist.create;
  169. sl.addsym(sl_load,vs);
  170. aliasvs:=cabsolutevarsym.create_ref('_CMD',objc_seltype,sl);
  171. include(aliasvs.varoptions,vo_is_msgsel);
  172. tlocalsymtable(tprocdef(pd).localst).insertsym(aliasvs);
  173. if (po_classmethod in pd.procoptions) then
  174. { compatible with what gcc does }
  175. hdef:=objc_idtype
  176. else
  177. hdef:=tprocdef(pd).struct;
  178. vs:=cparavarsym.create('$self',paranr_objc_self,vs_value,hdef,[vo_is_self,vo_is_hidden_para]);
  179. pd.parast.insertsym(vs);
  180. end
  181. else if (pd.typ=procvardef) and
  182. pd.is_methodpointer then
  183. begin
  184. { Generate self variable }
  185. vs:=cparavarsym.create('$self',paranr_self,vs_value,voidpointertype,[vo_is_self,vo_is_hidden_para]);
  186. pd.parast.insertsym(vs);
  187. end
  188. { while only procvardefs of this type can be declared in Pascal code,
  189. internally we also generate procdefs of this type when creating
  190. block wrappers }
  191. else if (po_is_block in pd.procoptions) then
  192. begin
  193. { generate the first hidden parameter, which is a so-called "block
  194. literal" describing the block and containing its invocation
  195. procedure }
  196. hdef:=cpointerdef.getreusable(get_block_literal_type_for_proc(pd));
  197. { mark as vo_is_parentfp so that proc2procvar comparisons will
  198. succeed when assigning arbitrary routines to the block }
  199. vs:=cparavarsym.create('$_block_literal',paranr_blockselfpara,vs_value,
  200. hdef,[vo_is_hidden_para,vo_is_parentfp]
  201. );
  202. pd.parast.insertsym(vs);
  203. if pd.typ=procdef then
  204. begin
  205. { make accessible to code }
  206. sl:=tpropaccesslist.create;
  207. sl.addsym(sl_load,vs);
  208. aliasvs:=cabsolutevarsym.create_ref('FPC_BLOCK_SELF',hdef,sl);
  209. include(aliasvs.varoptions,vo_is_parentfp);
  210. tlocalsymtable(tprocdef(pd).localst).insertsym(aliasvs);
  211. end;
  212. end
  213. else
  214. begin
  215. if (pd.typ=procdef) and
  216. assigned(tprocdef(pd).struct) and
  217. (pd.parast.symtablelevel=normal_function_level) then
  218. begin
  219. { static class methods have no hidden self/vmt pointer }
  220. if pd.no_self_node then
  221. exit;
  222. storepos:=current_tokenpos;
  223. current_tokenpos:=tprocdef(pd).fileinfo;
  224. { Generate VMT variable for constructor/destructor }
  225. if (pd.proctypeoption in [potype_constructor,potype_destructor]) and
  226. not(is_cppclass(tprocdef(pd).struct) or
  227. is_record(tprocdef(pd).struct) or
  228. is_javaclass(tprocdef(pd).struct) or
  229. (
  230. { no vmt for record/type helper constructors }
  231. is_objectpascal_helper(tprocdef(pd).struct) and
  232. (tobjectdef(tprocdef(pd).struct).extendeddef.typ<>objectdef)
  233. )) then
  234. begin
  235. vs:=cparavarsym.create('$vmt',paranr_vmt,vs_value,cclassrefdef.create(tprocdef(pd).struct),[vo_is_vmt,vo_is_hidden_para]);
  236. pd.parast.insertsym(vs);
  237. end;
  238. { for helpers the type of Self is equivalent to the extended
  239. type or equal to an instance of it }
  240. if is_objectpascal_helper(tprocdef(pd).struct) then
  241. selfdef:=tobjectdef(tprocdef(pd).struct).extendeddef
  242. else if is_objccategory(tprocdef(pd).struct) then
  243. selfdef:=tobjectdef(tprocdef(pd).struct).childof
  244. else
  245. selfdef:=tprocdef(pd).struct;
  246. { Generate self variable, for classes we need
  247. to use the generic voidpointer to be compatible with
  248. methodpointers }
  249. vsp:=vs_value;
  250. if (po_staticmethod in pd.procoptions) or
  251. (po_classmethod in pd.procoptions) then
  252. hdef:=cclassrefdef.create(selfdef)
  253. else
  254. begin
  255. if is_object(selfdef) or (selfdef.typ<>objectdef) then
  256. vsp:=vs_var;
  257. hdef:=selfdef;
  258. end;
  259. vs:=tparavarsym(pd.parast.find('self'));
  260. if not assigned(vs) or (vs.typ<>paravarsym) or (vs.vardef<>hdef) then
  261. begin
  262. vs:=cparavarsym.create('$self',paranr_self,vsp,hdef,[vo_is_self,vo_is_hidden_para]);
  263. pd.parast.insertsym(vs);
  264. end;
  265. current_tokenpos:=storepos;
  266. end;
  267. end;
  268. end;
  269. procedure insert_funcret_local(pd:tprocdef);
  270. var
  271. storepos : tfileposinfo;
  272. vs : tlocalvarsym;
  273. aliasvs : tabsolutevarsym;
  274. sl : tpropaccesslist;
  275. hs : string;
  276. begin
  277. storepos:=current_tokenpos;
  278. current_tokenpos:=pd.fileinfo;
  279. { The result from constructors and destructors can't be accessed directly }
  280. if not(pd.proctypeoption in [potype_constructor,potype_destructor]) and
  281. not is_void(pd.returndef) and
  282. (not(po_assembler in pd.procoptions) or paramanager.asm_result_var(pd.returndef,pd)) then
  283. begin
  284. { We need to insert a varsym for the result in the localst
  285. when it is returning in a register }
  286. { we also need to do this for a generic procdef as we didn't allow
  287. the creation of a result symbol in insert_funcret_para, but we need
  288. a valid funcretsym }
  289. if (df_generic in pd.defoptions) or
  290. not paramanager.ret_in_param(pd.returndef,pd) then
  291. begin
  292. vs:=clocalvarsym.create('$result',vs_value,pd.returndef,[vo_is_funcret]);
  293. pd.localst.insertsym(vs);
  294. pd.funcretsym:=vs;
  295. end;
  296. { insert the name of the procedure as alias for the function result,
  297. we can't use realname because that will not work for compilerprocs
  298. as the name is lowercase and unreachable from the code }
  299. if (pd.proctypeoption<>potype_operator) or assigned(pd.resultname) then
  300. begin
  301. if assigned(pd.resultname) then
  302. hs:=pd.resultname^
  303. else
  304. hs:=pd.procsym.name;
  305. sl:=tpropaccesslist.create;
  306. sl.addsym(sl_load,pd.funcretsym);
  307. aliasvs:=cabsolutevarsym.create_ref(hs,pd.returndef,sl);
  308. include(aliasvs.varoptions,vo_is_funcret);
  309. tlocalsymtable(pd.localst).insertsym(aliasvs);
  310. end;
  311. { insert result also if support is on }
  312. if (m_result in current_settings.modeswitches) then
  313. begin
  314. sl:=tpropaccesslist.create;
  315. sl.addsym(sl_load,pd.funcretsym);
  316. aliasvs:=cabsolutevarsym.create_ref('RESULT',pd.returndef,sl);
  317. include(aliasvs.varoptions,vo_is_funcret);
  318. include(aliasvs.varoptions,vo_is_result);
  319. tlocalsymtable(pd.localst).insertsym(aliasvs);
  320. end;
  321. end;
  322. if pd.generate_safecall_wrapper then
  323. begin
  324. { vo_is_funcret is necessary so the local only gets freed after we loaded its
  325. value into the return register }
  326. vs:=clocalvarsym.create('$safecallresult',vs_value,search_system_type('HRESULT').typedef,[vo_is_funcret]);
  327. { do not put this variable in a register. The register which will be bound
  328. to this symbol will not be allocated automatically. Which means it will
  329. be re-used wich breaks the code. Besides this it is questionable if it is
  330. an optimization if one of the registers is kept allocated during the complete
  331. function, without ever using it.
  332. (It would be better to re-write the safecall-support in such a way that this
  333. variable it not needed at all, but that the HRESULT is set when the method
  334. is finalized) }
  335. vs.varregable:=vr_none;
  336. pd.localst.insertsym(vs);
  337. end;
  338. current_tokenpos:=storepos;
  339. end;
  340. procedure insert_hidden_para(p:TObject;arg:pointer);
  341. var
  342. hvs : tparavarsym;
  343. pd : tabstractprocdef absolute arg;
  344. begin
  345. if (tsym(p).typ<>paravarsym) then
  346. exit;
  347. with tparavarsym(p) do
  348. begin
  349. { We need a local copy for a value parameter when only the
  350. address is pushed. Open arrays and Array of Const are
  351. an exception because they are allocated at runtime and the
  352. address that is pushed is patched.
  353. Arrays passed to cdecl routines are special: they are pointers in
  354. C and hence must be passed as such. Due to historical reasons, if
  355. a cdecl routine is implemented in Pascal, we still make a copy on
  356. the callee side. Do this the same on platforms that normally must
  357. make a copy on the caller side, as otherwise the behaviour will
  358. be different (and less perfomant) for routines implemented in C }
  359. if (varspez=vs_value) and
  360. paramanager.push_addr_param(varspez,vardef,pd.proccalloption) and
  361. not(is_open_array(vardef) or
  362. is_array_of_const(vardef)) and
  363. (not(target_info.system in systems_caller_copy_addr_value_para) or
  364. ((pd.proccalloption in cdecl_pocalls) and
  365. (vardef.typ=arraydef))) then
  366. include(varoptions,vo_has_local_copy);
  367. { needs high parameter ? }
  368. if paramanager.push_high_param(varspez,vardef,pd.proccalloption) then
  369. begin
  370. hvs:=cparavarsym.create('$high'+name,paranr+1,vs_const,sizesinttype,[vo_is_high_para,vo_is_hidden_para]);
  371. hvs.symoptions:=[];
  372. owner.insertsym(hvs);
  373. { don't place to register if it will be accessed from implicit finally block }
  374. if (varspez=vs_value) and
  375. is_open_array(vardef) and
  376. is_managed_type(vardef) then
  377. hvs.varregable:=vr_none;
  378. end
  379. else
  380. begin
  381. { Give a warning that cdecl routines does not include high()
  382. support }
  383. if (pd.proccalloption in cdecl_pocalls) and
  384. paramanager.push_high_param(varspez,vardef,pocall_default) then
  385. begin
  386. if is_open_string(vardef) then
  387. MessagePos(fileinfo,parser_w_cdecl_no_openstring);
  388. if not(po_external in pd.procoptions) and
  389. (pd.typ<>procvardef) and
  390. not is_objc_class_or_protocol(tprocdef(pd).struct) then
  391. if is_array_of_const(vardef) then
  392. MessagePos(fileinfo,parser_e_varargs_need_cdecl_and_external)
  393. else
  394. MessagePos(fileinfo,parser_w_cdecl_has_no_high);
  395. end;
  396. if (vardef.typ=formaldef) and (Tformaldef(vardef).typed) then
  397. begin
  398. hvs:=cparavarsym.create('$typinfo'+name,paranr+1,vs_const,voidpointertype,
  399. [vo_is_typinfo_para,vo_is_hidden_para]);
  400. owner.insertsym(hvs);
  401. end;
  402. end;
  403. end;
  404. end;
  405. procedure check_c_para(pd:Tabstractprocdef);
  406. var
  407. i,
  408. lastparaidx : longint;
  409. sym : TSym;
  410. begin
  411. lastparaidx:=pd.parast.SymList.Count-1;
  412. for i:=0 to pd.parast.SymList.Count-1 do
  413. begin
  414. sym:=tsym(pd.parast.SymList[i]);
  415. if (sym.typ=paravarsym) and
  416. (tparavarsym(sym).vardef.typ=arraydef) then
  417. begin
  418. if not is_variant_array(tparavarsym(sym).vardef) and
  419. not is_array_of_const(tparavarsym(sym).vardef) and
  420. (tparavarsym(sym).varspez<>vs_var) then
  421. MessagePos(tparavarsym(sym).fileinfo,parser_h_c_arrays_are_references);
  422. if is_array_of_const(tparavarsym(sym).vardef) and
  423. (i<lastparaidx) and
  424. (tsym(pd.parast.SymList[i+1]).typ=paravarsym) and
  425. not(vo_is_high_para in tparavarsym(pd.parast.SymList[i+1]).varoptions) then
  426. MessagePos(tparavarsym(sym).fileinfo,parser_e_C_array_of_const_must_be_last);
  427. end;
  428. end;
  429. end;
  430. procedure insert_struct_hidden_paras(astruct: tabstractrecorddef);
  431. var
  432. pd: tdef;
  433. i: longint;
  434. oldpos: tfileposinfo;
  435. begin
  436. // handle calling conventions of record methods
  437. oldpos:=current_filepos;
  438. { don't keep track of procdefs in a separate list, because the
  439. compiler may add additional procdefs (e.g. property wrappers for
  440. the jvm backend) }
  441. for i := 0 to astruct.symtable.deflist.count - 1 do
  442. begin
  443. pd:=tdef(astruct.symtable.deflist[i]);
  444. if pd.typ<>procdef then
  445. continue;
  446. current_filepos:=tprocdef(pd).fileinfo;
  447. handle_calling_convention(tprocdef(pd),[hcc_declaration,hcc_insert_hidden_paras]);
  448. end;
  449. current_filepos:=oldpos;
  450. end;
  451. procedure set_addr_param_regable(p:TObject;arg:pointer);
  452. begin
  453. if (tsym(p).typ<>paravarsym) then
  454. exit;
  455. with tparavarsym(p) do
  456. begin
  457. if (not needs_finalization) and
  458. paramanager.push_addr_param(varspez,vardef,tprocdef(arg).proccalloption) then
  459. varregable:=vr_addr;
  460. end;
  461. end;
  462. procedure handle_calling_convention(pd_or_invkdef:tdef;flags:thccflags);
  463. var
  464. pd : tabstractprocdef;
  465. begin
  466. if is_funcref(pd_or_invkdef) then
  467. pd:=get_invoke_procdef(tobjectdef(pd_or_invkdef))
  468. else if pd_or_invkdef.typ in [procdef,procvardef] then
  469. pd:=tabstractprocdef(pd_or_invkdef)
  470. else
  471. internalerror(2022012502);
  472. if hcc_check in flags then
  473. begin
  474. { set the default calling convention if none provided }
  475. if (pd.typ=procdef) and
  476. (is_objc_class_or_protocol(tprocdef(pd).struct) or
  477. is_cppclass(tprocdef(pd).struct)) then
  478. begin
  479. { none of the explicit calling conventions should be allowed }
  480. if (po_hascallingconvention in pd.procoptions) then
  481. internalerror(2009032501);
  482. if is_cppclass(tprocdef(pd).struct) then
  483. pd.proccalloption:=pocall_cppdecl
  484. else
  485. pd.proccalloption:=pocall_cdecl;
  486. end
  487. else if not(po_hascallingconvention in pd.procoptions) then
  488. pd.proccalloption:=current_settings.defproccall
  489. else
  490. begin
  491. if pd.proccalloption=pocall_none then
  492. internalerror(200309081);
  493. end;
  494. { handle proccall specific settings }
  495. case pd.proccalloption of
  496. pocall_cdecl,
  497. pocall_cppdecl,
  498. pocall_sysv_abi_cdecl,
  499. pocall_ms_abi_cdecl:
  500. begin
  501. { check C cdecl para types }
  502. check_c_para(pd);
  503. end;
  504. pocall_far16 :
  505. begin
  506. { Temporary stub, must be rewritten to support OS/2 far16 }
  507. Message1(parser_w_proc_directive_ignored,'FAR16');
  508. end;
  509. else
  510. ;
  511. end;
  512. { Inlining is enabled and supported? }
  513. if (po_inline in pd.procoptions) and
  514. not(cs_do_inline in current_settings.localswitches) then
  515. begin
  516. { Give an error if inline is not supported by the compiler mode,
  517. otherwise only give a hint that this procedure will not be inlined }
  518. if not(m_default_inline in current_settings.modeswitches) then
  519. Message(parser_e_proc_inline_not_supported)
  520. else
  521. Message(parser_h_inlining_disabled);
  522. exclude(pd.procoptions,po_inline);
  523. end;
  524. { For varargs directive also cdecl and external must be defined }
  525. if (po_varargs in pd.procoptions) then
  526. begin
  527. { check first for external in the interface, if available there
  528. then the cdecl must also be there since there is no implementation
  529. available to contain it }
  530. if hcc_declaration in flags then
  531. begin
  532. { if external is available, then cdecl must also be available,
  533. procvars don't need external }
  534. if not((po_external in pd.procoptions) or
  535. (pd.typ=procvardef) or
  536. { for objcclasses this is checked later, because the entire
  537. class may be external. }
  538. is_objc_class_or_protocol(tprocdef(pd).struct)) and
  539. not(pd.proccalloption in (cdecl_pocalls + [pocall_stdcall])) then
  540. Message(parser_e_varargs_need_cdecl_and_external);
  541. end
  542. else
  543. begin
  544. { both must be defined now }
  545. if not((po_external in pd.procoptions) or
  546. (pd.typ=procvardef)) or
  547. not(pd.proccalloption in (cdecl_pocalls + [pocall_stdcall])) then
  548. Message(parser_e_varargs_need_cdecl_and_external);
  549. end;
  550. end;
  551. end;
  552. if hcc_insert_hidden_paras in flags then
  553. begin
  554. { If the paraloc info has been calculated already, it will be missing for
  555. the new parameters we add below. This should never be necessary before
  556. we add them, as users of this information would not process these extra
  557. parameters in that case }
  558. if pd.has_paraloc_info<>callnoside then
  559. internalerror(2019031610);
  560. { insert hidden high parameters }
  561. pd.parast.SymList.ForEachCall(@insert_hidden_para,pd);
  562. { insert hidden self parameter }
  563. insert_self_and_vmt_para(pd);
  564. { insert funcret parameter if required }
  565. insert_funcret_para(pd);
  566. { Make var parameters regable, this must be done after the calling
  567. convention is set. }
  568. { this must be done before parentfp is insert, because getting all cases
  569. where parentfp must be in a memory location isn't catched properly so
  570. we put parentfp never in a register }
  571. pd.parast.SymList.ForEachCall(@set_addr_param_regable,pd);
  572. { insert parentfp parameter if required }
  573. insert_parentfp_para(pd);
  574. end;
  575. { Calculate parameter tlist }
  576. pd.calcparas;
  577. end;
  578. function proc_add_definition(var currpd:tprocdef):boolean;
  579. function check_generic_parameters(fwpd,currpd:tprocdef):boolean;
  580. var
  581. i : longint;
  582. fwsym,
  583. currsym : tsym;
  584. currtype : ttypesym absolute currsym;
  585. fileinfo : tfileposinfo;
  586. begin
  587. result:=true;
  588. if fwpd.genericparas.count<>currpd.genericparas.count then
  589. internalerror(2018090101);
  590. for i:=0 to fwpd.genericparas.count-1 do
  591. begin
  592. fwsym:=tsym(fwpd.genericparas[i]);
  593. currsym:=tsym(currpd.genericparas[i]);
  594. if fwsym.name<>currsym.name then
  595. begin
  596. messagepos1(currsym.fileinfo,sym_e_generic_type_param_mismatch,currsym.realname);
  597. messagepos1(fwsym.fileinfo,sym_e_generic_type_param_decl,fwsym.realname);
  598. result:=false;
  599. end;
  600. if (fwpd.interfacedef or assigned(fwpd.struct)) and
  601. (
  602. ((currsym.typ=typesym) and (df_genconstraint in currtype.typedef.defoptions)) or
  603. (currsym.typ=constsym)
  604. ) then
  605. begin
  606. if currsym.typ=constsym then
  607. fileinfo:=currsym.fileinfo
  608. else
  609. fileinfo:=tstoreddef(currtype.typedef).genconstraintdata.fileinfo;
  610. messagepos(fileinfo,parser_e_generic_constraints_not_allowed_here);
  611. result:=false;
  612. end;
  613. if not fwpd.interfacedef and not assigned(fwpd.struct) and
  614. (fwsym.typ=constsym) then
  615. begin
  616. { without modeswitch RepeatForward we need to check here
  617. if the type of the constants match }
  618. if (currsym.typ<>constsym) or not equal_defs(tconstsym(fwsym).constdef,tconstsym(currsym).constdef) then
  619. begin
  620. messagepos1(currpd.fileinfo,parser_e_header_dont_match_forward,currpd.fullprocname(false));
  621. result:=false;
  622. end;
  623. end;
  624. end;
  625. end;
  626. function equal_generic_procdefs(fwpd,currpd:tprocdef;out sameparas,sameret:boolean):boolean;
  627. var
  628. i : longint;
  629. fwsym,
  630. currsym : tsym;
  631. currtype : ttypesym absolute currsym;
  632. convdummy: tconverttype;
  633. pddummy: tprocdef;
  634. begin
  635. result:=false;
  636. sameparas:=false;
  637. sameret:=false;
  638. if fwpd.genericparas.count<>currpd.genericparas.count then
  639. exit;
  640. { comparing generic declarations is a bit more cumbersome as the
  641. defs of the generic parameter types are not equal, especially if the
  642. declaration contains constraints; essentially we have two cases:
  643. - proc declared in interface of unit (or in class/record/object)
  644. and defined in implementation; here the fwpd might contain
  645. constraints while currpd must only contain undefineddefs
  646. - forward declaration in implementation: here constraints must be
  647. repeated }
  648. for i:=0 to fwpd.genericparas.count-1 do
  649. begin
  650. fwsym:=tsym(fwpd.genericparas[i]);
  651. currsym:=tsym(currpd.genericparas[i]);
  652. { if the type in the currpd isn't a pure undefineddef (thus there
  653. are constraints and the fwpd was declared in the interface, then
  654. we can stop right there }
  655. if fwpd.interfacedef and
  656. (
  657. (currsym.typ=constsym) or
  658. ((currsym.typ=typesym) and
  659. (
  660. (currtype.typedef.typ<>undefineddef) or
  661. (df_genconstraint in currtype.typedef.defoptions)
  662. )
  663. )
  664. )then
  665. exit;
  666. if not fwpd.interfacedef then
  667. begin
  668. if (fwsym.typ=constsym) and (currsym.typ=constsym) then
  669. begin
  670. { check whether the constant type for forward functions match }
  671. if not equal_defs(tconstsym(fwsym).constdef,tconstsym(currsym).constdef) then
  672. exit;
  673. end
  674. else if (fwsym.typ=constsym) then
  675. { if the forward sym is a constant, the implementation needs to be one
  676. as well }
  677. exit;
  678. end;
  679. end;
  680. if compare_paras(fwpd.paras,currpd.paras,cp_none,[cpo_ignorehidden,cpo_openequalisexact,cpo_ignoreuniv,cpo_generic])<>te_exact then
  681. exit;
  682. sameparas:=true;
  683. if (df_specialization in tstoreddef(fwpd.returndef).defoptions) and (df_specialization in tstoreddef(currpd.returndef).defoptions) then
  684. { for specializations we're happy with equal defs instead of exactly the same defs }
  685. result:=equal_defs(fwpd.returndef,currpd.returndef)
  686. else
  687. begin
  688. { strictly compare defs using compare_defs_ext, but allow
  689. non exactly equal undefineddefs }
  690. convdummy:=tc_none;
  691. pddummy:=nil;
  692. result:=(compare_defs_ext(fwpd.returndef,currpd.returndef,nothingn,convdummy,pddummy,[cdo_allow_variant,cdo_strict_undefined_check])=te_exact) or
  693. equal_genfunc_paradefs(fwpd.returndef,currpd.returndef,fwpd.parast,currpd.parast);
  694. end;
  695. { the result variable is only set depending on the return type, so we
  696. can simply use "result" }
  697. sameret:=result;
  698. end;
  699. function equal_signature(fwpd,currpd:tprocdef;out sameparas,sameret:boolean):boolean;
  700. begin
  701. sameparas:=compare_paras(fwpd.paras,currpd.paras,cp_none,[cpo_ignorehidden,cpo_openequalisexact,cpo_ignoreuniv])=te_exact;
  702. sameret:=compare_defs(fwpd.returndef,currpd.returndef,nothingn)=te_exact;
  703. result:=sameparas and sameret;
  704. end;
  705. {
  706. Add definition aprocdef to the overloaded definitions of aprocsym. If a
  707. forwarddef is found and reused it returns true
  708. }
  709. var
  710. fwpd : tprocdef;
  711. currparasym,
  712. fwparasym : tsym;
  713. currparacnt,
  714. fwparacnt,
  715. curridx,
  716. fwidx,
  717. i : longint;
  718. po_comp : tprocoptions;
  719. paracompopt: tcompare_paras_options;
  720. sameparasfound,
  721. gensameparas,
  722. gensameret,
  723. sameparas,
  724. sameret,
  725. forwardfound : boolean;
  726. symentry: TSymEntry;
  727. item : tlinkedlistitem;
  728. begin
  729. forwardfound:=false;
  730. if assigned(currpd.struct) and
  731. (currpd.struct.symtable.moduleid<>current_module.moduleid) and
  732. not (df_specialization in currpd.defoptions) then
  733. begin
  734. result:=false;
  735. exit;
  736. end;
  737. sameparasfound:=false;
  738. fwpd:=nil;
  739. { check overloaded functions if the same function already exists }
  740. for i:=0 to tprocsym(currpd.procsym).ProcdefList.Count-1 do
  741. begin
  742. fwpd:=tprocdef(tprocsym(currpd.procsym).ProcdefList[i]);
  743. { can happen for internally generated routines }
  744. if (fwpd=currpd) then
  745. begin
  746. result:=true;
  747. exit;
  748. end;
  749. { Skip overloaded definitions that are declared in other units }
  750. if fwpd.procsym<>currpd.procsym then
  751. continue;
  752. gensameparas:=false;
  753. gensameret:=false;
  754. sameparas:=false;
  755. sameret:=false;
  756. { check the parameters, for delphi/tp it is possible to
  757. leave the parameters away in the implementation (forwarddef=false).
  758. But for an overload declared function this is not allowed }
  759. if { check if empty implementation arguments match is allowed }
  760. (
  761. not(m_repeat_forward in current_settings.modeswitches) and
  762. not(currpd.forwarddef) and
  763. is_bareprocdef(currpd) and
  764. not(po_overload in fwpd.procoptions)
  765. ) or
  766. (
  767. fwpd.is_generic and
  768. currpd.is_generic and
  769. equal_generic_procdefs(fwpd,currpd,gensameparas,gensameret)
  770. ) or
  771. { check arguments, we need to check only the user visible parameters. The hidden parameters
  772. can be in a different location because of the calling convention, eg. L-R vs. R-L order (PFV)
  773. don't check default values here, because routines that are the same except for their default
  774. values should be reported as mismatches (since you can't overload based on different default
  775. parameter values) }
  776. (
  777. not fwpd.is_generic and
  778. not currpd.is_generic and
  779. equal_signature(fwpd,currpd,sameparas,sameret)
  780. ) then
  781. begin
  782. { Check if we've found the forwarddef, if found then
  783. we need to update the forward def with the current
  784. implementation settings }
  785. if fwpd.forwarddef then
  786. begin
  787. forwardfound:=true;
  788. if not(m_repeat_forward in current_settings.modeswitches) and
  789. (fwpd.proccalloption<>currpd.proccalloption) then
  790. paracompopt:=[cpo_ignorehidden,cpo_comparedefaultvalue,cpo_openequalisexact,cpo_ignoreuniv]
  791. else
  792. paracompopt:=[cpo_comparedefaultvalue,cpo_openequalisexact,cpo_ignoreuniv];
  793. { Check calling convention }
  794. if (fwpd.proccalloption<>currpd.proccalloption) then
  795. begin
  796. { In delphi it is possible to specify the calling
  797. convention in the interface or implementation if
  798. there was no convention specified in the other
  799. part }
  800. if (m_delphi in current_settings.modeswitches) then
  801. begin
  802. if not(po_hascallingconvention in currpd.procoptions) then
  803. currpd.proccalloption:=fwpd.proccalloption
  804. else
  805. if not(po_hascallingconvention in fwpd.procoptions) then
  806. fwpd.proccalloption:=currpd.proccalloption
  807. else
  808. begin
  809. MessagePos(currpd.fileinfo,parser_e_call_convention_dont_match_forward);
  810. tprocsym(currpd.procsym).write_parameter_lists(currpd);
  811. { restore interface settings }
  812. currpd.proccalloption:=fwpd.proccalloption;
  813. end;
  814. end
  815. else
  816. begin
  817. MessagePos(currpd.fileinfo,parser_e_call_convention_dont_match_forward);
  818. tprocsym(currpd.procsym).write_parameter_lists(currpd);
  819. { restore interface settings }
  820. currpd.proccalloption:=fwpd.proccalloption;
  821. end;
  822. end;
  823. { Check static }
  824. if (po_staticmethod in fwpd.procoptions) then
  825. begin
  826. if not (po_staticmethod in currpd.procoptions) then
  827. begin
  828. include(currpd.procoptions, po_staticmethod);
  829. if (po_classmethod in currpd.procoptions) then
  830. begin
  831. { remove self from the hidden paras }
  832. symentry:=currpd.parast.Find('self');
  833. if symentry<>nil then
  834. begin
  835. currpd.parast.DeleteSym(symentry);
  836. currpd.calcparas;
  837. end;
  838. end;
  839. end;
  840. end;
  841. { Check if the procedure type and return type are correct,
  842. also the parameters must match also with the type and that
  843. if the implementation has default parameters, the interface
  844. also has them and that if they both have them, that they
  845. have the same value }
  846. if ((m_repeat_forward in current_settings.modeswitches) or
  847. not is_bareprocdef(currpd)) and
  848. (
  849. (
  850. fwpd.is_generic and
  851. currpd.is_generic and
  852. not equal_generic_procdefs(fwpd,currpd,sameparas,sameret)
  853. ) or
  854. (
  855. (
  856. not fwpd.is_generic or
  857. not currpd.is_generic
  858. ) and
  859. (
  860. (compare_paras(fwpd.paras,currpd.paras,cp_all,paracompopt)<>te_exact) or
  861. (compare_defs(fwpd.returndef,currpd.returndef,nothingn)<>te_exact)
  862. )
  863. )
  864. ) then
  865. begin
  866. MessagePos1(currpd.fileinfo,parser_e_header_dont_match_forward,
  867. fwpd.fullprocname(false));
  868. tprocsym(currpd.procsym).write_parameter_lists(currpd);
  869. break;
  870. end;
  871. { Check if both are declared forward }
  872. if fwpd.forwarddef and currpd.forwarddef then
  873. begin
  874. MessagePos1(currpd.fileinfo,parser_e_function_already_declared_public_forward,
  875. currpd.fullprocname(false));
  876. end;
  877. { internconst or internproc only need to be defined once }
  878. if (fwpd.proccalloption=pocall_internproc) then
  879. currpd.proccalloption:=fwpd.proccalloption
  880. else
  881. if (currpd.proccalloption=pocall_internproc) then
  882. fwpd.proccalloption:=currpd.proccalloption;
  883. { Check procedure options, Delphi requires that class is
  884. repeated in the implementation for class methods }
  885. if (m_fpc in current_settings.modeswitches) then
  886. po_comp:=[po_classmethod,po_varargs,po_methodpointer,po_interrupt]
  887. else
  888. po_comp:=[po_classmethod,po_methodpointer];
  889. if ((po_comp * fwpd.procoptions)<>(po_comp * currpd.procoptions)) or
  890. (fwpd.proctypeoption <> currpd.proctypeoption) or
  891. { if the implementation version has an "overload" modifier,
  892. the interface version must also have it (otherwise we can
  893. get annoying crashes due to interface crc changes) }
  894. (not(po_overload in fwpd.procoptions) and
  895. (po_overload in currpd.procoptions)) or
  896. { same with noreturn }
  897. (not(po_noreturn in fwpd.procoptions) and
  898. (po_noreturn in currpd.procoptions)) then
  899. begin
  900. MessagePos1(currpd.fileinfo,parser_e_header_dont_match_forward,
  901. fwpd.fullprocname(false));
  902. tprocsym(fwpd.procsym).write_parameter_lists(fwpd);
  903. { This error is non-fatal, we can recover }
  904. end;
  905. { Forward declaration is external? }
  906. if (po_external in fwpd.procoptions) then
  907. MessagePos(currpd.fileinfo,parser_e_proc_already_external);
  908. { check for conflicts with "virtual" if this is a virtual
  909. method, as "virtual" cannot be repeated in the
  910. implementation and hence does not get checked against }
  911. if (po_virtualmethod in fwpd.procoptions) then
  912. begin
  913. po_comp:=currpd.procoptions*PD_VIRTUAL_MUTEXCLPO;
  914. if po_comp<>[] then
  915. MessagePos2(currpd.fileinfo,parser_e_proc_dir_conflict,tokeninfo^[_VIRTUAL].str,get_first_proc_str(po_comp));
  916. end;
  917. { Check parameters }
  918. if (m_repeat_forward in current_settings.modeswitches) or
  919. (currpd.minparacount>0) then
  920. begin
  921. { If mangled names are equal then they have the same amount of arguments }
  922. { We can check the names of the arguments }
  923. { both symtables are in the same order from left to right }
  924. curridx:=0;
  925. fwidx:=0;
  926. currparacnt:=currpd.parast.SymList.Count;
  927. fwparacnt:=fwpd.parast.SymList.Count;
  928. repeat
  929. { skip default parameter constsyms }
  930. while (curridx<currparacnt) and
  931. (tsym(currpd.parast.SymList[curridx]).typ<>paravarsym) do
  932. inc(curridx);
  933. while (fwidx<fwparacnt) and
  934. (tsym(fwpd.parast.SymList[fwidx]).typ<>paravarsym) do
  935. inc(fwidx);
  936. { stop when one of the two lists is at the end }
  937. if (fwidx>=fwparacnt) or (curridx>=currparacnt) then
  938. break;
  939. { compare names of parameters, ignore implictly
  940. renamed parameters }
  941. currparasym:=tsym(currpd.parast.SymList[curridx]);
  942. fwparasym:=tsym(fwpd.parast.SymList[fwidx]);
  943. if not(sp_implicitrename in currparasym.symoptions) and
  944. not(sp_implicitrename in fwparasym.symoptions) then
  945. begin
  946. if (currparasym.name<>fwparasym.name) then
  947. begin
  948. MessagePos3(currpd.fileinfo,parser_e_header_different_var_names,
  949. tprocsym(currpd.procsym).realname,fwparasym.realname,currparasym.realname);
  950. break;
  951. end;
  952. end;
  953. { next parameter }
  954. inc(curridx);
  955. inc(fwidx);
  956. until false;
  957. end;
  958. { check that the type parameter names for generic methods match;
  959. we check this here and not in equal_generic_procdefs as the defs
  960. might still be different due to their parameters, so we'd generate
  961. errors without any need }
  962. if currpd.is_generic and fwpd.is_generic then
  963. { an error here is recoverable, so we simply continue }
  964. check_generic_parameters(fwpd,currpd);
  965. { Everything is checked, now we can update the forward declaration
  966. with the new data from the implementation }
  967. fwpd.forwarddef:=currpd.forwarddef;
  968. fwpd.hasforward:=true;
  969. fwpd.procoptions:=fwpd.procoptions+currpd.procoptions;
  970. { marked as local but exported from unit? }
  971. if (po_kylixlocal in fwpd.procoptions) and (fwpd.owner.symtabletype=globalsymtable) then
  972. MessagePos(fwpd.fileinfo,type_e_cant_export_local);
  973. if fwpd.extnumber=$ffff then
  974. fwpd.extnumber:=currpd.extnumber;
  975. while not currpd.aliasnames.empty do
  976. fwpd.aliasnames.insert(currpd.aliasnames.getfirst);
  977. { update fileinfo so position references the implementation,
  978. also update funcretsym if it is already generated }
  979. fwpd.fileinfo:=currpd.fileinfo;
  980. if assigned(fwpd.funcretsym) then
  981. fwpd.funcretsym.fileinfo:=currpd.fileinfo;
  982. if assigned(currpd.deprecatedmsg) then
  983. begin
  984. stringdispose(fwpd.deprecatedmsg);
  985. fwpd.deprecatedmsg:=stringdup(currpd.deprecatedmsg^);
  986. end;
  987. { import names }
  988. if assigned(currpd.import_dll) then
  989. begin
  990. stringdispose(fwpd.import_dll);
  991. fwpd.import_dll:=stringdup(currpd.import_dll^);
  992. end;
  993. if assigned(currpd.import_name) then
  994. begin
  995. stringdispose(fwpd.import_name);
  996. fwpd.import_name:=stringdup(currpd.import_name^);
  997. end;
  998. fwpd.import_nr:=currpd.import_nr;
  999. { for compilerproc defines we need to rename and update the
  1000. symbolname to lowercase so users can' access it (can't do
  1001. it immediately, because then the implementation symbol
  1002. won't be matched) }
  1003. if po_compilerproc in fwpd.procoptions then
  1004. begin
  1005. fwpd.setcompilerprocname;
  1006. current_module.add_public_asmsym(fwpd.procsym.realname,AB_GLOBAL,AT_FUNCTION);
  1007. end;
  1008. if po_public in fwpd.procoptions then
  1009. begin
  1010. item:=fwpd.aliasnames.first;
  1011. while assigned(item) do
  1012. begin
  1013. current_module.add_public_asmsym(TCmdStrListItem(item).str,AB_GLOBAL,AT_FUNCTION);
  1014. item:=item.next;
  1015. end;
  1016. end;
  1017. { Release current procdef }
  1018. currpd.owner.deletedef(currpd);
  1019. currpd:=fwpd;
  1020. end
  1021. else
  1022. begin
  1023. { abstract methods aren't forward defined, but this }
  1024. { needs another error message }
  1025. if (po_abstractmethod in fwpd.procoptions) then
  1026. MessagePos(currpd.fileinfo,parser_e_abstract_no_definition)
  1027. else
  1028. begin
  1029. MessagePos(currpd.fileinfo,parser_e_overloaded_have_same_parameters);
  1030. tprocsym(currpd.procsym).write_parameter_lists(currpd);
  1031. end;
  1032. end;
  1033. { we found one proc with the same arguments, there are no others
  1034. so we can stop }
  1035. break;
  1036. end;
  1037. { check for allowing overload directive }
  1038. if not(m_fpc in current_settings.modeswitches) then
  1039. begin
  1040. { overload directive turns on overloading }
  1041. if ((po_overload in currpd.procoptions) or
  1042. (po_overload in fwpd.procoptions)) then
  1043. begin
  1044. { check if all procs have overloading, but not if the proc is a method or
  1045. already declared forward, then the check is already done }
  1046. if not(fwpd.hasforward or
  1047. assigned(currpd.struct) or
  1048. (currpd.forwarddef<>fwpd.forwarddef) or
  1049. ((po_overload in currpd.procoptions) and
  1050. (po_overload in fwpd.procoptions))) then
  1051. begin
  1052. MessagePos1(currpd.fileinfo,parser_e_no_overload_for_all_procs,currpd.procsym.realname);
  1053. break;
  1054. end
  1055. end
  1056. else
  1057. begin
  1058. if not(fwpd.forwarddef) then
  1059. begin
  1060. if (m_tp7 in current_settings.modeswitches) then
  1061. MessagePos(currpd.fileinfo,parser_e_procedure_overloading_is_off)
  1062. else
  1063. MessagePos1(currpd.fileinfo,parser_e_no_overload_for_all_procs,currpd.procsym.realname);
  1064. break;
  1065. end;
  1066. end;
  1067. end; { equal arguments }
  1068. { we found a match with the same parameter signature, but mismatching
  1069. return types; complain about that, but only once we've checked for
  1070. a forward to improve error recovery }
  1071. if (sameparas and not sameret and
  1072. { ensure that specifiers are the same as well }
  1073. (compare_paras(fwpd.paras,currpd.paras,cp_all,[cpo_ignorehidden,cpo_openequalisexact,cpo_ignoreuniv])=te_exact)
  1074. ) or (gensameparas and not gensameret) then
  1075. sameparasfound:=true;
  1076. end;
  1077. if sameparasfound and
  1078. not (currpd.proctypeoption=potype_operator) and
  1079. (
  1080. { allow overloads with different result types for external java
  1081. classes as Java supports covariant return types when implementing
  1082. interfaces and e.g. AbstractStringBuilder uses that }
  1083. not assigned(currpd.struct) or
  1084. not is_java_class_or_interface(currpd.struct) or
  1085. not (oo_is_external in tobjectdef(currpd.struct).objectoptions)
  1086. ) then
  1087. begin
  1088. MessagePos(currpd.fileinfo,parser_e_overloaded_have_same_parameters);
  1089. tprocsym(currpd.procsym).write_parameter_lists(currpd);
  1090. end;
  1091. { if we didn't reuse a forwarddef then we add the procdef to the overloaded
  1092. list }
  1093. if not forwardfound then
  1094. begin
  1095. { can happen in Delphi mode }
  1096. if (currpd.proctypeoption = potype_function) and
  1097. is_void(currpd.returndef) then
  1098. MessagePos1(currpd.fileinfo,parser_e_no_funcret_specified,currpd.procsym.realname);
  1099. tprocsym(currpd.procsym).ProcdefList.Add(currpd);
  1100. if not currpd.forwarddef and (po_public in currpd.procoptions) then
  1101. begin
  1102. item:=currpd.aliasnames.first;
  1103. while assigned(item) do
  1104. begin
  1105. current_module.add_public_asmsym(TCmdStrListItem(item).str,AB_GLOBAL,AT_FUNCTION);
  1106. item:=item.next;
  1107. end;
  1108. end;
  1109. end;
  1110. proc_add_definition:=forwardfound;
  1111. end;
  1112. procedure build_parentfpstruct(pd: tprocdef);
  1113. var
  1114. nestedvars: tsym;
  1115. nestedvarsst: tsymtable;
  1116. pnestedvarsdef,
  1117. nestedvarsdef: tdef;
  1118. old_symtablestack: tsymtablestack;
  1119. begin
  1120. { make sure the defs are not registered in the current symtablestack,
  1121. because they may be for a parent procdef (changeowner does remove a def
  1122. from the symtable in which it was originally created, so that by itself
  1123. is not enough) }
  1124. old_symtablestack:=symtablestack;
  1125. symtablestack:=old_symtablestack.getcopyuntil(current_module.localsymtable);
  1126. { create struct to hold local variables and parameters that are
  1127. accessed from within nested routines (start with extra dollar to prevent
  1128. the JVM from thinking this is a nested class in the unit) }
  1129. nestedvarsst:=trecordsymtable.create('$'+current_module.realmodulename^+'$$_fpc_nestedvars$'+pd.unique_id_str,
  1130. current_settings.alignment.localalignmax,current_settings.alignment.localalignmin);
  1131. nestedvarsdef:=crecorddef.create(nestedvarsst.name^,nestedvarsst);
  1132. {$ifdef jvm}
  1133. maybe_guarantee_record_typesym(nestedvarsdef,nestedvarsdef.owner);
  1134. { don't add clone/FpcDeepCopy, because the field names are not all
  1135. representable in source form and we don't need them anyway }
  1136. symtablestack.push(trecorddef(nestedvarsdef).symtable);
  1137. maybe_add_public_default_java_constructor(trecorddef(nestedvarsdef));
  1138. insert_struct_hidden_paras(trecorddef(nestedvarsdef));
  1139. symtablestack.pop(trecorddef(nestedvarsdef).symtable);
  1140. {$endif}
  1141. symtablestack.free;
  1142. symtablestack:=old_symtablestack.getcopyuntil(pd.localst);
  1143. pnestedvarsdef:=cpointerdef.getreusable(nestedvarsdef);
  1144. if not(po_assembler in pd.procoptions) then
  1145. begin
  1146. nestedvars:=clocalvarsym.create('$nestedvars',vs_var,nestedvarsdef,[]);
  1147. include(nestedvars.symoptions,sp_internal);
  1148. pd.localst.insertsym(nestedvars);
  1149. pd.parentfpstruct:=nestedvars;
  1150. pd.parentfpinitblock:=cblocknode.create(nil);
  1151. end;
  1152. symtablestack.free;
  1153. pd.parentfpstructptrtype:=pnestedvarsdef;
  1154. symtablestack:=old_symtablestack;
  1155. end;
  1156. end.