defcmp.pas 68 KB

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