defcmp.pas 65 KB

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