pparautl.pas 53 KB

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