defcmp.pas 85 KB

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