defcmp.pas 73 KB

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