pparautl.pas 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119
  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]);
  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. else
  479. ;
  480. end;
  481. { Inlining is enabled and supported? }
  482. if (po_inline in pd.procoptions) and
  483. not(cs_do_inline in current_settings.localswitches) then
  484. begin
  485. { Give an error if inline is not supported by the compiler mode,
  486. otherwise only give a hint that this procedure will not be inlined }
  487. if not(m_default_inline in current_settings.modeswitches) then
  488. Message(parser_e_proc_inline_not_supported)
  489. else
  490. Message(parser_h_inlining_disabled);
  491. exclude(pd.procoptions,po_inline);
  492. end;
  493. { For varargs directive also cdecl and external must be defined }
  494. if (po_varargs in pd.procoptions) then
  495. begin
  496. { check first for external in the interface, if available there
  497. then the cdecl must also be there since there is no implementation
  498. available to contain it }
  499. if hcc_declaration in flags then
  500. begin
  501. { if external is available, then cdecl must also be available,
  502. procvars don't need external }
  503. if not((po_external in pd.procoptions) or
  504. (pd.typ=procvardef) or
  505. { for objcclasses this is checked later, because the entire
  506. class may be external. }
  507. is_objc_class_or_protocol(tprocdef(pd).struct)) and
  508. not(pd.proccalloption in (cdecl_pocalls + [pocall_stdcall])) then
  509. Message(parser_e_varargs_need_cdecl_and_external);
  510. end
  511. else
  512. begin
  513. { both must be defined now }
  514. if not((po_external in pd.procoptions) or
  515. (pd.typ=procvardef)) or
  516. not(pd.proccalloption in (cdecl_pocalls + [pocall_stdcall])) then
  517. Message(parser_e_varargs_need_cdecl_and_external);
  518. end;
  519. end;
  520. end;
  521. if hcc_insert_hidden_paras in flags then
  522. begin
  523. { If the paraloc info has been calculated already, it will be missing for
  524. the new parameters we add below. This should never be necessary before
  525. we add them, as users of this information would not process these extra
  526. parameters in that case }
  527. if pd.has_paraloc_info<>callnoside then
  528. internalerror(2019031610);
  529. { insert hidden high parameters }
  530. pd.parast.SymList.ForEachCall(@insert_hidden_para,pd);
  531. { insert hidden self parameter }
  532. insert_self_and_vmt_para(pd);
  533. { insert funcret parameter if required }
  534. insert_funcret_para(pd);
  535. { Make var parameters regable, this must be done after the calling
  536. convention is set. }
  537. { this must be done before parentfp is insert, because getting all cases
  538. where parentfp must be in a memory location isn't catched properly so
  539. we put parentfp never in a register }
  540. pd.parast.SymList.ForEachCall(@set_addr_param_regable,pd);
  541. { insert parentfp parameter if required }
  542. insert_parentfp_para(pd);
  543. end;
  544. { Calculate parameter tlist }
  545. pd.calcparas;
  546. end;
  547. function proc_add_definition(var currpd:tprocdef):boolean;
  548. function check_generic_parameters(fwpd,currpd:tprocdef):boolean;
  549. var
  550. i : longint;
  551. fwtype,
  552. currtype : ttypesym;
  553. begin
  554. result:=true;
  555. if fwpd.genericparas.count<>currpd.genericparas.count then
  556. internalerror(2018090101);
  557. for i:=0 to fwpd.genericparas.count-1 do
  558. begin
  559. fwtype:=ttypesym(fwpd.genericparas[i]);
  560. currtype:=ttypesym(currpd.genericparas[i]);
  561. if fwtype.name<>currtype.name then
  562. begin
  563. messagepos1(currtype.fileinfo,sym_e_generic_type_param_mismatch,currtype.realname);
  564. messagepos1(fwtype.fileinfo,sym_e_generic_type_param_decl,fwtype.realname);
  565. result:=false;
  566. end;
  567. end;
  568. end;
  569. function equal_generic_procdefs(fwpd,currpd:tprocdef):boolean;
  570. var
  571. i : longint;
  572. fwtype,
  573. currtype : ttypesym;
  574. foundretdef : boolean;
  575. begin
  576. result:=false;
  577. if fwpd.genericparas.count<>currpd.genericparas.count then
  578. exit;
  579. { comparing generic declarations is a bit more cumbersome as the
  580. defs of the generic parameter types are not equal, especially if the
  581. declaration contains constraints; essentially we have two cases:
  582. - proc declared in interface of unit (or in class/record/object)
  583. and defined in implementation; here the fwpd might contain
  584. constraints while currpd must only contain undefineddefs
  585. - forward declaration in implementation }
  586. foundretdef:=false;
  587. for i:=0 to fwpd.genericparas.count-1 do
  588. begin
  589. fwtype:=ttypesym(fwpd.genericparas[i]);
  590. currtype:=ttypesym(currpd.genericparas[i]);
  591. { if the type in the currpd isn't a pure undefineddef, then we can
  592. stop right there }
  593. if (currtype.typedef.typ<>undefineddef) or (df_genconstraint in currtype.typedef.defoptions) then
  594. exit;
  595. if not foundretdef then
  596. begin
  597. { if the returndef is the same as this parameter's def then this
  598. needs to be the case for both procdefs }
  599. foundretdef:=fwpd.returndef=fwtype.typedef;
  600. if foundretdef xor (currpd.returndef=currtype.typedef) then
  601. exit;
  602. end;
  603. end;
  604. if compare_paras(fwpd.paras,currpd.paras,cp_none,[cpo_ignorehidden,cpo_openequalisexact,cpo_ignoreuniv,cpo_generic])<>te_exact then
  605. exit;
  606. if not foundretdef then
  607. begin
  608. if (df_specialization in tstoreddef(fwpd.returndef).defoptions) and (df_specialization in tstoreddef(currpd.returndef).defoptions) then
  609. { for specializations we're happy with equal defs instead of exactly the same defs }
  610. result:=equal_defs(fwpd.returndef,currpd.returndef)
  611. else
  612. { the returndef isn't a type parameter, so compare as usual }
  613. result:=compare_defs(fwpd.returndef,currpd.returndef,nothingn)=te_exact;
  614. end
  615. else
  616. result:=true;
  617. end;
  618. {
  619. Add definition aprocdef to the overloaded definitions of aprocsym. If a
  620. forwarddef is found and reused it returns true
  621. }
  622. var
  623. fwpd : tprocdef;
  624. currparasym,
  625. fwparasym : tsym;
  626. currparacnt,
  627. fwparacnt,
  628. curridx,
  629. fwidx,
  630. i : longint;
  631. po_comp : tprocoptions;
  632. paracompopt: tcompare_paras_options;
  633. forwardfound : boolean;
  634. symentry: TSymEntry;
  635. item : tlinkedlistitem;
  636. begin
  637. forwardfound:=false;
  638. { check overloaded functions if the same function already exists }
  639. for i:=0 to tprocsym(currpd.procsym).ProcdefList.Count-1 do
  640. begin
  641. fwpd:=tprocdef(tprocsym(currpd.procsym).ProcdefList[i]);
  642. { can happen for internally generated routines }
  643. if (fwpd=currpd) then
  644. begin
  645. result:=true;
  646. exit;
  647. end;
  648. { Skip overloaded definitions that are declared in other units }
  649. if fwpd.procsym<>currpd.procsym then
  650. continue;
  651. { check the parameters, for delphi/tp it is possible to
  652. leave the parameters away in the implementation (forwarddef=false).
  653. But for an overload declared function this is not allowed }
  654. if { check if empty implementation arguments match is allowed }
  655. (
  656. not(m_repeat_forward in current_settings.modeswitches) and
  657. not(currpd.forwarddef) and
  658. is_bareprocdef(currpd) and
  659. not(po_overload in fwpd.procoptions)
  660. ) or
  661. (
  662. fwpd.is_generic and
  663. currpd.is_generic and
  664. equal_generic_procdefs(fwpd,currpd)
  665. ) or
  666. { check arguments, we need to check only the user visible parameters. The hidden parameters
  667. can be in a different location because of the calling convention, eg. L-R vs. R-L order (PFV)
  668. don't check default values here, because routines that are the same except for their default
  669. values should be reported as mismatches (since you can't overload based on different default
  670. parameter values) }
  671. (
  672. (compare_paras(fwpd.paras,currpd.paras,cp_none,[cpo_ignorehidden,cpo_openequalisexact,cpo_ignoreuniv])=te_exact) and
  673. (compare_defs(fwpd.returndef,currpd.returndef,nothingn)=te_exact)
  674. ) then
  675. begin
  676. { Check if we've found the forwarddef, if found then
  677. we need to update the forward def with the current
  678. implementation settings }
  679. if fwpd.forwarddef then
  680. begin
  681. forwardfound:=true;
  682. if not(m_repeat_forward in current_settings.modeswitches) and
  683. (fwpd.proccalloption<>currpd.proccalloption) then
  684. paracompopt:=[cpo_ignorehidden,cpo_comparedefaultvalue,cpo_openequalisexact,cpo_ignoreuniv]
  685. else
  686. paracompopt:=[cpo_comparedefaultvalue,cpo_openequalisexact,cpo_ignoreuniv];
  687. { Check calling convention }
  688. if (fwpd.proccalloption<>currpd.proccalloption) then
  689. begin
  690. { In delphi it is possible to specify the calling
  691. convention in the interface or implementation if
  692. there was no convention specified in the other
  693. part }
  694. if (m_delphi in current_settings.modeswitches) then
  695. begin
  696. if not(po_hascallingconvention in currpd.procoptions) then
  697. currpd.proccalloption:=fwpd.proccalloption
  698. else
  699. if not(po_hascallingconvention in fwpd.procoptions) then
  700. fwpd.proccalloption:=currpd.proccalloption
  701. else
  702. begin
  703. MessagePos(currpd.fileinfo,parser_e_call_convention_dont_match_forward);
  704. tprocsym(currpd.procsym).write_parameter_lists(currpd);
  705. { restore interface settings }
  706. currpd.proccalloption:=fwpd.proccalloption;
  707. end;
  708. end
  709. else
  710. begin
  711. MessagePos(currpd.fileinfo,parser_e_call_convention_dont_match_forward);
  712. tprocsym(currpd.procsym).write_parameter_lists(currpd);
  713. { restore interface settings }
  714. currpd.proccalloption:=fwpd.proccalloption;
  715. end;
  716. end;
  717. { Check static }
  718. if (po_staticmethod in fwpd.procoptions) then
  719. begin
  720. if not (po_staticmethod in currpd.procoptions) then
  721. begin
  722. include(currpd.procoptions, po_staticmethod);
  723. if (po_classmethod in currpd.procoptions) then
  724. begin
  725. { remove self from the hidden paras }
  726. symentry:=currpd.parast.Find('self');
  727. if symentry<>nil then
  728. begin
  729. currpd.parast.Delete(symentry);
  730. currpd.calcparas;
  731. end;
  732. end;
  733. end;
  734. end;
  735. { Check if the procedure type and return type are correct,
  736. also the parameters must match also with the type and that
  737. if the implementation has default parameters, the interface
  738. also has them and that if they both have them, that they
  739. have the same value }
  740. if ((m_repeat_forward in current_settings.modeswitches) or
  741. not is_bareprocdef(currpd)) and
  742. (
  743. (
  744. fwpd.is_generic and
  745. currpd.is_generic and
  746. not equal_generic_procdefs(fwpd,currpd)
  747. ) or
  748. (
  749. (
  750. not fwpd.is_generic or
  751. not currpd.is_generic
  752. ) and
  753. (
  754. (compare_paras(fwpd.paras,currpd.paras,cp_all,paracompopt)<>te_exact) or
  755. (compare_defs(fwpd.returndef,currpd.returndef,nothingn)<>te_exact)
  756. )
  757. )
  758. ) then
  759. begin
  760. MessagePos1(currpd.fileinfo,parser_e_header_dont_match_forward,
  761. fwpd.fullprocname(false));
  762. tprocsym(currpd.procsym).write_parameter_lists(currpd);
  763. break;
  764. end;
  765. { Check if both are declared forward }
  766. if fwpd.forwarddef and currpd.forwarddef then
  767. begin
  768. MessagePos1(currpd.fileinfo,parser_e_function_already_declared_public_forward,
  769. currpd.fullprocname(false));
  770. end;
  771. { internconst or internproc only need to be defined once }
  772. if (fwpd.proccalloption=pocall_internproc) then
  773. currpd.proccalloption:=fwpd.proccalloption
  774. else
  775. if (currpd.proccalloption=pocall_internproc) then
  776. fwpd.proccalloption:=currpd.proccalloption;
  777. { Check procedure options, Delphi requires that class is
  778. repeated in the implementation for class methods }
  779. if (m_fpc in current_settings.modeswitches) then
  780. po_comp:=[po_classmethod,po_varargs,po_methodpointer,po_interrupt]
  781. else
  782. po_comp:=[po_classmethod,po_methodpointer];
  783. if ((po_comp * fwpd.procoptions)<>(po_comp * currpd.procoptions)) or
  784. (fwpd.proctypeoption <> currpd.proctypeoption) or
  785. { if the implementation version has an "overload" modifier,
  786. the interface version must also have it (otherwise we can
  787. get annoying crashes due to interface crc changes) }
  788. (not(po_overload in fwpd.procoptions) and
  789. (po_overload in currpd.procoptions)) or
  790. { same with noreturn }
  791. (not(po_noreturn in fwpd.procoptions) and
  792. (po_noreturn in currpd.procoptions)) then
  793. begin
  794. MessagePos1(currpd.fileinfo,parser_e_header_dont_match_forward,
  795. fwpd.fullprocname(false));
  796. tprocsym(fwpd.procsym).write_parameter_lists(fwpd);
  797. { This error is non-fatal, we can recover }
  798. end;
  799. { Forward declaration is external? }
  800. if (po_external in fwpd.procoptions) then
  801. MessagePos(currpd.fileinfo,parser_e_proc_already_external);
  802. { check for conflicts with "virtual" if this is a virtual
  803. method, as "virtual" cannot be repeated in the
  804. implementation and hence does not get checked against }
  805. if (po_virtualmethod in fwpd.procoptions) then
  806. begin
  807. po_comp:=currpd.procoptions*PD_VIRTUAL_MUTEXCLPO;
  808. if po_comp<>[] then
  809. MessagePos2(currpd.fileinfo,parser_e_proc_dir_conflict,tokeninfo^[_VIRTUAL].str,get_first_proc_str(po_comp));
  810. end;
  811. { Check parameters }
  812. if (m_repeat_forward in current_settings.modeswitches) or
  813. (currpd.minparacount>0) then
  814. begin
  815. { If mangled names are equal then they have the same amount of arguments }
  816. { We can check the names of the arguments }
  817. { both symtables are in the same order from left to right }
  818. curridx:=0;
  819. fwidx:=0;
  820. currparacnt:=currpd.parast.SymList.Count;
  821. fwparacnt:=fwpd.parast.SymList.Count;
  822. repeat
  823. { skip default parameter constsyms }
  824. while (curridx<currparacnt) and
  825. (tsym(currpd.parast.SymList[curridx]).typ<>paravarsym) do
  826. inc(curridx);
  827. while (fwidx<fwparacnt) and
  828. (tsym(fwpd.parast.SymList[fwidx]).typ<>paravarsym) do
  829. inc(fwidx);
  830. { stop when one of the two lists is at the end }
  831. if (fwidx>=fwparacnt) or (curridx>=currparacnt) then
  832. break;
  833. { compare names of parameters, ignore implictly
  834. renamed parameters }
  835. currparasym:=tsym(currpd.parast.SymList[curridx]);
  836. fwparasym:=tsym(fwpd.parast.SymList[fwidx]);
  837. if not(sp_implicitrename in currparasym.symoptions) and
  838. not(sp_implicitrename in fwparasym.symoptions) then
  839. begin
  840. if (currparasym.name<>fwparasym.name) then
  841. begin
  842. MessagePos3(currpd.fileinfo,parser_e_header_different_var_names,
  843. tprocsym(currpd.procsym).realname,fwparasym.realname,currparasym.realname);
  844. break;
  845. end;
  846. end;
  847. { next parameter }
  848. inc(curridx);
  849. inc(fwidx);
  850. until false;
  851. end;
  852. { check that the type parameter names for generic methods match;
  853. we check this here and not in equal_generic_procdefs as the defs
  854. might still be different due to their parameters, so we'd generate
  855. errors without any need }
  856. if currpd.is_generic and fwpd.is_generic then
  857. { an error here is recoverable, so we simply continue }
  858. check_generic_parameters(fwpd,currpd);
  859. { Everything is checked, now we can update the forward declaration
  860. with the new data from the implementation }
  861. fwpd.forwarddef:=currpd.forwarddef;
  862. fwpd.hasforward:=true;
  863. fwpd.procoptions:=fwpd.procoptions+currpd.procoptions;
  864. { marked as local but exported from unit? }
  865. if (po_kylixlocal in fwpd.procoptions) and (fwpd.owner.symtabletype=globalsymtable) then
  866. MessagePos(fwpd.fileinfo,type_e_cant_export_local);
  867. if fwpd.extnumber=$ffff then
  868. fwpd.extnumber:=currpd.extnumber;
  869. while not currpd.aliasnames.empty do
  870. fwpd.aliasnames.insert(currpd.aliasnames.getfirst);
  871. { update fileinfo so position references the implementation,
  872. also update funcretsym if it is already generated }
  873. fwpd.fileinfo:=currpd.fileinfo;
  874. if assigned(fwpd.funcretsym) then
  875. fwpd.funcretsym.fileinfo:=currpd.fileinfo;
  876. if assigned(currpd.deprecatedmsg) then
  877. begin
  878. stringdispose(fwpd.deprecatedmsg);
  879. fwpd.deprecatedmsg:=stringdup(currpd.deprecatedmsg^);
  880. end;
  881. { import names }
  882. if assigned(currpd.import_dll) then
  883. begin
  884. stringdispose(fwpd.import_dll);
  885. fwpd.import_dll:=stringdup(currpd.import_dll^);
  886. end;
  887. if assigned(currpd.import_name) then
  888. begin
  889. stringdispose(fwpd.import_name);
  890. fwpd.import_name:=stringdup(currpd.import_name^);
  891. end;
  892. fwpd.import_nr:=currpd.import_nr;
  893. { for compilerproc defines we need to rename and update the
  894. symbolname to lowercase so users can' access it (can't do
  895. it immediately, because then the implementation symbol
  896. won't be matched) }
  897. if po_compilerproc in fwpd.procoptions then
  898. begin
  899. fwpd.setcompilerprocname;
  900. current_module.add_public_asmsym(fwpd.procsym.realname,AB_GLOBAL,AT_FUNCTION);
  901. end;
  902. if po_public in fwpd.procoptions then
  903. begin
  904. item:=fwpd.aliasnames.first;
  905. while assigned(item) do
  906. begin
  907. current_module.add_public_asmsym(TCmdStrListItem(item).str,AB_GLOBAL,AT_FUNCTION);
  908. item:=item.next;
  909. end;
  910. end;
  911. { Release current procdef }
  912. currpd.owner.deletedef(currpd);
  913. currpd:=fwpd;
  914. end
  915. else
  916. begin
  917. { abstract methods aren't forward defined, but this }
  918. { needs another error message }
  919. if (po_abstractmethod in fwpd.procoptions) then
  920. MessagePos(currpd.fileinfo,parser_e_abstract_no_definition)
  921. else
  922. begin
  923. MessagePos(currpd.fileinfo,parser_e_overloaded_have_same_parameters);
  924. tprocsym(currpd.procsym).write_parameter_lists(currpd);
  925. end;
  926. end;
  927. { we found one proc with the same arguments, there are no others
  928. so we can stop }
  929. break;
  930. end;
  931. { check for allowing overload directive }
  932. if not(m_fpc in current_settings.modeswitches) then
  933. begin
  934. { overload directive turns on overloading }
  935. if ((po_overload in currpd.procoptions) or
  936. (po_overload in fwpd.procoptions)) then
  937. begin
  938. { check if all procs have overloading, but not if the proc is a method or
  939. already declared forward, then the check is already done }
  940. if not(fwpd.hasforward or
  941. assigned(currpd.struct) or
  942. (currpd.forwarddef<>fwpd.forwarddef) or
  943. ((po_overload in currpd.procoptions) and
  944. (po_overload in fwpd.procoptions))) then
  945. begin
  946. MessagePos1(currpd.fileinfo,parser_e_no_overload_for_all_procs,currpd.procsym.realname);
  947. break;
  948. end
  949. end
  950. else
  951. begin
  952. if not(fwpd.forwarddef) then
  953. begin
  954. if (m_tp7 in current_settings.modeswitches) then
  955. MessagePos(currpd.fileinfo,parser_e_procedure_overloading_is_off)
  956. else
  957. MessagePos1(currpd.fileinfo,parser_e_no_overload_for_all_procs,currpd.procsym.realname);
  958. break;
  959. end;
  960. end;
  961. end; { equal arguments }
  962. end;
  963. { if we didn't reuse a forwarddef then we add the procdef to the overloaded
  964. list }
  965. if not forwardfound then
  966. begin
  967. { can happen in Delphi mode }
  968. if (currpd.proctypeoption = potype_function) and
  969. is_void(currpd.returndef) then
  970. MessagePos1(currpd.fileinfo,parser_e_no_funcret_specified,currpd.procsym.realname);
  971. tprocsym(currpd.procsym).ProcdefList.Add(currpd);
  972. if not currpd.forwarddef and (po_public in currpd.procoptions) then
  973. begin
  974. item:=currpd.aliasnames.first;
  975. while assigned(item) do
  976. begin
  977. current_module.add_public_asmsym(TCmdStrListItem(item).str,AB_GLOBAL,AT_FUNCTION);
  978. item:=item.next;
  979. end;
  980. end;
  981. end;
  982. proc_add_definition:=forwardfound;
  983. end;
  984. procedure build_parentfpstruct(pd: tprocdef);
  985. var
  986. nestedvars: tsym;
  987. nestedvarsst: tsymtable;
  988. pnestedvarsdef,
  989. nestedvarsdef: tdef;
  990. old_symtablestack: tsymtablestack;
  991. begin
  992. { make sure the defs are not registered in the current symtablestack,
  993. because they may be for a parent procdef (changeowner does remove a def
  994. from the symtable in which it was originally created, so that by itself
  995. is not enough) }
  996. old_symtablestack:=symtablestack;
  997. symtablestack:=old_symtablestack.getcopyuntil(current_module.localsymtable);
  998. { create struct to hold local variables and parameters that are
  999. accessed from within nested routines (start with extra dollar to prevent
  1000. the JVM from thinking this is a nested class in the unit) }
  1001. nestedvarsst:=trecordsymtable.create('$'+current_module.realmodulename^+'$$_fpc_nestedvars$'+pd.unique_id_str,
  1002. current_settings.alignment.localalignmax,current_settings.alignment.localalignmin);
  1003. nestedvarsdef:=crecorddef.create(nestedvarsst.name^,nestedvarsst);
  1004. {$ifdef jvm}
  1005. maybe_guarantee_record_typesym(nestedvarsdef,nestedvarsdef.owner);
  1006. { don't add clone/FpcDeepCopy, because the field names are not all
  1007. representable in source form and we don't need them anyway }
  1008. symtablestack.push(trecorddef(nestedvarsdef).symtable);
  1009. maybe_add_public_default_java_constructor(trecorddef(nestedvarsdef));
  1010. insert_struct_hidden_paras(trecorddef(nestedvarsdef));
  1011. symtablestack.pop(trecorddef(nestedvarsdef).symtable);
  1012. {$endif}
  1013. symtablestack.free;
  1014. symtablestack:=old_symtablestack.getcopyuntil(pd.localst);
  1015. pnestedvarsdef:=cpointerdef.getreusable(nestedvarsdef);
  1016. if not(po_assembler in pd.procoptions) then
  1017. begin
  1018. nestedvars:=clocalvarsym.create('$nestedvars',vs_var,nestedvarsdef,[]);
  1019. include(nestedvars.symoptions,sp_internal);
  1020. pd.localst.insert(nestedvars);
  1021. pd.parentfpstruct:=nestedvars;
  1022. pd.parentfpinitblock:=cblocknode.create(nil);
  1023. end;
  1024. symtablestack.free;
  1025. pd.parentfpstructptrtype:=pnestedvarsdef;
  1026. symtablestack:=old_symtablestack;
  1027. end;
  1028. end.