pparautl.pas 53 KB

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