pparautl.pas 58 KB

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