pparautl.pas 56 KB

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