pparautl.pas 55 KB

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