pparautl.pas 57 KB

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