defcmp.pas 62 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;methoderr:boolean):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. if is_integer(def_from) or
  433. (is_currency(def_from) and
  434. (s64currencytype.def.deftype = floatdef)) then
  435. begin
  436. doconv:=tc_int_2_real;
  437. eq:=te_convert_l1;
  438. end
  439. else if is_currency(def_from)
  440. { and (s64currencytype.def.deftype = orddef)) } then
  441. begin
  442. { prefer conversion to orddef in this case, unless }
  443. { the orddef < currency (then it will get convert l3, }
  444. { and conversion to float is favoured) }
  445. doconv:=tc_int_2_real;
  446. eq:=te_convert_l2;
  447. end;
  448. end;
  449. floatdef :
  450. begin
  451. if tfloatdef(def_from).typ=tfloatdef(def_to).typ then
  452. eq:=te_equal
  453. else
  454. begin
  455. if (fromtreetype=realconstn) or
  456. not((cdo_explicit in cdoptions) and
  457. (m_delphi in aktmodeswitches)) then
  458. begin
  459. doconv:=tc_real_2_real;
  460. { do we loose precision? }
  461. if def_to.size<def_from.size then
  462. eq:=te_convert_l2
  463. else
  464. eq:=te_convert_l1;
  465. end;
  466. end;
  467. end;
  468. end;
  469. end;
  470. enumdef :
  471. begin
  472. case def_from.deftype of
  473. enumdef :
  474. begin
  475. if cdo_explicit in cdoptions then
  476. begin
  477. eq:=te_convert_l1;
  478. doconv:=tc_int_2_int;
  479. end
  480. else
  481. begin
  482. hd1:=def_from;
  483. while assigned(tenumdef(hd1).basedef) do
  484. hd1:=tenumdef(hd1).basedef;
  485. hd2:=def_to;
  486. while assigned(tenumdef(hd2).basedef) do
  487. hd2:=tenumdef(hd2).basedef;
  488. if (hd1=hd2) then
  489. begin
  490. eq:=te_convert_l1;
  491. { because of packenum they can have different sizes! (JM) }
  492. doconv:=tc_int_2_int;
  493. end
  494. else
  495. begin
  496. { assignment of an enum symbol to an unique type? }
  497. if (fromtreetype=ordconstn) and
  498. (tenumsym(tenumdef(hd1).firstenum)=tenumsym(tenumdef(hd2).firstenum)) then
  499. begin
  500. { because of packenum they can have different sizes! (JM) }
  501. eq:=te_convert_l1;
  502. doconv:=tc_int_2_int;
  503. end;
  504. end;
  505. end;
  506. end;
  507. orddef :
  508. begin
  509. if cdo_explicit in cdoptions then
  510. begin
  511. eq:=te_convert_l1;
  512. doconv:=tc_int_2_int;
  513. end;
  514. end;
  515. variantdef :
  516. begin
  517. eq:=te_convert_l1;
  518. doconv:=tc_variant_2_enum;
  519. end;
  520. pointerdef :
  521. begin
  522. { ugly, but delphi allows it }
  523. if (cdo_explicit in cdoptions) and
  524. (m_delphi in aktmodeswitches) and
  525. (eq=te_incompatible) then
  526. begin
  527. doconv:=tc_int_2_int;
  528. eq:=te_convert_l1;
  529. end;
  530. end;
  531. end;
  532. end;
  533. arraydef :
  534. begin
  535. { open array is also compatible with a single element of its base type }
  536. if is_open_array(def_to) and
  537. equal_defs(def_from,tarraydef(def_to).elementtype.def) then
  538. begin
  539. doconv:=tc_equal;
  540. eq:=te_convert_l1;
  541. end
  542. else
  543. begin
  544. case def_from.deftype of
  545. arraydef :
  546. begin
  547. { to dynamic array }
  548. if is_dynamic_array(def_to) then
  549. begin
  550. if equal_defs(tarraydef(def_from).elementtype.def,tarraydef(def_to).elementtype.def) then
  551. begin
  552. { dynamic array -> dynamic array }
  553. if is_dynamic_array(def_from) then
  554. eq:=te_equal
  555. { fpc modes only: array -> dyn. array }
  556. else if (aktmodeswitches*[m_objfpc,m_fpc]<>[]) and
  557. not(is_special_array(def_from)) and
  558. is_zero_based_array(def_from) then
  559. begin
  560. eq:=te_convert_l2;
  561. doconv:=tc_array_2_dynarray;
  562. end;
  563. end
  564. end
  565. else
  566. { to open array }
  567. if is_open_array(def_to) then
  568. begin
  569. { array constructor -> open array }
  570. if is_array_constructor(def_from) then
  571. begin
  572. if is_void(tarraydef(def_from).elementtype.def) then
  573. begin
  574. doconv:=tc_equal;
  575. eq:=te_convert_l1;
  576. end
  577. else
  578. begin
  579. subeq:=compare_defs_ext(tarraydef(def_from).elementtype.def,
  580. tarraydef(def_to).elementtype.def,
  581. arrayconstructorn,hct,hpd,[cdo_check_operator]);
  582. if (subeq>=te_equal) then
  583. begin
  584. doconv:=tc_equal;
  585. eq:=te_convert_l1;
  586. end
  587. else
  588. if (subeq>te_incompatible) then
  589. begin
  590. doconv:=hct;
  591. eq:=te_convert_l2;
  592. end;
  593. end;
  594. end
  595. else
  596. { dynamic array -> open array }
  597. if is_dynamic_array(def_from) and
  598. equal_defs(tarraydef(def_from).elementtype.def,tarraydef(def_to).elementtype.def) then
  599. begin
  600. doconv:=tc_dynarray_2_openarray;
  601. eq:=te_convert_l2;
  602. end
  603. else
  604. { array -> open array }
  605. if equal_defs(tarraydef(def_from).elementtype.def,tarraydef(def_to).elementtype.def) then
  606. eq:=te_equal;
  607. end
  608. else
  609. { to array of const }
  610. if is_array_of_const(def_to) then
  611. begin
  612. if is_array_of_const(def_from) or
  613. is_array_constructor(def_from) then
  614. begin
  615. eq:=te_equal;
  616. end
  617. else
  618. { array of tvarrec -> array of const }
  619. if equal_defs(tarraydef(def_to).elementtype.def,tarraydef(def_from).elementtype.def) then
  620. begin
  621. doconv:=tc_equal;
  622. eq:=te_convert_l1;
  623. end;
  624. end
  625. else
  626. { to array of char, from "Untyped" stringconstn (array of char) }
  627. if (fromtreetype=stringconstn) and
  628. is_chararray(def_to) then
  629. begin
  630. eq:=te_convert_l1;
  631. doconv:=tc_string_2_chararray;
  632. end
  633. else
  634. { other arrays }
  635. begin
  636. { open array -> array }
  637. if is_open_array(def_from) and
  638. equal_defs(tarraydef(def_from).elementtype.def,tarraydef(def_to).elementtype.def) then
  639. begin
  640. eq:=te_equal
  641. end
  642. else
  643. { array -> array }
  644. if not(m_tp7 in aktmodeswitches) and
  645. not(m_delphi in aktmodeswitches) and
  646. (tarraydef(def_from).lowrange=tarraydef(def_to).lowrange) and
  647. (tarraydef(def_from).highrange=tarraydef(def_to).highrange) and
  648. equal_defs(tarraydef(def_from).elementtype.def,tarraydef(def_to).elementtype.def) and
  649. equal_defs(tarraydef(def_from).rangetype.def,tarraydef(def_to).rangetype.def) then
  650. begin
  651. eq:=te_equal
  652. end;
  653. end;
  654. end;
  655. pointerdef :
  656. begin
  657. { nil and voidpointers are compatible with dyn. arrays }
  658. if is_dynamic_array(def_to) and
  659. ((fromtreetype=niln) or
  660. is_voidpointer(def_from)) then
  661. begin
  662. doconv:=tc_equal;
  663. eq:=te_convert_l1;
  664. end
  665. else
  666. if is_zero_based_array(def_to) and
  667. equal_defs(tpointerdef(def_from).pointertype.def,tarraydef(def_to).elementtype.def) then
  668. begin
  669. doconv:=tc_pointer_2_array;
  670. eq:=te_convert_l1;
  671. end;
  672. end;
  673. stringdef :
  674. begin
  675. { string to char array }
  676. if (not is_special_array(def_to)) and
  677. (is_char(tarraydef(def_to).elementtype.def)or
  678. is_widechar(tarraydef(def_to).elementtype.def)) then
  679. begin
  680. doconv:=tc_string_2_chararray;
  681. eq:=te_convert_l1;
  682. end;
  683. end;
  684. orddef:
  685. begin
  686. if is_chararray(def_to) and
  687. is_char(def_from) then
  688. begin
  689. doconv:=tc_char_2_chararray;
  690. eq:=te_convert_l2;
  691. end;
  692. end;
  693. recorddef :
  694. begin
  695. { tvarrec -> array of const }
  696. if is_array_of_const(def_to) and
  697. equal_defs(def_from,tarraydef(def_to).elementtype.def) then
  698. begin
  699. doconv:=tc_equal;
  700. eq:=te_convert_l1;
  701. end;
  702. end;
  703. variantdef :
  704. begin
  705. if is_dynamic_array(def_to) then
  706. begin
  707. doconv:=tc_variant_2_dynarray;
  708. eq:=te_convert_l1;
  709. end;
  710. end;
  711. end;
  712. end;
  713. end;
  714. variantdef :
  715. begin
  716. if (cdo_allow_variant in cdoptions) then
  717. begin
  718. case def_from.deftype of
  719. enumdef :
  720. begin
  721. doconv:=tc_enum_2_variant;
  722. eq:=te_convert_l1;
  723. end;
  724. arraydef :
  725. begin
  726. if is_dynamic_array(def_from) then
  727. begin
  728. doconv:=tc_dynarray_2_variant;
  729. eq:=te_convert_l1;
  730. end;
  731. end;
  732. objectdef :
  733. begin
  734. if is_interface(def_from) then
  735. begin
  736. doconv:=tc_interface_2_variant;
  737. eq:=te_convert_l1;
  738. end;
  739. end;
  740. end;
  741. end;
  742. end;
  743. pointerdef :
  744. begin
  745. case def_from.deftype of
  746. stringdef :
  747. begin
  748. { string constant (which can be part of array constructor)
  749. to zero terminated string constant }
  750. if (fromtreetype in [arrayconstructorn,stringconstn]) and
  751. (is_pchar(def_to) or is_pwidechar(def_to)) then
  752. begin
  753. doconv:=tc_cstring_2_pchar;
  754. eq:=te_convert_l2;
  755. end
  756. else
  757. if cdo_explicit in cdoptions then
  758. begin
  759. { pchar(ansistring) }
  760. if is_pchar(def_to) and
  761. is_ansistring(def_from) then
  762. begin
  763. doconv:=tc_ansistring_2_pchar;
  764. eq:=te_convert_l1;
  765. end
  766. else
  767. { pwidechar(widestring) }
  768. if is_pwidechar(def_to) and
  769. is_widestring(def_from) then
  770. begin
  771. doconv:=tc_ansistring_2_pchar;
  772. eq:=te_convert_l1;
  773. end;
  774. end;
  775. end;
  776. orddef :
  777. begin
  778. { char constant to zero terminated string constant }
  779. if (fromtreetype=ordconstn) then
  780. begin
  781. if (is_char(def_from) or is_widechar(def_from)) and
  782. (is_pchar(def_to) or is_pwidechar(def_to)) then
  783. begin
  784. doconv:=tc_cchar_2_pchar;
  785. eq:=te_convert_l1;
  786. end
  787. else
  788. if (m_delphi in aktmodeswitches) and is_integer(def_from) then
  789. begin
  790. doconv:=tc_cord_2_pointer;
  791. eq:=te_convert_l2;
  792. end;
  793. end;
  794. { delphi compatible, allow explicit typecasts from
  795. ordinals to pointer.
  796. It is also used by the compiler internally for inc(pointer,ordinal) }
  797. if (eq=te_incompatible) and
  798. not is_void(def_from) and
  799. (
  800. (
  801. (m_delphi in aktmodeswitches) and
  802. (cdo_explicit in cdoptions)
  803. ) or
  804. (cdo_internal in cdoptions)
  805. ) then
  806. begin
  807. doconv:=tc_int_2_int;
  808. eq:=te_convert_l1;
  809. end;
  810. end;
  811. arraydef :
  812. begin
  813. { string constant (which can be part of array constructor)
  814. to zero terminated string constant }
  815. if (fromtreetype in [arrayconstructorn,stringconstn]) and
  816. (is_pchar(def_to) or is_pwidechar(def_to)) then
  817. begin
  818. doconv:=tc_cstring_2_pchar;
  819. eq:=te_convert_l2;
  820. end
  821. else
  822. { chararray to pointer }
  823. if (is_zero_based_array(def_from) or
  824. is_open_array(def_from)) and
  825. equal_defs(tarraydef(def_from).elementtype.def,tpointerdef(def_to).pointertype.def) then
  826. begin
  827. doconv:=tc_array_2_pointer;
  828. { don't prefer the pchar overload when a constant
  829. string was passed }
  830. if fromtreetype=stringconstn then
  831. eq:=te_convert_l2
  832. else
  833. eq:=te_convert_l1;
  834. end
  835. else
  836. { dynamic array to pointer, delphi only }
  837. if (m_delphi in aktmodeswitches) and
  838. is_dynamic_array(def_from) then
  839. begin
  840. eq:=te_equal;
  841. end;
  842. end;
  843. pointerdef :
  844. begin
  845. { check for far pointers }
  846. if (tpointerdef(def_from).is_far<>tpointerdef(def_to).is_far) then
  847. begin
  848. eq:=te_incompatible;
  849. end
  850. else
  851. { the types can be forward type, handle before normal type check !! }
  852. if assigned(def_to.typesym) and
  853. (tpointerdef(def_to).pointertype.def.deftype=forwarddef) then
  854. begin
  855. if (def_from.typesym=def_to.typesym) then
  856. eq:=te_equal
  857. end
  858. else
  859. { same types }
  860. if equal_defs(tpointerdef(def_from).pointertype.def,tpointerdef(def_to).pointertype.def) then
  861. begin
  862. eq:=te_equal
  863. end
  864. else
  865. { child class pointer can be assigned to anchestor pointers }
  866. if (
  867. (tpointerdef(def_from).pointertype.def.deftype=objectdef) and
  868. (tpointerdef(def_to).pointertype.def.deftype=objectdef) and
  869. tobjectdef(tpointerdef(def_from).pointertype.def).is_related(
  870. tobjectdef(tpointerdef(def_to).pointertype.def))
  871. ) then
  872. begin
  873. doconv:=tc_equal;
  874. eq:=te_convert_l1;
  875. end
  876. else
  877. { all pointers can be assigned to void-pointer }
  878. if is_void(tpointerdef(def_to).pointertype.def) then
  879. begin
  880. doconv:=tc_equal;
  881. { give pwidechar,pchar a penalty so it prefers
  882. conversion to ansistring }
  883. if is_pchar(def_from) or
  884. is_pwidechar(def_from) then
  885. eq:=te_convert_l2
  886. else
  887. eq:=te_convert_l1;
  888. end
  889. else
  890. { all pointers can be assigned from void-pointer }
  891. if is_void(tpointerdef(def_from).pointertype.def) or
  892. { all pointers can be assigned from void-pointer or formaldef pointer, check
  893. tw3777.pp if you change this }
  894. (tpointerdef(def_from).pointertype.def.deftype=formaldef) then
  895. begin
  896. doconv:=tc_equal;
  897. { give pwidechar a penalty so it prefers
  898. conversion to pchar }
  899. if is_pwidechar(def_to) then
  900. eq:=te_convert_l2
  901. else
  902. eq:=te_convert_l1;
  903. end;
  904. end;
  905. procvardef :
  906. begin
  907. { procedure variable can be assigned to an void pointer,
  908. this not allowed for methodpointers }
  909. if (is_void(tpointerdef(def_to).pointertype.def) or
  910. (m_mac_procvar in aktmodeswitches)) and
  911. tprocvardef(def_from).is_addressonly then
  912. begin
  913. doconv:=tc_equal;
  914. eq:=te_convert_l1;
  915. end;
  916. end;
  917. procdef :
  918. begin
  919. { procedure variable can be assigned to an void pointer,
  920. this not allowed for methodpointers }
  921. if (m_mac_procvar in aktmodeswitches) and
  922. tprocdef(def_from).is_addressonly then
  923. begin
  924. doconv:=tc_proc_2_procvar;
  925. eq:=te_convert_l2;
  926. end;
  927. end;
  928. classrefdef,
  929. objectdef :
  930. begin
  931. { class types and class reference type
  932. can be assigned to void pointers, but it is less
  933. preferred than assigning to a related objectdef }
  934. if (
  935. is_class_or_interface(def_from) or
  936. (def_from.deftype=classrefdef)
  937. ) and
  938. (tpointerdef(def_to).pointertype.def.deftype=orddef) and
  939. (torddef(tpointerdef(def_to).pointertype.def).typ=uvoid) then
  940. begin
  941. doconv:=tc_equal;
  942. eq:=te_convert_l2;
  943. end;
  944. end;
  945. end;
  946. end;
  947. setdef :
  948. begin
  949. case def_from.deftype of
  950. setdef :
  951. begin
  952. if assigned(tsetdef(def_from).elementtype.def) and
  953. assigned(tsetdef(def_to).elementtype.def) then
  954. begin
  955. { sets with the same element base type are equal }
  956. if is_subequal(tsetdef(def_from).elementtype.def,tsetdef(def_to).elementtype.def) then
  957. eq:=te_equal;
  958. end
  959. else
  960. { empty set is compatible with everything }
  961. eq:=te_equal;
  962. end;
  963. arraydef :
  964. begin
  965. { automatic arrayconstructor -> set conversion }
  966. if is_array_constructor(def_from) then
  967. begin
  968. doconv:=tc_arrayconstructor_2_set;
  969. eq:=te_convert_l1;
  970. end;
  971. end;
  972. end;
  973. end;
  974. procvardef :
  975. begin
  976. case def_from.deftype of
  977. procdef :
  978. begin
  979. { proc -> procvar }
  980. if (m_tp_procvar in aktmodeswitches) or
  981. (m_mac_procvar in aktmodeswitches) then
  982. begin
  983. subeq:=proc_to_procvar_equal(tprocdef(def_from),tprocvardef(def_to),true);
  984. if subeq>te_incompatible then
  985. begin
  986. doconv:=tc_proc_2_procvar;
  987. eq:=te_convert_l1;
  988. end;
  989. end;
  990. end;
  991. procvardef :
  992. begin
  993. { procvar -> procvar }
  994. eq:=proc_to_procvar_equal(tprocvardef(def_from),tprocvardef(def_to),false);
  995. end;
  996. pointerdef :
  997. begin
  998. { nil is compatible with procvars }
  999. if (fromtreetype=niln) then
  1000. begin
  1001. doconv:=tc_equal;
  1002. eq:=te_convert_l1;
  1003. end
  1004. else
  1005. { for example delphi allows the assignement from pointers }
  1006. { to procedure variables }
  1007. if (m_pointer_2_procedure in aktmodeswitches) and
  1008. is_void(tpointerdef(def_from).pointertype.def) and
  1009. tprocvardef(def_to).is_addressonly then
  1010. begin
  1011. doconv:=tc_equal;
  1012. eq:=te_convert_l1;
  1013. end;
  1014. end;
  1015. end;
  1016. end;
  1017. objectdef :
  1018. begin
  1019. { object pascal objects }
  1020. if (def_from.deftype=objectdef) and
  1021. (tobjectdef(def_from).is_related(tobjectdef(def_to))) then
  1022. begin
  1023. doconv:=tc_equal;
  1024. eq:=te_convert_l1;
  1025. end
  1026. else
  1027. { Class/interface specific }
  1028. if is_class_or_interface(def_to) then
  1029. begin
  1030. { void pointer also for delphi mode }
  1031. if (m_delphi in aktmodeswitches) and
  1032. is_voidpointer(def_from) then
  1033. begin
  1034. doconv:=tc_equal;
  1035. { prefer pointer-pointer assignments }
  1036. eq:=te_convert_l2;
  1037. end
  1038. else
  1039. { nil is compatible with class instances and interfaces }
  1040. if (fromtreetype=niln) then
  1041. begin
  1042. doconv:=tc_equal;
  1043. eq:=te_convert_l1;
  1044. end
  1045. { classes can be assigned to interfaces }
  1046. else if is_interface(def_to) and
  1047. is_class(def_from) and
  1048. assigned(tobjectdef(def_from).implementedinterfaces) then
  1049. begin
  1050. { we've to search in parent classes as well }
  1051. hd3:=tobjectdef(def_from);
  1052. while assigned(hd3) do
  1053. begin
  1054. if hd3.implementedinterfaces.searchintf(def_to)<>-1 then
  1055. begin
  1056. doconv:=tc_class_2_intf;
  1057. eq:=te_convert_l1;
  1058. break;
  1059. end;
  1060. hd3:=hd3.childof;
  1061. end;
  1062. end
  1063. { Interface 2 GUID handling }
  1064. else if (def_to=tdef(rec_tguid)) and
  1065. (fromtreetype=typen) and
  1066. is_interface(def_from) and
  1067. assigned(tobjectdef(def_from).iidguid) then
  1068. begin
  1069. eq:=te_convert_l1;
  1070. doconv:=tc_equal;
  1071. end
  1072. else if (def_from.deftype=variantdef) and is_interface(def_to) then
  1073. begin
  1074. doconv:=tc_variant_2_interface;
  1075. eq:=te_convert_l1;
  1076. end
  1077. { ugly, but delphi allows it }
  1078. else if (eq=te_incompatible) and
  1079. (def_from.deftype=orddef) and
  1080. (m_delphi in aktmodeswitches) and
  1081. (cdo_explicit in cdoptions) then
  1082. begin
  1083. doconv:=tc_int_2_int;
  1084. eq:=te_convert_l1;
  1085. end;
  1086. end;
  1087. end;
  1088. classrefdef :
  1089. begin
  1090. { similar to pointerdef wrt forwards }
  1091. if assigned(def_to.typesym) and
  1092. (tclassrefdef(def_to).pointertype.def.deftype=forwarddef) then
  1093. begin
  1094. if (def_from.typesym=def_to.typesym) then
  1095. eq:=te_equal;
  1096. end
  1097. else
  1098. { class reference types }
  1099. if (def_from.deftype=classrefdef) then
  1100. begin
  1101. if equal_defs(tclassrefdef(def_from).pointertype.def,tclassrefdef(def_to).pointertype.def) then
  1102. begin
  1103. eq:=te_equal;
  1104. end
  1105. else
  1106. begin
  1107. doconv:=tc_equal;
  1108. if (cdo_explicit in cdoptions) or
  1109. tobjectdef(tclassrefdef(def_from).pointertype.def).is_related(
  1110. tobjectdef(tclassrefdef(def_to).pointertype.def)) then
  1111. eq:=te_convert_l1;
  1112. end;
  1113. end
  1114. else
  1115. { nil is compatible with class references }
  1116. if (fromtreetype=niln) then
  1117. begin
  1118. doconv:=tc_equal;
  1119. eq:=te_convert_l1;
  1120. end;
  1121. end;
  1122. filedef :
  1123. begin
  1124. { typed files are all equal to the abstract file type
  1125. name TYPEDFILE in system.pp in is_equal in types.pas
  1126. the problem is that it sholud be also compatible to FILE
  1127. but this would leed to a problem for ASSIGN RESET and REWRITE
  1128. when trying to find the good overloaded function !!
  1129. so all file function are doubled in system.pp
  1130. this is not very beautiful !!}
  1131. if (def_from.deftype=filedef) then
  1132. begin
  1133. if (tfiledef(def_from).filetyp=tfiledef(def_to).filetyp) then
  1134. begin
  1135. if
  1136. (
  1137. (tfiledef(def_from).typedfiletype.def=nil) and
  1138. (tfiledef(def_to).typedfiletype.def=nil)
  1139. ) or
  1140. (
  1141. (tfiledef(def_from).typedfiletype.def<>nil) and
  1142. (tfiledef(def_to).typedfiletype.def<>nil) and
  1143. equal_defs(tfiledef(def_from).typedfiletype.def,tfiledef(def_to).typedfiletype.def)
  1144. ) or
  1145. (
  1146. (tfiledef(def_from).filetyp = ft_typed) and
  1147. (tfiledef(def_to).filetyp = ft_typed) and
  1148. (
  1149. (tfiledef(def_from).typedfiletype.def = tdef(voidtype.def)) or
  1150. (tfiledef(def_to).typedfiletype.def = tdef(voidtype.def))
  1151. )
  1152. ) then
  1153. begin
  1154. eq:=te_equal;
  1155. end;
  1156. end
  1157. else
  1158. if ((tfiledef(def_from).filetyp = ft_untyped) and
  1159. (tfiledef(def_to).filetyp = ft_typed)) or
  1160. ((tfiledef(def_from).filetyp = ft_typed) and
  1161. (tfiledef(def_to).filetyp = ft_untyped)) then
  1162. begin
  1163. doconv:=tc_equal;
  1164. eq:=te_convert_l1;
  1165. end;
  1166. end;
  1167. end;
  1168. recorddef :
  1169. begin
  1170. { interface -> guid }
  1171. if is_interface(def_from) and
  1172. (def_to=rec_tguid) then
  1173. begin
  1174. doconv:=tc_intf_2_guid;
  1175. eq:=te_convert_l1;
  1176. end;
  1177. end;
  1178. formaldef :
  1179. begin
  1180. doconv:=tc_equal;
  1181. if (def_from.deftype=formaldef) then
  1182. eq:=te_equal
  1183. else
  1184. { Just about everything can be converted to a formaldef...}
  1185. if not (def_from.deftype in [abstractdef,errordef]) then
  1186. eq:=te_convert_l1;
  1187. end;
  1188. end;
  1189. { if we didn't find an appropriate type conversion yet
  1190. then we search also the := operator }
  1191. if (eq=te_incompatible) and
  1192. (
  1193. { Check for variants? }
  1194. (
  1195. (cdo_allow_variant in cdoptions) and
  1196. ((def_from.deftype=variantdef) or (def_to.deftype=variantdef))
  1197. ) or
  1198. { Check for operators? }
  1199. (
  1200. (cdo_check_operator in cdoptions) and
  1201. ((def_from.deftype in [objectdef,recorddef,arraydef,stringdef,variantdef]) or
  1202. (def_to.deftype in [objectdef,recorddef,arraydef,stringdef,variantdef]))
  1203. )
  1204. ) then
  1205. begin
  1206. operatorpd:=search_assignment_operator(def_from,def_to);
  1207. if assigned(operatorpd) then
  1208. eq:=te_convert_operator;
  1209. end;
  1210. { update convtype for te_equal when it is not yet set }
  1211. if (eq=te_equal) and
  1212. (doconv=tc_not_possible) then
  1213. doconv:=tc_equal;
  1214. compare_defs_ext:=eq;
  1215. end;
  1216. function equal_defs(def_from,def_to:tdef):boolean;
  1217. var
  1218. convtyp : tconverttype;
  1219. pd : tprocdef;
  1220. begin
  1221. { Compare defs with nothingn and no explicit typecasts and
  1222. searching for overloaded operators is not needed }
  1223. equal_defs:=(compare_defs_ext(def_from,def_to,nothingn,convtyp,pd,[])>=te_equal);
  1224. end;
  1225. function compare_defs(def_from,def_to:tdef;fromtreetype:tnodetype):tequaltype;
  1226. var
  1227. doconv : tconverttype;
  1228. pd : tprocdef;
  1229. begin
  1230. compare_defs:=compare_defs_ext(def_from,def_to,fromtreetype,doconv,pd,[cdo_check_operator,cdo_allow_variant]);
  1231. end;
  1232. function is_subequal(def1, def2: tdef): boolean;
  1233. var
  1234. basedef1,basedef2 : tenumdef;
  1235. Begin
  1236. is_subequal := false;
  1237. if assigned(def1) and assigned(def2) then
  1238. Begin
  1239. if (def1.deftype = orddef) and (def2.deftype = orddef) then
  1240. Begin
  1241. { see p.47 of Turbo Pascal 7.01 manual for the separation of types }
  1242. { range checking for case statements is done with testrange }
  1243. case torddef(def1).typ of
  1244. u8bit,u16bit,u32bit,u64bit,
  1245. s8bit,s16bit,s32bit,s64bit :
  1246. is_subequal:=(torddef(def2).typ in [s64bit,u64bit,s32bit,u32bit,u8bit,s8bit,s16bit,u16bit]);
  1247. bool8bit,bool16bit,bool32bit :
  1248. is_subequal:=(torddef(def2).typ in [bool8bit,bool16bit,bool32bit]);
  1249. uchar :
  1250. is_subequal:=(torddef(def2).typ=uchar);
  1251. uwidechar :
  1252. is_subequal:=(torddef(def2).typ=uwidechar);
  1253. end;
  1254. end
  1255. else
  1256. Begin
  1257. { Check if both basedefs are equal }
  1258. if (def1.deftype=enumdef) and (def2.deftype=enumdef) then
  1259. Begin
  1260. { get both basedefs }
  1261. basedef1:=tenumdef(def1);
  1262. while assigned(basedef1.basedef) do
  1263. basedef1:=basedef1.basedef;
  1264. basedef2:=tenumdef(def2);
  1265. while assigned(basedef2.basedef) do
  1266. basedef2:=basedef2.basedef;
  1267. is_subequal:=(basedef1=basedef2);
  1268. end;
  1269. end;
  1270. end;
  1271. end;
  1272. function compare_paras(para1,para2 : tlist; acp : tcompare_paras_type; cpoptions: tcompare_paras_options):tequaltype;
  1273. var
  1274. currpara1,
  1275. currpara2 : tparavarsym;
  1276. eq,lowesteq : tequaltype;
  1277. hpd : tprocdef;
  1278. convtype : tconverttype;
  1279. cdoptions : tcompare_defs_options;
  1280. i1,i2 : byte;
  1281. begin
  1282. compare_paras:=te_incompatible;
  1283. cdoptions:=[cdo_check_operator,cdo_allow_variant];
  1284. { we need to parse the list from left-right so the
  1285. not-default parameters are checked first }
  1286. lowesteq:=high(tequaltype);
  1287. i1:=0;
  1288. i2:=0;
  1289. if cpo_ignorehidden in cpoptions then
  1290. begin
  1291. while (i1<para1.count) and
  1292. (vo_is_hidden_para in tparavarsym(para1[i1]).varoptions) do
  1293. inc(i1);
  1294. while (i2<para2.count) and
  1295. (vo_is_hidden_para in tparavarsym(para2[i2]).varoptions) do
  1296. inc(i2);
  1297. end;
  1298. while (i1<para1.count) and (i2<para2.count) do
  1299. begin
  1300. eq:=te_incompatible;
  1301. currpara1:=tparavarsym(para1[i1]);
  1302. currpara2:=tparavarsym(para2[i2]);
  1303. { Unique types must match exact }
  1304. if ((df_unique in currpara1.vartype.def.defoptions) or (df_unique in currpara2.vartype.def.defoptions)) and
  1305. (currpara1.vartype.def<>currpara2.vartype.def) then
  1306. exit;
  1307. { Handle hidden parameters separately, because self is
  1308. defined as voidpointer for methodpointers }
  1309. if (vo_is_hidden_para in currpara1.varoptions) or
  1310. (vo_is_hidden_para in currpara2.varoptions) then
  1311. begin
  1312. { both must be hidden }
  1313. if (vo_is_hidden_para in currpara1.varoptions)<>(vo_is_hidden_para in currpara2.varoptions) then
  1314. exit;
  1315. eq:=te_equal;
  1316. if not(vo_is_self in currpara1.varoptions) and
  1317. not(vo_is_self in currpara2.varoptions) then
  1318. begin
  1319. if (currpara1.varspez<>currpara2.varspez) then
  1320. exit;
  1321. eq:=compare_defs_ext(currpara1.vartype.def,currpara2.vartype.def,nothingn,
  1322. convtype,hpd,cdoptions);
  1323. end;
  1324. end
  1325. else
  1326. begin
  1327. case acp of
  1328. cp_value_equal_const :
  1329. begin
  1330. if (
  1331. (currpara1.varspez<>currpara2.varspez) and
  1332. ((currpara1.varspez in [vs_var,vs_out]) or
  1333. (currpara2.varspez in [vs_var,vs_out]))
  1334. ) then
  1335. exit;
  1336. eq:=compare_defs_ext(currpara1.vartype.def,currpara2.vartype.def,nothingn,
  1337. convtype,hpd,cdoptions);
  1338. end;
  1339. cp_all :
  1340. begin
  1341. if (currpara1.varspez<>currpara2.varspez) then
  1342. exit;
  1343. eq:=compare_defs_ext(currpara1.vartype.def,currpara2.vartype.def,nothingn,
  1344. convtype,hpd,cdoptions);
  1345. end;
  1346. cp_procvar :
  1347. begin
  1348. if (currpara1.varspez<>currpara2.varspez) then
  1349. exit;
  1350. eq:=compare_defs_ext(currpara1.vartype.def,currpara2.vartype.def,nothingn,
  1351. convtype,hpd,cdoptions);
  1352. { Parameters must be at least equal otherwise the are incompatible }
  1353. if (eq<te_equal) then
  1354. eq:=te_incompatible;
  1355. end;
  1356. else
  1357. eq:=compare_defs_ext(currpara1.vartype.def,currpara2.vartype.def,nothingn,
  1358. convtype,hpd,cdoptions);
  1359. end;
  1360. end;
  1361. { check type }
  1362. if eq=te_incompatible then
  1363. exit;
  1364. if eq<lowesteq then
  1365. lowesteq:=eq;
  1366. { also check default value if both have it declared }
  1367. if (cpo_comparedefaultvalue in cpoptions) and
  1368. assigned(currpara1.defaultconstsym) and
  1369. assigned(currpara2.defaultconstsym) then
  1370. begin
  1371. if not equal_constsym(tconstsym(currpara1.defaultconstsym),tconstsym(currpara2.defaultconstsym)) then
  1372. exit;
  1373. end;
  1374. inc(i1);
  1375. inc(i2);
  1376. if cpo_ignorehidden in cpoptions then
  1377. begin
  1378. while (i1<para1.count) and
  1379. (vo_is_hidden_para in tparavarsym(para1[i1]).varoptions) do
  1380. inc(i1);
  1381. while (i2<para2.count) and
  1382. (vo_is_hidden_para in tparavarsym(para2[i2]).varoptions) do
  1383. inc(i2);
  1384. end;
  1385. end;
  1386. { when both lists are empty then the parameters are equal. Also
  1387. when one list is empty and the other has a parameter with default
  1388. value assigned then the parameters are also equal }
  1389. if ((i1>=para1.count) and (i2>=para2.count)) or
  1390. ((cpo_allowdefaults in cpoptions) and
  1391. (((i1<para1.count) and assigned(tparavarsym(para1[i1]).defaultconstsym)) or
  1392. ((i2<para2.count) and assigned(tparavarsym(para2[i2]).defaultconstsym)))) then
  1393. compare_paras:=lowesteq;
  1394. end;
  1395. function proc_to_procvar_equal(def1:tabstractprocdef;def2:tprocvardef;methoderr:boolean):tequaltype;
  1396. var
  1397. eq : tequaltype;
  1398. po_comp : tprocoptions;
  1399. begin
  1400. proc_to_procvar_equal:=te_incompatible;
  1401. if not(assigned(def1)) or not(assigned(def2)) then
  1402. exit;
  1403. { check for method pointer }
  1404. if (def1.is_methodpointer xor def2.is_methodpointer) or
  1405. (def1.is_addressonly xor def2.is_addressonly) then
  1406. begin
  1407. if methoderr then
  1408. Message(type_e_no_method_and_procedure_not_compatible);
  1409. exit;
  1410. end;
  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.