defcmp.pas 63 KB

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