pparautl.pas 49 KB

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