pparautl.pas 58 KB

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