defcmp.pas 75 KB

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