defcmp.pas 52 KB

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