defcmp.pas 63 KB

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