defcmp.pas 84 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. Compare definitions and parameter lists
  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 defcmp;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. cclasses,
  22. globtype,globals,
  23. node,
  24. symconst,symtype,symbase,symdef;
  25. type
  26. { if acp is cp_all the var const or nothing are considered equal }
  27. tcompare_paras_type = ( cp_none, cp_value_equal_const, cp_all,cp_procvar);
  28. tcompare_paras_option = (
  29. cpo_allowdefaults,
  30. cpo_ignorehidden, // ignore hidden parameters
  31. cpo_allowconvert,
  32. cpo_comparedefaultvalue,
  33. cpo_openequalisexact,
  34. cpo_ignoreuniv,
  35. cpo_warn_incompatible_univ,
  36. cpo_ignorevarspez, // ignore parameter access type
  37. cpo_ignoreframepointer // ignore frame pointer parameter (for assignment-compatibility of global procedures to nested procvars)
  38. );
  39. tcompare_paras_options = set of tcompare_paras_option;
  40. tcompare_defs_option = (cdo_internal,cdo_explicit,cdo_check_operator,cdo_allow_variant,cdo_parameter,cdo_warn_incompatible_univ);
  41. tcompare_defs_options = set of tcompare_defs_option;
  42. tconverttype = (tc_none,
  43. tc_equal,
  44. tc_not_possible,
  45. tc_string_2_string,
  46. tc_char_2_string,
  47. tc_char_2_chararray,
  48. tc_pchar_2_string,
  49. tc_cchar_2_pchar,
  50. tc_cstring_2_pchar,
  51. tc_cstring_2_int,
  52. tc_ansistring_2_pchar,
  53. tc_string_2_chararray,
  54. tc_chararray_2_string,
  55. tc_array_2_pointer,
  56. tc_pointer_2_array,
  57. tc_int_2_int,
  58. tc_int_2_bool,
  59. tc_bool_2_bool,
  60. tc_bool_2_int,
  61. tc_real_2_real,
  62. tc_int_2_real,
  63. tc_real_2_currency,
  64. tc_proc_2_procvar,
  65. tc_nil_2_methodprocvar,
  66. tc_arrayconstructor_2_set,
  67. tc_set_to_set,
  68. tc_cord_2_pointer,
  69. tc_intf_2_string,
  70. tc_intf_2_guid,
  71. tc_class_2_intf,
  72. tc_char_2_char,
  73. tc_dynarray_2_openarray,
  74. tc_pwchar_2_string,
  75. tc_variant_2_dynarray,
  76. tc_dynarray_2_variant,
  77. tc_variant_2_enum,
  78. tc_enum_2_variant,
  79. tc_interface_2_variant,
  80. tc_variant_2_interface,
  81. tc_array_2_dynarray
  82. );
  83. function compare_defs_ext(def_from,def_to : tdef;
  84. fromtreetype : tnodetype;
  85. var doconv : tconverttype;
  86. var operatorpd : tprocdef;
  87. cdoptions:tcompare_defs_options):tequaltype;
  88. { Returns if the type def_from can be converted to def_to or if both types are equal }
  89. function compare_defs(def_from,def_to:tdef;fromtreetype:tnodetype):tequaltype;
  90. { Returns true, if def1 and def2 are semantically the same }
  91. function equal_defs(def_from,def_to:tdef):boolean;
  92. { Checks for type compatibility (subgroups of type)
  93. used for case statements... probably missing stuff
  94. to use on other types }
  95. function is_subequal(def1, def2: tdef): boolean;
  96. {# true, if two parameter lists are equal
  97. if acp is cp_all, all have to match exactly
  98. if acp is cp_value_equal_const call by value
  99. and call by const parameter are assumed as
  100. equal
  101. if acp is cp_procvar then the varspez have to match,
  102. and all parameter types must be at least te_equal
  103. if acp is cp_none, then we don't check the varspez at all
  104. allowdefaults indicates if default value parameters
  105. are allowed (in this case, the search order will first
  106. search for a routine with default parameters, before
  107. searching for the same definition with no parameters)
  108. }
  109. function compare_paras(para1,para2 : TFPObjectList; acp : tcompare_paras_type; cpoptions: tcompare_paras_options):tequaltype;
  110. { True if a function can be assigned to a procvar }
  111. { changed first argument type to pabstractprocdef so that it can also be }
  112. { used to test compatibility between two pprocvardefs (JM) }
  113. function proc_to_procvar_equal(def1:tabstractprocdef;def2:tprocvardef;checkincompatibleuniv: boolean):tequaltype;
  114. { Parentdef is the definition of a method defined in a parent class or interface }
  115. { Childdef is the definition of a method defined in a child class, interface or }
  116. { a class implementing an interface with parentdef. }
  117. { Returns true if the resultdef of childdef can be used to implement/override }
  118. { parentdef's resultdef }
  119. function compatible_childmethod_resultdef(parentretdef, childretdef: tdef): boolean;
  120. implementation
  121. uses
  122. verbose,systems,constexp,
  123. symtable,symsym,
  124. defutil,symutil;
  125. function compare_defs_ext(def_from,def_to : tdef;
  126. fromtreetype : tnodetype;
  127. var doconv : tconverttype;
  128. var operatorpd : tprocdef;
  129. cdoptions:tcompare_defs_options):tequaltype;
  130. { tordtype:
  131. uvoid,
  132. u8bit,u16bit,u32bit,u64bit,
  133. s8bit,s16bit,s32bit,s64bit,
  134. bool8bit,bool16bit,bool32bit,bool64bit,
  135. uchar,uwidechar }
  136. type
  137. tbasedef=(bvoid,bchar,bint,bbool);
  138. const
  139. basedeftbl:array[tordtype] of tbasedef =
  140. (bvoid,
  141. bint,bint,bint,bint,
  142. bint,bint,bint,bint,
  143. bbool,bbool,bbool,bbool,bbool,
  144. bchar,bchar,bint);
  145. basedefconvertsimplicit : array[tbasedef,tbasedef] of tconverttype =
  146. { void, char, int, bool }
  147. ((tc_not_possible,tc_not_possible,tc_not_possible,tc_not_possible),
  148. (tc_not_possible,tc_char_2_char,tc_not_possible,tc_not_possible),
  149. (tc_not_possible,tc_not_possible,tc_int_2_int,tc_not_possible),
  150. (tc_not_possible,tc_not_possible,tc_not_possible,tc_bool_2_bool));
  151. basedefconvertsexplicit : array[tbasedef,tbasedef] of tconverttype =
  152. { void, char, int, bool }
  153. ((tc_not_possible,tc_not_possible,tc_not_possible,tc_not_possible),
  154. (tc_not_possible,tc_char_2_char,tc_int_2_int,tc_int_2_bool),
  155. (tc_not_possible,tc_int_2_int,tc_int_2_int,tc_int_2_bool),
  156. (tc_not_possible,tc_bool_2_int,tc_bool_2_int,tc_bool_2_bool));
  157. var
  158. subeq,eq : tequaltype;
  159. hd1,hd2 : tdef;
  160. hct : tconverttype;
  161. hobjdef : tobjectdef;
  162. hpd : tprocdef;
  163. begin
  164. eq:=te_incompatible;
  165. doconv:=tc_not_possible;
  166. { safety check }
  167. if not(assigned(def_from) and assigned(def_to)) then
  168. begin
  169. compare_defs_ext:=te_incompatible;
  170. exit;
  171. end;
  172. { same def? then we've an exact match }
  173. if def_from=def_to then
  174. begin
  175. doconv:=tc_equal;
  176. compare_defs_ext:=te_exact;
  177. exit;
  178. end;
  179. { undefined def? then mark it as equal }
  180. if (def_from.typ=undefineddef) or
  181. (def_to.typ=undefineddef) then
  182. begin
  183. doconv:=tc_equal;
  184. compare_defs_ext:=te_exact;
  185. exit;
  186. end;
  187. { we walk the wanted (def_to) types and check then the def_from
  188. types if there is a conversion possible }
  189. case def_to.typ of
  190. orddef :
  191. begin
  192. case def_from.typ of
  193. orddef :
  194. begin
  195. if (torddef(def_from).ordtype=torddef(def_to).ordtype) then
  196. begin
  197. case torddef(def_from).ordtype of
  198. uchar,uwidechar,
  199. u8bit,u16bit,u32bit,u64bit,
  200. s8bit,s16bit,s32bit,s64bit:
  201. begin
  202. if (torddef(def_from).low>=torddef(def_to).low) and
  203. (torddef(def_from).high<=torddef(def_to).high) then
  204. eq:=te_equal
  205. else
  206. begin
  207. doconv:=tc_int_2_int;
  208. eq:=te_convert_l1;
  209. end;
  210. end;
  211. uvoid,
  212. pasbool,bool8bit,bool16bit,bool32bit,bool64bit:
  213. eq:=te_equal;
  214. else
  215. internalerror(200210061);
  216. end;
  217. end
  218. else
  219. begin
  220. if cdo_explicit in cdoptions then
  221. doconv:=basedefconvertsexplicit[basedeftbl[torddef(def_from).ordtype],basedeftbl[torddef(def_to).ordtype]]
  222. else
  223. doconv:=basedefconvertsimplicit[basedeftbl[torddef(def_from).ordtype],basedeftbl[torddef(def_to).ordtype]];
  224. if (doconv=tc_not_possible) then
  225. eq:=te_incompatible
  226. else if (not is_in_limit(def_from,def_to)) then
  227. { "punish" bad type conversions :) (JM) }
  228. eq:=te_convert_l3
  229. else
  230. eq:=te_convert_l1;
  231. end;
  232. end;
  233. enumdef :
  234. begin
  235. { needed for char(enum) }
  236. if cdo_explicit in cdoptions then
  237. begin
  238. doconv:=tc_int_2_int;
  239. eq:=te_convert_l1;
  240. end;
  241. end;
  242. floatdef :
  243. begin
  244. if is_currency(def_to) then
  245. begin
  246. doconv:=tc_real_2_currency;
  247. eq:=te_convert_l2;
  248. end;
  249. end;
  250. objectdef:
  251. begin
  252. if (m_delphi in current_settings.modeswitches) and
  253. is_implicit_pointer_object_type(def_from) and
  254. (cdo_explicit in cdoptions) then
  255. begin
  256. eq:=te_convert_l1;
  257. if (fromtreetype=niln) then
  258. begin
  259. { will be handled by the constant folding }
  260. doconv:=tc_equal;
  261. end
  262. else
  263. doconv:=tc_int_2_int;
  264. end;
  265. end;
  266. classrefdef,
  267. procvardef,
  268. pointerdef :
  269. begin
  270. if cdo_explicit in cdoptions then
  271. begin
  272. eq:=te_convert_l1;
  273. if (fromtreetype=niln) then
  274. begin
  275. { will be handled by the constant folding }
  276. doconv:=tc_equal;
  277. end
  278. else
  279. doconv:=tc_int_2_int;
  280. end;
  281. end;
  282. arraydef :
  283. begin
  284. if (m_mac in current_settings.modeswitches) and
  285. (fromtreetype=stringconstn) then
  286. begin
  287. eq:=te_convert_l3;
  288. doconv:=tc_cstring_2_int;
  289. end;
  290. end;
  291. end;
  292. end;
  293. stringdef :
  294. begin
  295. case def_from.typ of
  296. stringdef :
  297. begin
  298. { Constant string }
  299. if (fromtreetype=stringconstn) then
  300. begin
  301. if (tstringdef(def_from).stringtype=tstringdef(def_to).stringtype) then
  302. eq:=te_equal
  303. else
  304. begin
  305. doconv:=tc_string_2_string;
  306. { Don't prefer conversions from widestring to a
  307. normal string as we can lose information }
  308. if (tstringdef(def_from).stringtype in [st_widestring,st_unicodestring]) and
  309. not (tstringdef(def_to).stringtype in [st_widestring,st_unicodestring]) then
  310. eq:=te_convert_l3
  311. else if tstringdef(def_to).stringtype in [st_widestring,st_unicodestring] then
  312. eq:=te_convert_l2
  313. else
  314. eq:=te_equal;
  315. end;
  316. end
  317. else
  318. { Same string type, for shortstrings also the length must match }
  319. if (tstringdef(def_from).stringtype=tstringdef(def_to).stringtype) and
  320. ((tstringdef(def_from).stringtype<>st_shortstring) or
  321. (tstringdef(def_from).len=tstringdef(def_to).len)) then
  322. eq:=te_equal
  323. else
  324. begin
  325. doconv:=tc_string_2_string;
  326. case tstringdef(def_from).stringtype of
  327. st_widestring :
  328. begin
  329. { Prefer conversions to unicodestring }
  330. if tstringdef(def_to).stringtype=st_unicodestring then
  331. eq:=te_convert_l1
  332. { else prefer conversions to ansistring }
  333. else if tstringdef(def_to).stringtype=st_ansistring then
  334. eq:=te_convert_l2
  335. else
  336. eq:=te_convert_l3;
  337. end;
  338. st_unicodestring :
  339. begin
  340. { Prefer conversions to widestring }
  341. if tstringdef(def_to).stringtype=st_widestring then
  342. eq:=te_convert_l1
  343. { else prefer conversions to ansistring }
  344. else if tstringdef(def_to).stringtype=st_ansistring then
  345. eq:=te_convert_l2
  346. else
  347. eq:=te_convert_l3;
  348. end;
  349. st_shortstring :
  350. begin
  351. { Prefer shortstrings of different length or conversions
  352. from shortstring to ansistring }
  353. if (tstringdef(def_to).stringtype=st_shortstring) then
  354. eq:=te_convert_l1
  355. else if tstringdef(def_to).stringtype=st_ansistring then
  356. eq:=te_convert_l2
  357. else
  358. eq:=te_convert_l3;
  359. end;
  360. st_ansistring :
  361. begin
  362. { Prefer conversion to widestrings }
  363. if (tstringdef(def_to).stringtype in [st_widestring,st_unicodestring]) then
  364. eq:=te_convert_l2
  365. else
  366. eq:=te_convert_l3;
  367. end;
  368. end;
  369. end;
  370. end;
  371. orddef :
  372. begin
  373. { char to string}
  374. if is_char(def_from) or
  375. is_widechar(def_from) then
  376. begin
  377. doconv:=tc_char_2_string;
  378. eq:=te_convert_l1;
  379. end;
  380. end;
  381. arraydef :
  382. begin
  383. { array of char to string, the length check is done by the firstpass of this node }
  384. if is_chararray(def_from) or is_open_chararray(def_from) then
  385. begin
  386. { "Untyped" stringconstn is an array of char }
  387. if fromtreetype=stringconstn then
  388. begin
  389. doconv:=tc_string_2_string;
  390. { prefered string type depends on the $H switch }
  391. if not(cs_ansistrings in current_settings.localswitches) and
  392. (tstringdef(def_to).stringtype=st_shortstring) then
  393. eq:=te_equal
  394. else if (cs_ansistrings in current_settings.localswitches) and
  395. (tstringdef(def_to).stringtype=st_ansistring) then
  396. eq:=te_equal
  397. else if tstringdef(def_to).stringtype in [st_widestring,st_unicodestring] then
  398. eq:=te_convert_l3
  399. else
  400. eq:=te_convert_l1;
  401. end
  402. else
  403. begin
  404. doconv:=tc_chararray_2_string;
  405. if is_open_array(def_from) then
  406. begin
  407. if is_ansistring(def_to) then
  408. eq:=te_convert_l1
  409. else if is_widestring(def_to) or is_unicodestring(def_to) then
  410. eq:=te_convert_l3
  411. else
  412. eq:=te_convert_l2;
  413. end
  414. else
  415. begin
  416. if is_shortstring(def_to) then
  417. begin
  418. { Only compatible with arrays that fit
  419. smaller than 255 chars }
  420. if (def_from.size <= 255) then
  421. eq:=te_convert_l1;
  422. end
  423. else if is_ansistring(def_to) then
  424. begin
  425. if (def_from.size > 255) then
  426. eq:=te_convert_l1
  427. else
  428. eq:=te_convert_l2;
  429. end
  430. else if is_widestring(def_to) or is_unicodestring(def_to) then
  431. eq:=te_convert_l3
  432. else
  433. eq:=te_convert_l2;
  434. end;
  435. end;
  436. end
  437. else
  438. { array of widechar to string, the length check is done by the firstpass of this node }
  439. if is_widechararray(def_from) or is_open_widechararray(def_from) then
  440. begin
  441. doconv:=tc_chararray_2_string;
  442. if is_widestring(def_to) or is_unicodestring(def_to) then
  443. eq:=te_convert_l1
  444. else
  445. { size of widechar array is double due the sizeof a widechar }
  446. if not(is_shortstring(def_to) and (is_open_widechararray(def_from) or (def_from.size>255*sizeof(widechar)))) then
  447. eq:=te_convert_l3
  448. else
  449. eq:=te_convert_l2;
  450. end;
  451. end;
  452. pointerdef :
  453. begin
  454. { pchar can be assigned to short/ansistrings,
  455. but not in tp7 compatible mode }
  456. if not(m_tp7 in current_settings.modeswitches) then
  457. begin
  458. if is_pchar(def_from) then
  459. begin
  460. doconv:=tc_pchar_2_string;
  461. { prefer ansistrings because pchars can overflow shortstrings, }
  462. { but only if ansistrings are the default (JM) }
  463. if (is_shortstring(def_to) and
  464. not(cs_ansistrings in current_settings.localswitches)) or
  465. (is_ansistring(def_to) and
  466. (cs_ansistrings in current_settings.localswitches)) then
  467. eq:=te_convert_l1
  468. else
  469. eq:=te_convert_l2;
  470. end
  471. else if is_pwidechar(def_from) then
  472. begin
  473. doconv:=tc_pwchar_2_string;
  474. if is_widestring(def_to) or is_unicodestring(def_to) then
  475. eq:=te_convert_l1
  476. else
  477. eq:=te_convert_l3;
  478. end;
  479. end;
  480. end;
  481. objectdef :
  482. begin
  483. { corba interface -> id string }
  484. if is_interfacecorba(def_from) then
  485. begin
  486. doconv:=tc_intf_2_string;
  487. eq:=te_convert_l1;
  488. end;
  489. end;
  490. end;
  491. end;
  492. floatdef :
  493. begin
  494. case def_from.typ of
  495. orddef :
  496. begin { ordinal to real }
  497. { only for implicit and internal typecasts in tp/delphi }
  498. if (([cdo_explicit,cdo_internal] * cdoptions <> [cdo_explicit]) or
  499. ([m_tp7,m_delphi] * current_settings.modeswitches = [])) and
  500. (is_integer(def_from) or
  501. (is_currency(def_from) and
  502. (s64currencytype.typ = floatdef))) then
  503. begin
  504. doconv:=tc_int_2_real;
  505. eq:=te_convert_l4;
  506. end
  507. else if is_currency(def_from)
  508. { and (s64currencytype.typ = orddef)) } then
  509. begin
  510. { prefer conversion to orddef in this case, unless }
  511. { the orddef < currency (then it will get convert l3, }
  512. { and conversion to float is favoured) }
  513. doconv:=tc_int_2_real;
  514. eq:=te_convert_l2;
  515. end;
  516. end;
  517. floatdef :
  518. begin
  519. if tfloatdef(def_from).floattype=tfloatdef(def_to).floattype then
  520. eq:=te_equal
  521. else
  522. begin
  523. { Delphi does not allow explicit type conversions for float types like:
  524. single_var:=single(double_var);
  525. But if such conversion is inserted by compiler (internal) for some purpose,
  526. it should be allowed even in Delphi mode. }
  527. if (fromtreetype=realconstn) or
  528. not((cdoptions*[cdo_explicit,cdo_internal]=[cdo_explicit]) and
  529. (m_delphi in current_settings.modeswitches)) then
  530. begin
  531. doconv:=tc_real_2_real;
  532. { do we lose precision? }
  533. if def_to.size<def_from.size then
  534. eq:=te_convert_l2
  535. else
  536. eq:=te_convert_l1;
  537. end;
  538. end;
  539. end;
  540. end;
  541. end;
  542. enumdef :
  543. begin
  544. case def_from.typ of
  545. enumdef :
  546. begin
  547. if cdo_explicit in cdoptions then
  548. begin
  549. eq:=te_convert_l1;
  550. doconv:=tc_int_2_int;
  551. end
  552. else
  553. begin
  554. hd1:=def_from;
  555. while assigned(tenumdef(hd1).basedef) do
  556. hd1:=tenumdef(hd1).basedef;
  557. hd2:=def_to;
  558. while assigned(tenumdef(hd2).basedef) do
  559. hd2:=tenumdef(hd2).basedef;
  560. if (hd1=hd2) then
  561. begin
  562. eq:=te_convert_l1;
  563. { because of packenum they can have different sizes! (JM) }
  564. doconv:=tc_int_2_int;
  565. end
  566. else
  567. begin
  568. { assignment of an enum symbol to an unique type? }
  569. if (fromtreetype=ordconstn) and
  570. (tenumsym(tenumdef(hd1).getfirstsym)=tenumsym(tenumdef(hd2).getfirstsym)) then
  571. begin
  572. { because of packenum they can have different sizes! (JM) }
  573. eq:=te_convert_l1;
  574. doconv:=tc_int_2_int;
  575. end;
  576. end;
  577. end;
  578. end;
  579. orddef :
  580. begin
  581. if cdo_explicit in cdoptions then
  582. begin
  583. eq:=te_convert_l1;
  584. doconv:=tc_int_2_int;
  585. end;
  586. end;
  587. variantdef :
  588. begin
  589. eq:=te_convert_l1;
  590. doconv:=tc_variant_2_enum;
  591. end;
  592. pointerdef :
  593. begin
  594. { ugly, but delphi allows it }
  595. if (cdo_explicit in cdoptions) and
  596. (m_delphi in current_settings.modeswitches) then
  597. begin
  598. doconv:=tc_int_2_int;
  599. eq:=te_convert_l1;
  600. end;
  601. end;
  602. objectdef:
  603. begin
  604. { ugly, but delphi allows it }
  605. if (m_delphi in current_settings.modeswitches) and
  606. is_class_or_interface_or_dispinterface(def_from) and
  607. (cdo_explicit in cdoptions) then
  608. begin
  609. doconv:=tc_int_2_int;
  610. eq:=te_convert_l1;
  611. end;
  612. end;
  613. end;
  614. end;
  615. arraydef :
  616. begin
  617. { open array is also compatible with a single element of its base type.
  618. the extra check for deftyp is needed because equal defs can also return
  619. true if the def types are not the same, for example with dynarray to pointer. }
  620. if is_open_array(def_to) and
  621. (def_from.typ=tarraydef(def_to).elementdef.typ) and
  622. equal_defs(def_from,tarraydef(def_to).elementdef) then
  623. begin
  624. doconv:=tc_equal;
  625. eq:=te_convert_l1;
  626. end
  627. else
  628. begin
  629. case def_from.typ of
  630. arraydef :
  631. begin
  632. { from/to packed array -- packed chararrays are }
  633. { strings in ISO Pascal (at least if the lower bound }
  634. { is 1, but GPC makes all equal-length chararrays }
  635. { compatible), so treat those the same as regular }
  636. { char arrays }
  637. if (is_packed_array(def_from) and
  638. not is_chararray(def_from) and
  639. not is_widechararray(def_from)) xor
  640. (is_packed_array(def_to) and
  641. not is_chararray(def_to) and
  642. not is_widechararray(def_to)) then
  643. { both must be packed }
  644. begin
  645. compare_defs_ext:=te_incompatible;
  646. exit;
  647. end
  648. { to dynamic array }
  649. else if is_dynamic_array(def_to) then
  650. begin
  651. if equal_defs(tarraydef(def_from).elementdef,tarraydef(def_to).elementdef) then
  652. begin
  653. { dynamic array -> dynamic array }
  654. if is_dynamic_array(def_from) then
  655. eq:=te_equal
  656. { fpc modes only: array -> dyn. array }
  657. else if (current_settings.modeswitches*[m_objfpc,m_fpc]<>[]) and
  658. not(is_special_array(def_from)) and
  659. is_zero_based_array(def_from) then
  660. begin
  661. eq:=te_convert_l2;
  662. doconv:=tc_array_2_dynarray;
  663. end;
  664. end
  665. end
  666. else
  667. { to open array }
  668. if is_open_array(def_to) then
  669. begin
  670. { array constructor -> open array }
  671. if is_array_constructor(def_from) then
  672. begin
  673. if is_void(tarraydef(def_from).elementdef) then
  674. begin
  675. doconv:=tc_equal;
  676. eq:=te_convert_l1;
  677. end
  678. else
  679. begin
  680. subeq:=compare_defs_ext(tarraydef(def_from).elementdef,
  681. tarraydef(def_to).elementdef,
  682. { reason for cdo_allow_variant: see webtbs/tw7070a and webtbs/tw7070b }
  683. arrayconstructorn,hct,hpd,[cdo_check_operator,cdo_allow_variant]);
  684. if (subeq>=te_equal) then
  685. begin
  686. doconv:=tc_equal;
  687. eq:=te_convert_l1;
  688. end
  689. else
  690. if (subeq>te_incompatible) then
  691. begin
  692. doconv:=hct;
  693. eq:=te_convert_l2;
  694. end;
  695. end;
  696. end
  697. else
  698. { dynamic array -> open array }
  699. if is_dynamic_array(def_from) and
  700. equal_defs(tarraydef(def_from).elementdef,tarraydef(def_to).elementdef) then
  701. begin
  702. doconv:=tc_dynarray_2_openarray;
  703. eq:=te_convert_l2;
  704. end
  705. else
  706. { open array -> open array }
  707. if is_open_array(def_from) and
  708. equal_defs(tarraydef(def_from).elementdef,tarraydef(def_to).elementdef) then
  709. if tarraydef(def_from).elementdef=tarraydef(def_to).elementdef then
  710. eq:=te_exact
  711. else
  712. eq:=te_equal
  713. else
  714. { array -> open array }
  715. if not(cdo_parameter in cdoptions) and
  716. equal_defs(tarraydef(def_from).elementdef,tarraydef(def_to).elementdef) then
  717. begin
  718. if fromtreetype=stringconstn then
  719. eq:=te_convert_l1
  720. else
  721. eq:=te_equal;
  722. end;
  723. end
  724. else
  725. { to array of const }
  726. if is_array_of_const(def_to) then
  727. begin
  728. if is_array_of_const(def_from) or
  729. is_array_constructor(def_from) then
  730. begin
  731. eq:=te_equal;
  732. end
  733. else
  734. { array of tvarrec -> array of const }
  735. if equal_defs(tarraydef(def_to).elementdef,tarraydef(def_from).elementdef) then
  736. begin
  737. doconv:=tc_equal;
  738. eq:=te_convert_l1;
  739. end;
  740. end
  741. else
  742. { to array of char, from "Untyped" stringconstn (array of char) }
  743. if (fromtreetype=stringconstn) and
  744. (is_chararray(def_to) or
  745. is_widechararray(def_to)) then
  746. begin
  747. eq:=te_convert_l1;
  748. doconv:=tc_string_2_chararray;
  749. end
  750. else
  751. { other arrays }
  752. begin
  753. { open array -> array }
  754. if not(cdo_parameter in cdoptions) and
  755. is_open_array(def_from) and
  756. equal_defs(tarraydef(def_from).elementdef,tarraydef(def_to).elementdef) then
  757. begin
  758. eq:=te_equal
  759. end
  760. else
  761. { array -> array }
  762. if not(m_tp7 in current_settings.modeswitches) and
  763. not(m_delphi in current_settings.modeswitches) and
  764. (tarraydef(def_from).lowrange=tarraydef(def_to).lowrange) and
  765. (tarraydef(def_from).highrange=tarraydef(def_to).highrange) and
  766. equal_defs(tarraydef(def_from).elementdef,tarraydef(def_to).elementdef) and
  767. equal_defs(tarraydef(def_from).rangedef,tarraydef(def_to).rangedef) then
  768. begin
  769. eq:=te_equal
  770. end;
  771. end;
  772. end;
  773. pointerdef :
  774. begin
  775. { nil and voidpointers are compatible with dyn. arrays }
  776. if is_dynamic_array(def_to) and
  777. ((fromtreetype=niln) or
  778. is_voidpointer(def_from)) then
  779. begin
  780. doconv:=tc_equal;
  781. eq:=te_convert_l1;
  782. end
  783. else
  784. if is_zero_based_array(def_to) and
  785. equal_defs(tpointerdef(def_from).pointeddef,tarraydef(def_to).elementdef) then
  786. begin
  787. doconv:=tc_pointer_2_array;
  788. eq:=te_convert_l1;
  789. end;
  790. end;
  791. stringdef :
  792. begin
  793. { string to char array }
  794. if (not is_special_array(def_to)) and
  795. (is_char(tarraydef(def_to).elementdef)or
  796. is_widechar(tarraydef(def_to).elementdef)) then
  797. begin
  798. doconv:=tc_string_2_chararray;
  799. eq:=te_convert_l1;
  800. end;
  801. end;
  802. orddef:
  803. begin
  804. if is_chararray(def_to) and
  805. is_char(def_from) then
  806. begin
  807. doconv:=tc_char_2_chararray;
  808. eq:=te_convert_l2;
  809. end;
  810. end;
  811. recorddef :
  812. begin
  813. { tvarrec -> array of const }
  814. if is_array_of_const(def_to) and
  815. equal_defs(def_from,tarraydef(def_to).elementdef) then
  816. begin
  817. doconv:=tc_equal;
  818. eq:=te_convert_l1;
  819. end;
  820. end;
  821. variantdef :
  822. begin
  823. if is_dynamic_array(def_to) then
  824. begin
  825. doconv:=tc_variant_2_dynarray;
  826. eq:=te_convert_l1;
  827. end;
  828. end;
  829. end;
  830. end;
  831. end;
  832. variantdef :
  833. begin
  834. if (cdo_allow_variant in cdoptions) then
  835. begin
  836. case def_from.typ of
  837. enumdef :
  838. begin
  839. doconv:=tc_enum_2_variant;
  840. eq:=te_convert_l1;
  841. end;
  842. arraydef :
  843. begin
  844. if is_dynamic_array(def_from) then
  845. begin
  846. doconv:=tc_dynarray_2_variant;
  847. eq:=te_convert_l1;
  848. end;
  849. end;
  850. objectdef :
  851. begin
  852. if is_interface(def_from) then
  853. begin
  854. doconv:=tc_interface_2_variant;
  855. eq:=te_convert_l1;
  856. end;
  857. end;
  858. variantdef :
  859. begin
  860. { doing this in the compiler avoids a lot of unncessary
  861. copying }
  862. if (tvariantdef(def_from).varianttype=vt_olevariant) and
  863. (tvariantdef(def_to).varianttype=vt_normalvariant) then
  864. begin
  865. doconv:=tc_equal;
  866. eq:=te_convert_l1;
  867. end;
  868. end;
  869. end;
  870. end;
  871. end;
  872. pointerdef :
  873. begin
  874. case def_from.typ of
  875. stringdef :
  876. begin
  877. { string constant (which can be part of array constructor)
  878. to zero terminated string constant }
  879. if (fromtreetype = stringconstn) and
  880. (is_pchar(def_to) or is_pwidechar(def_to)) then
  881. begin
  882. doconv:=tc_cstring_2_pchar;
  883. eq:=te_convert_l2;
  884. end
  885. else
  886. if (cdo_explicit in cdoptions) or (fromtreetype = arrayconstructorn) then
  887. begin
  888. { pchar(ansistring) }
  889. if is_pchar(def_to) and
  890. is_ansistring(def_from) then
  891. begin
  892. doconv:=tc_ansistring_2_pchar;
  893. eq:=te_convert_l1;
  894. end
  895. else
  896. { pwidechar(widestring) }
  897. if is_pwidechar(def_to) and
  898. is_wide_or_unicode_string(def_from) then
  899. begin
  900. doconv:=tc_ansistring_2_pchar;
  901. eq:=te_convert_l1;
  902. end;
  903. end;
  904. end;
  905. orddef :
  906. begin
  907. { char constant to zero terminated string constant }
  908. if (fromtreetype in [ordconstn,arrayconstructorn]) then
  909. begin
  910. if (is_char(def_from) or is_widechar(def_from)) and
  911. (is_pchar(def_to) or is_pwidechar(def_to)) then
  912. begin
  913. doconv:=tc_cchar_2_pchar;
  914. eq:=te_convert_l1;
  915. end
  916. else
  917. if (m_delphi in current_settings.modeswitches) and is_integer(def_from) then
  918. begin
  919. doconv:=tc_cord_2_pointer;
  920. eq:=te_convert_l5;
  921. end;
  922. end;
  923. { allow explicit typecasts from ordinals to pointer.
  924. Support for delphi compatibility
  925. Support constructs like pointer(cardinal-cardinal) or pointer(longint+cardinal) where
  926. the result of the ordinal operation is int64 also on 32 bit platforms.
  927. It is also used by the compiler internally for inc(pointer,ordinal) }
  928. if (eq=te_incompatible) and
  929. not is_void(def_from) and
  930. (
  931. (
  932. (cdo_explicit in cdoptions) and
  933. (
  934. (m_delphi in current_settings.modeswitches) or
  935. { Don't allow pchar(char) in fpc modes }
  936. is_integer(def_from)
  937. )
  938. ) or
  939. (cdo_internal in cdoptions)
  940. ) then
  941. begin
  942. doconv:=tc_int_2_int;
  943. eq:=te_convert_l1;
  944. end;
  945. end;
  946. enumdef :
  947. begin
  948. { allow explicit typecasts from enums to pointer.
  949. Support for delphi compatibility
  950. }
  951. if (((cdo_explicit in cdoptions) and
  952. (m_delphi in current_settings.modeswitches)
  953. ) or
  954. (cdo_internal in cdoptions)
  955. ) then
  956. begin
  957. doconv:=tc_int_2_int;
  958. eq:=te_convert_l1;
  959. end;
  960. end;
  961. arraydef :
  962. begin
  963. { string constant (which can be part of array constructor)
  964. to zero terminated string constant }
  965. if (((fromtreetype = arrayconstructorn) and
  966. { can't use is_chararray, because returns false for }
  967. { array constructors }
  968. is_char(tarraydef(def_from).elementdef)) or
  969. (fromtreetype = stringconstn)) and
  970. (is_pchar(def_to) or is_pwidechar(def_to)) then
  971. begin
  972. doconv:=tc_cstring_2_pchar;
  973. eq:=te_convert_l2;
  974. end
  975. else
  976. { chararray to pointer }
  977. if (is_zero_based_array(def_from) or
  978. is_open_array(def_from)) and
  979. equal_defs(tarraydef(def_from).elementdef,tpointerdef(def_to).pointeddef) then
  980. begin
  981. doconv:=tc_array_2_pointer;
  982. { don't prefer the pchar overload when a constant
  983. string was passed }
  984. if fromtreetype=stringconstn then
  985. eq:=te_convert_l2
  986. else
  987. eq:=te_convert_l1;
  988. end
  989. else
  990. { dynamic array to pointer, delphi only }
  991. if (m_delphi in current_settings.modeswitches) and
  992. is_dynamic_array(def_from) and
  993. is_voidpointer(def_to) then
  994. begin
  995. eq:=te_equal;
  996. end;
  997. end;
  998. pointerdef :
  999. begin
  1000. { check for far pointers }
  1001. if (tpointerdef(def_from).is_far<>tpointerdef(def_to).is_far) then
  1002. begin
  1003. eq:=te_incompatible;
  1004. end
  1005. else
  1006. { the types can be forward type, handle before normal type check !! }
  1007. if assigned(def_to.typesym) and
  1008. (tpointerdef(def_to).pointeddef.typ=forwarddef) then
  1009. begin
  1010. if (def_from.typesym=def_to.typesym) then
  1011. eq:=te_equal
  1012. end
  1013. else
  1014. { same types }
  1015. if equal_defs(tpointerdef(def_from).pointeddef,tpointerdef(def_to).pointeddef) then
  1016. begin
  1017. eq:=te_equal
  1018. end
  1019. else
  1020. { child class pointer can be assigned to anchestor pointers }
  1021. if (
  1022. (tpointerdef(def_from).pointeddef.typ=objectdef) and
  1023. (tpointerdef(def_to).pointeddef.typ=objectdef) and
  1024. tobjectdef(tpointerdef(def_from).pointeddef).is_related(
  1025. tobjectdef(tpointerdef(def_to).pointeddef))
  1026. ) then
  1027. begin
  1028. doconv:=tc_equal;
  1029. eq:=te_convert_l1;
  1030. end
  1031. else
  1032. { all pointers can be assigned to void-pointer }
  1033. if is_void(tpointerdef(def_to).pointeddef) then
  1034. begin
  1035. doconv:=tc_equal;
  1036. { give pwidechar,pchar a penalty so it prefers
  1037. conversion to ansistring }
  1038. if is_pchar(def_from) or
  1039. is_pwidechar(def_from) then
  1040. eq:=te_convert_l2
  1041. else
  1042. eq:=te_convert_l1;
  1043. end
  1044. else
  1045. { all pointers can be assigned from void-pointer }
  1046. if is_void(tpointerdef(def_from).pointeddef) or
  1047. { all pointers can be assigned from void-pointer or formaldef pointer, check
  1048. tw3777.pp if you change this }
  1049. (tpointerdef(def_from).pointeddef.typ=formaldef) then
  1050. begin
  1051. doconv:=tc_equal;
  1052. { give pwidechar a penalty so it prefers
  1053. conversion to pchar }
  1054. if is_pwidechar(def_to) then
  1055. eq:=te_convert_l2
  1056. else
  1057. eq:=te_convert_l1;
  1058. end
  1059. { id = generic class instance. metaclasses are also
  1060. class instances themselves. }
  1061. else if ((def_from=objc_idtype) and
  1062. (def_to=objc_metaclasstype)) or
  1063. ((def_to=objc_idtype) and
  1064. (def_from=objc_metaclasstype)) then
  1065. begin
  1066. doconv:=tc_equal;
  1067. eq:=te_convert_l2;
  1068. end;
  1069. end;
  1070. procvardef :
  1071. begin
  1072. { procedure variable can be assigned to an void pointer,
  1073. this is not allowed for complex procvars }
  1074. if (is_void(tpointerdef(def_to).pointeddef) or
  1075. (m_mac_procvar in current_settings.modeswitches)) and
  1076. tprocvardef(def_from).is_addressonly then
  1077. begin
  1078. doconv:=tc_equal;
  1079. eq:=te_convert_l1;
  1080. end;
  1081. end;
  1082. procdef :
  1083. begin
  1084. { procedure variable can be assigned to an void pointer,
  1085. this not allowed for methodpointers }
  1086. if (m_mac_procvar in current_settings.modeswitches) and
  1087. tprocdef(def_from).is_addressonly then
  1088. begin
  1089. doconv:=tc_proc_2_procvar;
  1090. eq:=te_convert_l2;
  1091. end;
  1092. end;
  1093. classrefdef,
  1094. objectdef :
  1095. begin
  1096. { implicit pointer object and class reference types
  1097. can be assigned to void pointers, but it is less
  1098. preferred than assigning to a related objectdef }
  1099. if (
  1100. is_implicit_pointer_object_type(def_from) or
  1101. (def_from.typ=classrefdef)
  1102. ) and
  1103. (tpointerdef(def_to).pointeddef.typ=orddef) and
  1104. (torddef(tpointerdef(def_to).pointeddef).ordtype=uvoid) then
  1105. begin
  1106. doconv:=tc_equal;
  1107. eq:=te_convert_l2;
  1108. end
  1109. else if (is_objc_class_or_protocol(def_from) and
  1110. (def_to=objc_idtype)) or
  1111. { classrefs are also instances in Objective-C,
  1112. hence they're also assignment-cpmpatible with
  1113. id }
  1114. (is_objcclassref(def_from) and
  1115. ((def_to=objc_metaclasstype) or
  1116. (def_to=objc_idtype))) then
  1117. begin
  1118. doconv:=tc_equal;
  1119. eq:=te_convert_l2;
  1120. end;
  1121. end;
  1122. end;
  1123. end;
  1124. setdef :
  1125. begin
  1126. case def_from.typ of
  1127. setdef :
  1128. begin
  1129. if assigned(tsetdef(def_from).elementdef) and
  1130. assigned(tsetdef(def_to).elementdef) then
  1131. begin
  1132. { sets with the same element base type and the same range are equal }
  1133. if equal_defs(tsetdef(def_from).elementdef,tsetdef(def_to).elementdef) and
  1134. (tsetdef(def_from).setbase=tsetdef(def_to).setbase) and
  1135. (tsetdef(def_from).setmax=tsetdef(def_to).setmax) then
  1136. eq:=te_equal
  1137. else if is_subequal(tsetdef(def_from).elementdef,tsetdef(def_to).elementdef) then
  1138. begin
  1139. eq:=te_convert_l1;
  1140. doconv:=tc_set_to_set;
  1141. end;
  1142. end
  1143. else
  1144. begin
  1145. { empty set is compatible with everything }
  1146. eq:=te_convert_l1;
  1147. doconv:=tc_set_to_set;
  1148. end;
  1149. end;
  1150. arraydef :
  1151. begin
  1152. { automatic arrayconstructor -> set conversion }
  1153. if is_array_constructor(def_from) then
  1154. begin
  1155. doconv:=tc_arrayconstructor_2_set;
  1156. eq:=te_convert_l1;
  1157. end;
  1158. end;
  1159. end;
  1160. end;
  1161. procvardef :
  1162. begin
  1163. case def_from.typ of
  1164. procdef :
  1165. begin
  1166. { proc -> procvar }
  1167. if (m_tp_procvar in current_settings.modeswitches) or
  1168. (m_mac_procvar in current_settings.modeswitches) then
  1169. begin
  1170. subeq:=proc_to_procvar_equal(tprocdef(def_from),tprocvardef(def_to),cdo_warn_incompatible_univ in cdoptions);
  1171. if subeq>te_incompatible then
  1172. begin
  1173. doconv:=tc_proc_2_procvar;
  1174. if subeq>te_convert_l5 then
  1175. eq:=pred(subeq)
  1176. else
  1177. eq:=subeq;
  1178. end;
  1179. end;
  1180. end;
  1181. procvardef :
  1182. begin
  1183. { procvar -> procvar }
  1184. eq:=proc_to_procvar_equal(tprocvardef(def_from),tprocvardef(def_to),cdo_warn_incompatible_univ in cdoptions);
  1185. end;
  1186. pointerdef :
  1187. begin
  1188. { nil is compatible with procvars }
  1189. if (fromtreetype=niln) then
  1190. begin
  1191. if not Tprocvardef(def_to).is_addressonly then
  1192. {Nil to method pointers requires to convert a single
  1193. pointer nil value to a two pointer procvardef.}
  1194. doconv:=tc_nil_2_methodprocvar
  1195. else
  1196. doconv:=tc_equal;
  1197. eq:=te_convert_l1;
  1198. end
  1199. else
  1200. { for example delphi allows the assignement from pointers }
  1201. { to procedure variables }
  1202. if (m_pointer_2_procedure in current_settings.modeswitches) and
  1203. is_void(tpointerdef(def_from).pointeddef) and
  1204. tprocvardef(def_to).is_addressonly then
  1205. begin
  1206. doconv:=tc_equal;
  1207. eq:=te_convert_l1;
  1208. end;
  1209. end;
  1210. end;
  1211. end;
  1212. objectdef :
  1213. begin
  1214. { Objective-C classes (handle anonymous externals) }
  1215. if (def_from.typ=objectdef) and
  1216. (find_real_objcclass_definition(tobjectdef(def_from),false) =
  1217. find_real_objcclass_definition(tobjectdef(def_to),false)) then
  1218. begin
  1219. doconv:=tc_equal;
  1220. eq:=te_equal;
  1221. end
  1222. { object pascal objects }
  1223. else if (def_from.typ=objectdef) and
  1224. (tobjectdef(def_from).is_related(tobjectdef(def_to))) then
  1225. begin
  1226. doconv:=tc_equal;
  1227. eq:=te_convert_l1;
  1228. end
  1229. else
  1230. { specific to implicit pointer object types }
  1231. if is_implicit_pointer_object_type(def_to) then
  1232. begin
  1233. { void pointer also for delphi mode }
  1234. if (m_delphi in current_settings.modeswitches) and
  1235. is_voidpointer(def_from) then
  1236. begin
  1237. doconv:=tc_equal;
  1238. { prefer pointer-pointer assignments }
  1239. eq:=te_convert_l2;
  1240. end
  1241. else
  1242. { nil is compatible with class instances and interfaces }
  1243. if (fromtreetype=niln) then
  1244. begin
  1245. doconv:=tc_equal;
  1246. eq:=te_convert_l1;
  1247. end
  1248. { All Objective-C classes are compatible with ID }
  1249. else if is_objc_class_or_protocol(def_to) and
  1250. (def_from=objc_idtype) then
  1251. begin
  1252. doconv:=tc_equal;
  1253. eq:=te_convert_l2;
  1254. end
  1255. { classes can be assigned to interfaces
  1256. (same with objcclass and objcprotocol) }
  1257. else if ((is_interface(def_to) and
  1258. is_class(def_from)) or
  1259. (is_objcprotocol(def_to) and
  1260. is_objcclass(def_from))) and
  1261. assigned(tobjectdef(def_from).ImplementedInterfaces) then
  1262. begin
  1263. { we've to search in parent classes as well }
  1264. hobjdef:=tobjectdef(def_from);
  1265. while assigned(hobjdef) do
  1266. begin
  1267. if hobjdef.find_implemented_interface(tobjectdef(def_to))<>nil then
  1268. begin
  1269. if is_interface(def_to) then
  1270. doconv:=tc_class_2_intf
  1271. else
  1272. { for Objective-C, we don't have to do anything special }
  1273. doconv:=tc_equal;
  1274. { don't prefer this over objectdef->objectdef }
  1275. eq:=te_convert_l2;
  1276. break;
  1277. end;
  1278. hobjdef:=hobjdef.childof;
  1279. end;
  1280. end
  1281. { Interface 2 GUID handling }
  1282. else if (def_to=tdef(rec_tguid)) and
  1283. (fromtreetype=typen) and
  1284. is_interface(def_from) and
  1285. assigned(tobjectdef(def_from).iidguid) then
  1286. begin
  1287. eq:=te_convert_l1;
  1288. doconv:=tc_equal;
  1289. end
  1290. else if (def_from.typ=variantdef) and is_interface(def_to) then
  1291. begin
  1292. doconv:=tc_variant_2_interface;
  1293. eq:=te_convert_l2;
  1294. end
  1295. { ugly, but delphi allows it }
  1296. else if (def_from.typ in [orddef,enumdef]) and
  1297. (m_delphi in current_settings.modeswitches) and
  1298. (cdo_explicit in cdoptions) then
  1299. begin
  1300. doconv:=tc_int_2_int;
  1301. eq:=te_convert_l1;
  1302. end;
  1303. end;
  1304. end;
  1305. classrefdef :
  1306. begin
  1307. { similar to pointerdef wrt forwards }
  1308. if assigned(def_to.typesym) and
  1309. (tclassrefdef(def_to).pointeddef.typ=forwarddef) then
  1310. begin
  1311. if (def_from.typesym=def_to.typesym) then
  1312. eq:=te_equal;
  1313. end
  1314. else
  1315. { class reference types }
  1316. if (def_from.typ=classrefdef) then
  1317. begin
  1318. if equal_defs(tclassrefdef(def_from).pointeddef,tclassrefdef(def_to).pointeddef) then
  1319. begin
  1320. eq:=te_equal;
  1321. end
  1322. else
  1323. begin
  1324. doconv:=tc_equal;
  1325. if (cdo_explicit in cdoptions) or
  1326. tobjectdef(tclassrefdef(def_from).pointeddef).is_related(
  1327. tobjectdef(tclassrefdef(def_to).pointeddef)) then
  1328. eq:=te_convert_l1;
  1329. end;
  1330. end
  1331. else
  1332. if (m_delphi in current_settings.modeswitches) and
  1333. is_voidpointer(def_from) then
  1334. begin
  1335. doconv:=tc_equal;
  1336. { prefer pointer-pointer assignments }
  1337. eq:=te_convert_l2;
  1338. end
  1339. else
  1340. { nil is compatible with class references }
  1341. if (fromtreetype=niln) then
  1342. begin
  1343. doconv:=tc_equal;
  1344. eq:=te_convert_l1;
  1345. end
  1346. else
  1347. { id is compatible with all classref types }
  1348. if (def_from=objc_idtype) then
  1349. begin
  1350. doconv:=tc_equal;
  1351. eq:=te_convert_l1;
  1352. end;
  1353. end;
  1354. filedef :
  1355. begin
  1356. { typed files are all equal to the abstract file type
  1357. name TYPEDFILE in system.pp in is_equal in types.pas
  1358. the problem is that it sholud be also compatible to FILE
  1359. but this would leed to a problem for ASSIGN RESET and REWRITE
  1360. when trying to find the good overloaded function !!
  1361. so all file function are doubled in system.pp
  1362. this is not very beautiful !!}
  1363. if (def_from.typ=filedef) then
  1364. begin
  1365. if (tfiledef(def_from).filetyp=tfiledef(def_to).filetyp) then
  1366. begin
  1367. if
  1368. (
  1369. (tfiledef(def_from).typedfiledef=nil) and
  1370. (tfiledef(def_to).typedfiledef=nil)
  1371. ) or
  1372. (
  1373. (tfiledef(def_from).typedfiledef<>nil) and
  1374. (tfiledef(def_to).typedfiledef<>nil) and
  1375. equal_defs(tfiledef(def_from).typedfiledef,tfiledef(def_to).typedfiledef)
  1376. ) or
  1377. (
  1378. (tfiledef(def_from).filetyp = ft_typed) and
  1379. (tfiledef(def_to).filetyp = ft_typed) and
  1380. (
  1381. (tfiledef(def_from).typedfiledef = tdef(voidtype)) or
  1382. (tfiledef(def_to).typedfiledef = tdef(voidtype))
  1383. )
  1384. ) then
  1385. begin
  1386. eq:=te_equal;
  1387. end;
  1388. end
  1389. else
  1390. if ((tfiledef(def_from).filetyp = ft_untyped) and
  1391. (tfiledef(def_to).filetyp = ft_typed)) or
  1392. ((tfiledef(def_from).filetyp = ft_typed) and
  1393. (tfiledef(def_to).filetyp = ft_untyped)) then
  1394. begin
  1395. doconv:=tc_equal;
  1396. eq:=te_convert_l1;
  1397. end;
  1398. end;
  1399. end;
  1400. recorddef :
  1401. begin
  1402. { interface -> guid }
  1403. if (def_to=rec_tguid) and
  1404. (is_interfacecom(def_from) or is_dispinterface(def_from)) then
  1405. begin
  1406. doconv:=tc_intf_2_guid;
  1407. eq:=te_convert_l1;
  1408. end;
  1409. end;
  1410. formaldef :
  1411. begin
  1412. doconv:=tc_equal;
  1413. if (def_from.typ=formaldef) then
  1414. eq:=te_equal
  1415. else
  1416. { Just about everything can be converted to a formaldef...}
  1417. if not (def_from.typ in [abstractdef,errordef]) then
  1418. eq:=te_convert_l2;
  1419. end;
  1420. end;
  1421. { if we didn't find an appropriate type conversion yet
  1422. then we search also the := operator }
  1423. if (eq=te_incompatible) and
  1424. { make sure there is not a single variant if variants }
  1425. { are not allowed (otherwise if only cdo_check_operator }
  1426. { and e.g. fromdef=stringdef and todef=variantdef, then }
  1427. { the test will still succeed }
  1428. ((cdo_allow_variant in cdoptions) or
  1429. ((def_from.typ<>variantdef) and (def_to.typ<>variantdef))
  1430. ) and
  1431. (
  1432. { Check for variants? }
  1433. (
  1434. (cdo_allow_variant in cdoptions) and
  1435. ((def_from.typ=variantdef) or (def_to.typ=variantdef))
  1436. ) or
  1437. { Check for operators? }
  1438. (
  1439. (cdo_check_operator in cdoptions) and
  1440. ((def_from.typ<>variantdef) or (def_to.typ<>variantdef))
  1441. )
  1442. ) then
  1443. begin
  1444. { search record/object symtable first for a sutable operator }
  1445. if def_from.typ in [recorddef,objectdef] then
  1446. symtablestack.push(tabstractrecorddef(def_from).symtable);
  1447. { if type conversion is explicit then search first for explicit
  1448. operator overload and if not found then use implicit operator }
  1449. if cdo_explicit in cdoptions then
  1450. operatorpd:=search_assignment_operator(def_from,def_to,true)
  1451. else
  1452. operatorpd:=nil;
  1453. if operatorpd=nil then
  1454. operatorpd:=search_assignment_operator(def_from,def_to,false);
  1455. if def_from.typ in [recorddef,objectdef] then
  1456. symtablestack.pop(tabstractrecorddef(def_from).symtable);
  1457. if assigned(operatorpd) then
  1458. eq:=te_convert_operator;
  1459. end;
  1460. { update convtype for te_equal when it is not yet set }
  1461. if (eq=te_equal) and
  1462. (doconv=tc_not_possible) then
  1463. doconv:=tc_equal;
  1464. compare_defs_ext:=eq;
  1465. end;
  1466. function equal_defs(def_from,def_to:tdef):boolean;
  1467. var
  1468. convtyp : tconverttype;
  1469. pd : tprocdef;
  1470. begin
  1471. { Compare defs with nothingn and no explicit typecasts and
  1472. searching for overloaded operators is not needed }
  1473. equal_defs:=(compare_defs_ext(def_from,def_to,nothingn,convtyp,pd,[])>=te_equal);
  1474. end;
  1475. function compare_defs(def_from,def_to:tdef;fromtreetype:tnodetype):tequaltype;
  1476. var
  1477. doconv : tconverttype;
  1478. pd : tprocdef;
  1479. begin
  1480. compare_defs:=compare_defs_ext(def_from,def_to,fromtreetype,doconv,pd,[cdo_check_operator,cdo_allow_variant]);
  1481. end;
  1482. function is_subequal(def1, def2: tdef): boolean;
  1483. var
  1484. basedef1,basedef2 : tenumdef;
  1485. Begin
  1486. is_subequal := false;
  1487. if assigned(def1) and assigned(def2) then
  1488. Begin
  1489. if (def1.typ = orddef) and (def2.typ = orddef) then
  1490. Begin
  1491. { see p.47 of Turbo Pascal 7.01 manual for the separation of types }
  1492. { range checking for case statements is done with testrange }
  1493. case torddef(def1).ordtype of
  1494. u8bit,u16bit,u32bit,u64bit,
  1495. s8bit,s16bit,s32bit,s64bit :
  1496. is_subequal:=(torddef(def2).ordtype in [s64bit,u64bit,s32bit,u32bit,u8bit,s8bit,s16bit,u16bit]);
  1497. pasbool,bool8bit,bool16bit,bool32bit,bool64bit :
  1498. is_subequal:=(torddef(def2).ordtype in [pasbool,bool8bit,bool16bit,bool32bit,bool64bit]);
  1499. uchar :
  1500. is_subequal:=(torddef(def2).ordtype=uchar);
  1501. uwidechar :
  1502. is_subequal:=(torddef(def2).ordtype=uwidechar);
  1503. end;
  1504. end
  1505. else
  1506. Begin
  1507. { Check if both basedefs are equal }
  1508. if (def1.typ=enumdef) and (def2.typ=enumdef) then
  1509. Begin
  1510. { get both basedefs }
  1511. basedef1:=tenumdef(def1);
  1512. while assigned(basedef1.basedef) do
  1513. basedef1:=basedef1.basedef;
  1514. basedef2:=tenumdef(def2);
  1515. while assigned(basedef2.basedef) do
  1516. basedef2:=basedef2.basedef;
  1517. is_subequal:=(basedef1=basedef2);
  1518. end;
  1519. end;
  1520. end;
  1521. end;
  1522. function potentially_incompatible_univ_paras(def1, def2: tdef): boolean;
  1523. begin
  1524. result :=
  1525. { not entirely safe: different records can be passed differently
  1526. depending on the types of their fields, but they're hard to compare
  1527. (variant records, bitpacked vs non-bitpacked) }
  1528. ((def1.typ in [floatdef,recorddef,arraydef,filedef,variantdef]) and
  1529. (def1.typ<>def2.typ)) or
  1530. { pointers, ordinals and small sets are all passed the same}
  1531. (((def1.typ in [orddef,enumdef,pointerdef,procvardef,classrefdef]) or
  1532. (is_class_or_interface_or_objc(def1)) or
  1533. is_dynamic_array(def1) or
  1534. is_smallset(def1) or
  1535. is_ansistring(def1) or
  1536. is_unicodestring(def1)) <>
  1537. (def2.typ in [orddef,enumdef,pointerdef,procvardef,classrefdef]) or
  1538. (is_class_or_interface_or_objc(def2)) or
  1539. is_dynamic_array(def2) or
  1540. is_smallset(def2) or
  1541. is_ansistring(def2) or
  1542. is_unicodestring(def2)) or
  1543. { shortstrings }
  1544. (is_shortstring(def1)<>
  1545. is_shortstring(def2)) or
  1546. { winlike widestrings }
  1547. (is_widestring(def1)<>
  1548. is_widestring(def2)) or
  1549. { TP-style objects }
  1550. (is_object(def1) <>
  1551. is_object(def2));
  1552. end;
  1553. function compare_paras(para1,para2 : TFPObjectList; acp : tcompare_paras_type; cpoptions: tcompare_paras_options):tequaltype;
  1554. var
  1555. currpara1,
  1556. currpara2 : tparavarsym;
  1557. eq,lowesteq : tequaltype;
  1558. hpd : tprocdef;
  1559. convtype : tconverttype;
  1560. cdoptions : tcompare_defs_options;
  1561. i1,i2 : byte;
  1562. begin
  1563. compare_paras:=te_incompatible;
  1564. cdoptions:=[cdo_parameter,cdo_check_operator,cdo_allow_variant];
  1565. { we need to parse the list from left-right so the
  1566. not-default parameters are checked first }
  1567. lowesteq:=high(tequaltype);
  1568. i1:=0;
  1569. i2:=0;
  1570. if cpo_ignorehidden in cpoptions then
  1571. begin
  1572. while (i1<para1.count) and
  1573. (vo_is_hidden_para in tparavarsym(para1[i1]).varoptions) do
  1574. inc(i1);
  1575. while (i2<para2.count) and
  1576. (vo_is_hidden_para in tparavarsym(para2[i2]).varoptions) do
  1577. inc(i2);
  1578. end;
  1579. if cpo_ignoreframepointer in cpoptions then
  1580. begin
  1581. if (i1<para1.count) and
  1582. (vo_is_parentfp in tparavarsym(para1[i1]).varoptions) then
  1583. inc(i1);
  1584. if (i2<para2.count) and
  1585. (vo_is_parentfp in tparavarsym(para2[i2]).varoptions) then
  1586. inc(i2);
  1587. end;
  1588. while (i1<para1.count) and (i2<para2.count) do
  1589. begin
  1590. eq:=te_incompatible;
  1591. currpara1:=tparavarsym(para1[i1]);
  1592. currpara2:=tparavarsym(para2[i2]);
  1593. { Unique types must match exact }
  1594. if ((df_unique in currpara1.vardef.defoptions) or (df_unique in currpara2.vardef.defoptions)) and
  1595. (currpara1.vardef<>currpara2.vardef) then
  1596. exit;
  1597. { Handle hidden parameters separately, because self is
  1598. defined as voidpointer for methodpointers }
  1599. if (vo_is_hidden_para in currpara1.varoptions) or
  1600. (vo_is_hidden_para in currpara2.varoptions) then
  1601. begin
  1602. { both must be hidden }
  1603. if (vo_is_hidden_para in currpara1.varoptions)<>(vo_is_hidden_para in currpara2.varoptions) then
  1604. exit;
  1605. eq:=te_exact;
  1606. if not(vo_is_self in currpara1.varoptions) and
  1607. not(vo_is_self in currpara2.varoptions) then
  1608. begin
  1609. if not(cpo_ignorevarspez in cpoptions) and
  1610. (currpara1.varspez<>currpara2.varspez) then
  1611. exit;
  1612. eq:=compare_defs_ext(currpara1.vardef,currpara2.vardef,nothingn,
  1613. convtype,hpd,cdoptions);
  1614. end;
  1615. end
  1616. else
  1617. begin
  1618. case acp of
  1619. cp_value_equal_const :
  1620. begin
  1621. { this one is used for matching parameters from a call
  1622. statement to a procdef -> univ state can't be equal
  1623. in any case since the call statement does not contain
  1624. any information about that }
  1625. if (
  1626. not(cpo_ignorevarspez in cpoptions) and
  1627. (currpara1.varspez<>currpara2.varspez) and
  1628. ((currpara1.varspez in [vs_var,vs_out,vs_constref]) or
  1629. (currpara2.varspez in [vs_var,vs_out,vs_constref]))
  1630. ) then
  1631. exit;
  1632. eq:=compare_defs_ext(currpara1.vardef,currpara2.vardef,nothingn,
  1633. convtype,hpd,cdoptions);
  1634. end;
  1635. cp_all :
  1636. begin
  1637. { used to resolve forward definitions -> headers must
  1638. match exactly, including the "univ" specifier }
  1639. if (not(cpo_ignorevarspez in cpoptions) and
  1640. (currpara1.varspez<>currpara2.varspez)) or
  1641. (currpara1.univpara<>currpara2.univpara) then
  1642. exit;
  1643. eq:=compare_defs_ext(currpara1.vardef,currpara2.vardef,nothingn,
  1644. convtype,hpd,cdoptions);
  1645. end;
  1646. cp_procvar :
  1647. begin
  1648. if not(cpo_ignorevarspez in cpoptions) and
  1649. (currpara1.varspez<>currpara2.varspez) then
  1650. exit;
  1651. { "univ" state doesn't matter here: from univ to non-univ
  1652. matches if the types are compatible (i.e., as usual),
  1653. from from non-univ to univ also matches if the types
  1654. have the same size (checked below) }
  1655. eq:=compare_defs_ext(currpara1.vardef,currpara2.vardef,nothingn,
  1656. convtype,hpd,cdoptions);
  1657. { Parameters must be at least equal otherwise the are incompatible }
  1658. if (eq<te_equal) then
  1659. eq:=te_incompatible;
  1660. end;
  1661. else
  1662. eq:=compare_defs_ext(currpara1.vardef,currpara2.vardef,nothingn,
  1663. convtype,hpd,cdoptions);
  1664. end;
  1665. end;
  1666. { check type }
  1667. if eq=te_incompatible then
  1668. begin
  1669. { special case: "univ" parameters match if their size is equal }
  1670. if not(cpo_ignoreuniv in cpoptions) and
  1671. currpara2.univpara and
  1672. is_valid_univ_para_type(currpara1.vardef) and
  1673. (currpara1.vardef.size=currpara2.vardef.size) then
  1674. begin
  1675. { only pick as last choice }
  1676. eq:=te_convert_l5;
  1677. if (acp=cp_procvar) and
  1678. (cpo_warn_incompatible_univ in cpoptions) then
  1679. begin
  1680. { if the types may be passed in different ways by the
  1681. calling convention then this can lead to crashes
  1682. (note: not an exhaustive check, and failing this
  1683. this check does not mean things will crash on all
  1684. platforms) }
  1685. if potentially_incompatible_univ_paras(currpara1.vardef,currpara2.vardef) then
  1686. Message2(type_w_procvar_univ_conflicting_para,currpara1.vardef.typename,currpara2.vardef.typename)
  1687. end;
  1688. end
  1689. else
  1690. exit;
  1691. end;
  1692. { open strings can never match exactly, since you cannot define }
  1693. { a separate "open string" type -> we have to be able to }
  1694. { consider those as exact when resolving forward definitions. }
  1695. { The same goes for array of const. Open arrays are handled }
  1696. { already (if their element types match exactly, they are }
  1697. { considered to be an exact match) }
  1698. { And also for "inline defined" function parameter definitions }
  1699. { (i.e., function types directly declared in a parameter list) }
  1700. if (is_array_of_const(currpara1.vardef) or
  1701. is_open_string(currpara1.vardef) or
  1702. ((currpara1.vardef.typ = procvardef) and
  1703. not(assigned(currpara1.vardef.typesym)))) and
  1704. (eq=te_equal) and
  1705. (cpo_openequalisexact in cpoptions) then
  1706. eq:=te_exact;
  1707. if eq<lowesteq then
  1708. lowesteq:=eq;
  1709. { also check default value if both have it declared }
  1710. if (cpo_comparedefaultvalue in cpoptions) and
  1711. assigned(currpara1.defaultconstsym) and
  1712. assigned(currpara2.defaultconstsym) then
  1713. begin
  1714. if not equal_constsym(tconstsym(currpara1.defaultconstsym),tconstsym(currpara2.defaultconstsym)) then
  1715. exit;
  1716. end;
  1717. inc(i1);
  1718. inc(i2);
  1719. if cpo_ignorehidden in cpoptions then
  1720. begin
  1721. while (i1<para1.count) and
  1722. (vo_is_hidden_para in tparavarsym(para1[i1]).varoptions) do
  1723. inc(i1);
  1724. while (i2<para2.count) and
  1725. (vo_is_hidden_para in tparavarsym(para2[i2]).varoptions) do
  1726. inc(i2);
  1727. end;
  1728. if cpo_ignoreframepointer in cpoptions then
  1729. begin
  1730. if (i1<para1.count) and
  1731. (vo_is_parentfp in tparavarsym(para1[i1]).varoptions) then
  1732. inc(i1);
  1733. if (i2<para2.count) and
  1734. (vo_is_parentfp in tparavarsym(para2[i2]).varoptions) then
  1735. inc(i2);
  1736. end;
  1737. end;
  1738. { when both lists are empty then the parameters are equal. Also
  1739. when one list is empty and the other has a parameter with default
  1740. value assigned then the parameters are also equal }
  1741. if ((i1>=para1.count) and (i2>=para2.count)) or
  1742. ((cpo_allowdefaults in cpoptions) and
  1743. (((i1<para1.count) and assigned(tparavarsym(para1[i1]).defaultconstsym)) or
  1744. ((i2<para2.count) and assigned(tparavarsym(para2[i2]).defaultconstsym)))) then
  1745. compare_paras:=lowesteq;
  1746. end;
  1747. function proc_to_procvar_equal(def1:tabstractprocdef;def2:tprocvardef;checkincompatibleuniv: boolean):tequaltype;
  1748. var
  1749. eq : tequaltype;
  1750. po_comp : tprocoptions;
  1751. pa_comp: tcompare_paras_options;
  1752. begin
  1753. proc_to_procvar_equal:=te_incompatible;
  1754. if not(assigned(def1)) or not(assigned(def2)) then
  1755. exit;
  1756. { check for method pointer and local procedure pointer:
  1757. a) if one is a procedure of object, the other also has to be one
  1758. b) if one is a pure address, the other also has to be one
  1759. except if def1 is a global proc and def2 is a nested procdef
  1760. (global procedures can be converted into nested procvars)
  1761. c) if def1 is a nested procedure, then def2 has to be a nested
  1762. procvar and def1 has to have the po_delphi_nested_cc option
  1763. d) if def1 is a procvar, def1 and def2 both have to be nested or
  1764. non-nested (we don't allow assignments from non-nested to
  1765. nested procvars to make sure that we can still implement
  1766. nested procvars using trampolines -- e.g., this would be
  1767. necessary for LLVM or CIL as long as they do not have support
  1768. for Delphi-style frame pointer parameter passing) }
  1769. if (def1.is_methodpointer<>def2.is_methodpointer) or { a) }
  1770. ((def1.is_addressonly<>def2.is_addressonly) and { b) }
  1771. (is_nested_pd(def1) or
  1772. not is_nested_pd(def2))) or
  1773. ((def1.typ=procdef) and { c) }
  1774. is_nested_pd(def1) and
  1775. (not(po_delphi_nested_cc in def1.procoptions) or
  1776. not is_nested_pd(def2))) or
  1777. ((def1.typ=procvardef) and { d) }
  1778. (is_nested_pd(def1)<>is_nested_pd(def2))) then
  1779. exit;
  1780. pa_comp:=[cpo_ignoreframepointer];
  1781. if checkincompatibleuniv then
  1782. include(pa_comp,cpo_warn_incompatible_univ);
  1783. { check return value and options, methodpointer is already checked }
  1784. po_comp:=[po_staticmethod,po_interrupt,
  1785. po_iocheck,po_varargs];
  1786. if (m_delphi in current_settings.modeswitches) then
  1787. exclude(po_comp,po_varargs);
  1788. if (def1.proccalloption=def2.proccalloption) and
  1789. ((po_comp * def1.procoptions)= (po_comp * def2.procoptions)) and
  1790. equal_defs(def1.returndef,def2.returndef) then
  1791. begin
  1792. { return equal type based on the parameters, but a proc->procvar
  1793. is never exact, so map an exact match of the parameters to
  1794. te_equal }
  1795. eq:=compare_paras(def1.paras,def2.paras,cp_procvar,pa_comp);
  1796. if eq=te_exact then
  1797. eq:=te_equal;
  1798. if (eq=te_equal) then
  1799. begin
  1800. { prefer non-nested to non-nested over non-nested to nested }
  1801. if (is_nested_pd(def1)<>is_nested_pd(def2)) then
  1802. eq:=te_convert_l1;
  1803. end;
  1804. proc_to_procvar_equal:=eq;
  1805. end;
  1806. end;
  1807. function compatible_childmethod_resultdef(parentretdef, childretdef: tdef): boolean;
  1808. begin
  1809. compatible_childmethod_resultdef :=
  1810. (equal_defs(parentretdef,childretdef)) or
  1811. ((parentretdef.typ=objectdef) and
  1812. (childretdef.typ=objectdef) and
  1813. is_class_or_interface_or_objc(parentretdef) and
  1814. is_class_or_interface_or_objc(childretdef) and
  1815. (tobjectdef(childretdef).is_related(tobjectdef(parentretdef))))
  1816. end;
  1817. end.