defcmp.pas 63 KB

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