pparautl.pas 56 KB

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