pparautl.pas 56 KB

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