pparautl.pas 49 KB

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