defcmp.pas 53 KB

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