defcmp.pas 66 KB

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