defcmp.pas 64 KB

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