defcmp.pas 67 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571
  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. (
  865. (
  866. (cdo_explicit in cdoptions) and
  867. (
  868. (m_delphi in current_settings.modeswitches) or
  869. { Don't allow pchar(char) in fpc modes }
  870. is_integer(def_from)
  871. )
  872. ) or
  873. (cdo_internal in cdoptions)
  874. ) then
  875. begin
  876. doconv:=tc_int_2_int;
  877. eq:=te_convert_l1;
  878. end;
  879. end;
  880. arraydef :
  881. begin
  882. { string constant (which can be part of array constructor)
  883. to zero terminated string constant }
  884. if (fromtreetype in [arrayconstructorn,stringconstn]) and
  885. (is_pchar(def_to) or is_pwidechar(def_to)) then
  886. begin
  887. doconv:=tc_cstring_2_pchar;
  888. eq:=te_convert_l2;
  889. end
  890. else
  891. { chararray to pointer }
  892. if (is_zero_based_array(def_from) or
  893. is_open_array(def_from)) and
  894. equal_defs(tarraydef(def_from).elementdef,tpointerdef(def_to).pointeddef) then
  895. begin
  896. doconv:=tc_array_2_pointer;
  897. { don't prefer the pchar overload when a constant
  898. string was passed }
  899. if fromtreetype=stringconstn then
  900. eq:=te_convert_l2
  901. else
  902. eq:=te_convert_l1;
  903. end
  904. else
  905. { dynamic array to pointer, delphi only }
  906. if (m_delphi in current_settings.modeswitches) and
  907. is_dynamic_array(def_from) then
  908. begin
  909. eq:=te_equal;
  910. end;
  911. end;
  912. pointerdef :
  913. begin
  914. { check for far pointers }
  915. if (tpointerdef(def_from).is_far<>tpointerdef(def_to).is_far) then
  916. begin
  917. eq:=te_incompatible;
  918. end
  919. else
  920. { the types can be forward type, handle before normal type check !! }
  921. if assigned(def_to.typesym) and
  922. (tpointerdef(def_to).pointeddef.typ=forwarddef) then
  923. begin
  924. if (def_from.typesym=def_to.typesym) then
  925. eq:=te_equal
  926. end
  927. else
  928. { same types }
  929. if equal_defs(tpointerdef(def_from).pointeddef,tpointerdef(def_to).pointeddef) then
  930. begin
  931. eq:=te_equal
  932. end
  933. else
  934. { child class pointer can be assigned to anchestor pointers }
  935. if (
  936. (tpointerdef(def_from).pointeddef.typ=objectdef) and
  937. (tpointerdef(def_to).pointeddef.typ=objectdef) and
  938. tobjectdef(tpointerdef(def_from).pointeddef).is_related(
  939. tobjectdef(tpointerdef(def_to).pointeddef))
  940. ) then
  941. begin
  942. doconv:=tc_equal;
  943. eq:=te_convert_l1;
  944. end
  945. else
  946. { all pointers can be assigned to void-pointer }
  947. if is_void(tpointerdef(def_to).pointeddef) then
  948. begin
  949. doconv:=tc_equal;
  950. { give pwidechar,pchar a penalty so it prefers
  951. conversion to ansistring }
  952. if is_pchar(def_from) or
  953. is_pwidechar(def_from) then
  954. eq:=te_convert_l2
  955. else
  956. eq:=te_convert_l1;
  957. end
  958. else
  959. { all pointers can be assigned from void-pointer }
  960. if is_void(tpointerdef(def_from).pointeddef) or
  961. { all pointers can be assigned from void-pointer or formaldef pointer, check
  962. tw3777.pp if you change this }
  963. (tpointerdef(def_from).pointeddef.typ=formaldef) then
  964. begin
  965. doconv:=tc_equal;
  966. { give pwidechar a penalty so it prefers
  967. conversion to pchar }
  968. if is_pwidechar(def_to) then
  969. eq:=te_convert_l2
  970. else
  971. eq:=te_convert_l1;
  972. end;
  973. end;
  974. procvardef :
  975. begin
  976. { procedure variable can be assigned to an void pointer,
  977. this not allowed for methodpointers }
  978. if (is_void(tpointerdef(def_to).pointeddef) or
  979. (m_mac_procvar in current_settings.modeswitches)) and
  980. tprocvardef(def_from).is_addressonly then
  981. begin
  982. doconv:=tc_equal;
  983. eq:=te_convert_l1;
  984. end;
  985. end;
  986. procdef :
  987. begin
  988. { procedure variable can be assigned to an void pointer,
  989. this not allowed for methodpointers }
  990. if (m_mac_procvar in current_settings.modeswitches) and
  991. tprocdef(def_from).is_addressonly then
  992. begin
  993. doconv:=tc_proc_2_procvar;
  994. eq:=te_convert_l2;
  995. end;
  996. end;
  997. classrefdef,
  998. objectdef :
  999. begin
  1000. { class types and class reference type
  1001. can be assigned to void pointers, but it is less
  1002. preferred than assigning to a related objectdef }
  1003. if (
  1004. is_class_or_interface_or_dispinterface(def_from) or
  1005. (def_from.typ=classrefdef)
  1006. ) and
  1007. (tpointerdef(def_to).pointeddef.typ=orddef) and
  1008. (torddef(tpointerdef(def_to).pointeddef).ordtype=uvoid) then
  1009. begin
  1010. doconv:=tc_equal;
  1011. eq:=te_convert_l2;
  1012. end;
  1013. end;
  1014. end;
  1015. end;
  1016. setdef :
  1017. begin
  1018. case def_from.typ of
  1019. setdef :
  1020. begin
  1021. if assigned(tsetdef(def_from).elementdef) and
  1022. assigned(tsetdef(def_to).elementdef) then
  1023. begin
  1024. { sets with the same element base type are equal }
  1025. if is_subequal(tsetdef(def_from).elementdef,tsetdef(def_to).elementdef) then
  1026. eq:=te_equal;
  1027. end
  1028. else
  1029. { empty set is compatible with everything }
  1030. eq:=te_equal;
  1031. end;
  1032. arraydef :
  1033. begin
  1034. { automatic arrayconstructor -> set conversion }
  1035. if is_array_constructor(def_from) then
  1036. begin
  1037. doconv:=tc_arrayconstructor_2_set;
  1038. eq:=te_convert_l1;
  1039. end;
  1040. end;
  1041. end;
  1042. end;
  1043. procvardef :
  1044. begin
  1045. case def_from.typ of
  1046. procdef :
  1047. begin
  1048. { proc -> procvar }
  1049. if (m_tp_procvar in current_settings.modeswitches) or
  1050. (m_mac_procvar in current_settings.modeswitches) then
  1051. begin
  1052. subeq:=proc_to_procvar_equal(tprocdef(def_from),tprocvardef(def_to));
  1053. if subeq>te_incompatible then
  1054. begin
  1055. doconv:=tc_proc_2_procvar;
  1056. eq:=te_convert_l1;
  1057. end;
  1058. end;
  1059. end;
  1060. procvardef :
  1061. begin
  1062. { procvar -> procvar }
  1063. eq:=proc_to_procvar_equal(tprocvardef(def_from),tprocvardef(def_to));
  1064. end;
  1065. pointerdef :
  1066. begin
  1067. { nil is compatible with procvars }
  1068. if (fromtreetype=niln) then
  1069. begin
  1070. doconv:=tc_equal;
  1071. eq:=te_convert_l1;
  1072. end
  1073. else
  1074. { for example delphi allows the assignement from pointers }
  1075. { to procedure variables }
  1076. if (m_pointer_2_procedure in current_settings.modeswitches) and
  1077. is_void(tpointerdef(def_from).pointeddef) and
  1078. tprocvardef(def_to).is_addressonly then
  1079. begin
  1080. doconv:=tc_equal;
  1081. eq:=te_convert_l1;
  1082. end;
  1083. end;
  1084. end;
  1085. end;
  1086. objectdef :
  1087. begin
  1088. { object pascal objects }
  1089. if (def_from.typ=objectdef) and
  1090. (tobjectdef(def_from).is_related(tobjectdef(def_to))) then
  1091. begin
  1092. doconv:=tc_equal;
  1093. eq:=te_convert_l1;
  1094. end
  1095. else
  1096. { Class/interface specific }
  1097. if is_class_or_interface_or_dispinterface(def_to) then
  1098. begin
  1099. { void pointer also for delphi mode }
  1100. if (m_delphi in current_settings.modeswitches) and
  1101. is_voidpointer(def_from) then
  1102. begin
  1103. doconv:=tc_equal;
  1104. { prefer pointer-pointer assignments }
  1105. eq:=te_convert_l2;
  1106. end
  1107. else
  1108. { nil is compatible with class instances and interfaces }
  1109. if (fromtreetype=niln) then
  1110. begin
  1111. doconv:=tc_equal;
  1112. eq:=te_convert_l1;
  1113. end
  1114. { classes can be assigned to interfaces }
  1115. else if is_interface(def_to) and
  1116. is_class(def_from) and
  1117. assigned(tobjectdef(def_from).ImplementedInterfaces) then
  1118. begin
  1119. { we've to search in parent classes as well }
  1120. hobjdef:=tobjectdef(def_from);
  1121. while assigned(hobjdef) do
  1122. begin
  1123. if hobjdef.find_implemented_interface(tobjectdef(def_to))<>nil then
  1124. begin
  1125. doconv:=tc_class_2_intf;
  1126. { don't prefer this over objectdef->objectdef }
  1127. eq:=te_convert_l2;
  1128. break;
  1129. end;
  1130. hobjdef:=hobjdef.childof;
  1131. end;
  1132. end
  1133. { Interface 2 GUID handling }
  1134. else if (def_to=tdef(rec_tguid)) and
  1135. (fromtreetype=typen) and
  1136. is_interface(def_from) and
  1137. assigned(tobjectdef(def_from).iidguid) then
  1138. begin
  1139. eq:=te_convert_l1;
  1140. doconv:=tc_equal;
  1141. end
  1142. else if (def_from.typ=variantdef) and is_interface(def_to) then
  1143. begin
  1144. doconv:=tc_variant_2_interface;
  1145. eq:=te_convert_l2;
  1146. end
  1147. { ugly, but delphi allows it }
  1148. else if (eq=te_incompatible) and
  1149. (def_from.typ=orddef) and
  1150. (m_delphi in current_settings.modeswitches) and
  1151. (cdo_explicit in cdoptions) then
  1152. begin
  1153. doconv:=tc_int_2_int;
  1154. eq:=te_convert_l1;
  1155. end;
  1156. end;
  1157. end;
  1158. classrefdef :
  1159. begin
  1160. { similar to pointerdef wrt forwards }
  1161. if assigned(def_to.typesym) and
  1162. (tclassrefdef(def_to).pointeddef.typ=forwarddef) then
  1163. begin
  1164. if (def_from.typesym=def_to.typesym) then
  1165. eq:=te_equal;
  1166. end
  1167. else
  1168. { class reference types }
  1169. if (def_from.typ=classrefdef) then
  1170. begin
  1171. if equal_defs(tclassrefdef(def_from).pointeddef,tclassrefdef(def_to).pointeddef) then
  1172. begin
  1173. eq:=te_equal;
  1174. end
  1175. else
  1176. begin
  1177. doconv:=tc_equal;
  1178. if (cdo_explicit in cdoptions) or
  1179. tobjectdef(tclassrefdef(def_from).pointeddef).is_related(
  1180. tobjectdef(tclassrefdef(def_to).pointeddef)) then
  1181. eq:=te_convert_l1;
  1182. end;
  1183. end
  1184. else
  1185. { nil is compatible with class references }
  1186. if (fromtreetype=niln) then
  1187. begin
  1188. doconv:=tc_equal;
  1189. eq:=te_convert_l1;
  1190. end;
  1191. end;
  1192. filedef :
  1193. begin
  1194. { typed files are all equal to the abstract file type
  1195. name TYPEDFILE in system.pp in is_equal in types.pas
  1196. the problem is that it sholud be also compatible to FILE
  1197. but this would leed to a problem for ASSIGN RESET and REWRITE
  1198. when trying to find the good overloaded function !!
  1199. so all file function are doubled in system.pp
  1200. this is not very beautiful !!}
  1201. if (def_from.typ=filedef) then
  1202. begin
  1203. if (tfiledef(def_from).filetyp=tfiledef(def_to).filetyp) then
  1204. begin
  1205. if
  1206. (
  1207. (tfiledef(def_from).typedfiledef=nil) and
  1208. (tfiledef(def_to).typedfiledef=nil)
  1209. ) or
  1210. (
  1211. (tfiledef(def_from).typedfiledef<>nil) and
  1212. (tfiledef(def_to).typedfiledef<>nil) and
  1213. equal_defs(tfiledef(def_from).typedfiledef,tfiledef(def_to).typedfiledef)
  1214. ) or
  1215. (
  1216. (tfiledef(def_from).filetyp = ft_typed) and
  1217. (tfiledef(def_to).filetyp = ft_typed) and
  1218. (
  1219. (tfiledef(def_from).typedfiledef = tdef(voidtype)) or
  1220. (tfiledef(def_to).typedfiledef = tdef(voidtype))
  1221. )
  1222. ) then
  1223. begin
  1224. eq:=te_equal;
  1225. end;
  1226. end
  1227. else
  1228. if ((tfiledef(def_from).filetyp = ft_untyped) and
  1229. (tfiledef(def_to).filetyp = ft_typed)) or
  1230. ((tfiledef(def_from).filetyp = ft_typed) and
  1231. (tfiledef(def_to).filetyp = ft_untyped)) then
  1232. begin
  1233. doconv:=tc_equal;
  1234. eq:=te_convert_l1;
  1235. end;
  1236. end;
  1237. end;
  1238. recorddef :
  1239. begin
  1240. { interface -> guid }
  1241. if is_interface(def_from) and
  1242. (def_to=rec_tguid) then
  1243. begin
  1244. doconv:=tc_intf_2_guid;
  1245. eq:=te_convert_l1;
  1246. end;
  1247. end;
  1248. formaldef :
  1249. begin
  1250. doconv:=tc_equal;
  1251. if (def_from.typ=formaldef) then
  1252. eq:=te_equal
  1253. else
  1254. { Just about everything can be converted to a formaldef...}
  1255. if not (def_from.typ in [abstractdef,errordef]) then
  1256. eq:=te_convert_l2;
  1257. end;
  1258. end;
  1259. { if we didn't find an appropriate type conversion yet
  1260. then we search also the := operator }
  1261. if (eq=te_incompatible) and
  1262. (
  1263. { Check for variants? }
  1264. (
  1265. (cdo_allow_variant in cdoptions) and
  1266. ((def_from.typ=variantdef) or (def_to.typ=variantdef))
  1267. ) or
  1268. { Check for operators? }
  1269. (
  1270. (cdo_check_operator in cdoptions) and
  1271. ((def_from.typ in [objectdef,recorddef,arraydef,stringdef,variantdef]) or
  1272. (def_to.typ in [objectdef,recorddef,arraydef,stringdef,variantdef]))
  1273. )
  1274. ) then
  1275. begin
  1276. operatorpd:=search_assignment_operator(def_from,def_to);
  1277. if assigned(operatorpd) then
  1278. eq:=te_convert_operator;
  1279. end;
  1280. { update convtype for te_equal when it is not yet set }
  1281. if (eq=te_equal) and
  1282. (doconv=tc_not_possible) then
  1283. doconv:=tc_equal;
  1284. compare_defs_ext:=eq;
  1285. end;
  1286. function equal_defs(def_from,def_to:tdef):boolean;
  1287. var
  1288. convtyp : tconverttype;
  1289. pd : tprocdef;
  1290. begin
  1291. { Compare defs with nothingn and no explicit typecasts and
  1292. searching for overloaded operators is not needed }
  1293. equal_defs:=(compare_defs_ext(def_from,def_to,nothingn,convtyp,pd,[])>=te_equal);
  1294. end;
  1295. function compare_defs(def_from,def_to:tdef;fromtreetype:tnodetype):tequaltype;
  1296. var
  1297. doconv : tconverttype;
  1298. pd : tprocdef;
  1299. begin
  1300. compare_defs:=compare_defs_ext(def_from,def_to,fromtreetype,doconv,pd,[cdo_check_operator,cdo_allow_variant]);
  1301. end;
  1302. function is_subequal(def1, def2: tdef): boolean;
  1303. var
  1304. basedef1,basedef2 : tenumdef;
  1305. Begin
  1306. is_subequal := false;
  1307. if assigned(def1) and assigned(def2) then
  1308. Begin
  1309. if (def1.typ = orddef) and (def2.typ = orddef) then
  1310. Begin
  1311. { see p.47 of Turbo Pascal 7.01 manual for the separation of types }
  1312. { range checking for case statements is done with testrange }
  1313. case torddef(def1).ordtype of
  1314. u8bit,u16bit,u32bit,u64bit,
  1315. s8bit,s16bit,s32bit,s64bit :
  1316. is_subequal:=(torddef(def2).ordtype in [s64bit,u64bit,s32bit,u32bit,u8bit,s8bit,s16bit,u16bit]);
  1317. bool8bit,bool16bit,bool32bit,bool64bit :
  1318. is_subequal:=(torddef(def2).ordtype in [bool8bit,bool16bit,bool32bit,bool64bit]);
  1319. uchar :
  1320. is_subequal:=(torddef(def2).ordtype=uchar);
  1321. uwidechar :
  1322. is_subequal:=(torddef(def2).ordtype=uwidechar);
  1323. end;
  1324. end
  1325. else
  1326. Begin
  1327. { Check if both basedefs are equal }
  1328. if (def1.typ=enumdef) and (def2.typ=enumdef) then
  1329. Begin
  1330. { get both basedefs }
  1331. basedef1:=tenumdef(def1);
  1332. while assigned(basedef1.basedef) do
  1333. basedef1:=basedef1.basedef;
  1334. basedef2:=tenumdef(def2);
  1335. while assigned(basedef2.basedef) do
  1336. basedef2:=basedef2.basedef;
  1337. is_subequal:=(basedef1=basedef2);
  1338. end;
  1339. end;
  1340. end;
  1341. end;
  1342. function compare_paras(para1,para2 : TFPObjectList; acp : tcompare_paras_type; cpoptions: tcompare_paras_options):tequaltype;
  1343. var
  1344. currpara1,
  1345. currpara2 : tparavarsym;
  1346. eq,lowesteq : tequaltype;
  1347. hpd : tprocdef;
  1348. convtype : tconverttype;
  1349. cdoptions : tcompare_defs_options;
  1350. i1,i2 : byte;
  1351. begin
  1352. compare_paras:=te_incompatible;
  1353. cdoptions:=[cdo_parameter,cdo_check_operator,cdo_allow_variant];
  1354. { we need to parse the list from left-right so the
  1355. not-default parameters are checked first }
  1356. lowesteq:=high(tequaltype);
  1357. i1:=0;
  1358. i2:=0;
  1359. if cpo_ignorehidden in cpoptions then
  1360. begin
  1361. while (i1<para1.count) and
  1362. (vo_is_hidden_para in tparavarsym(para1[i1]).varoptions) do
  1363. inc(i1);
  1364. while (i2<para2.count) and
  1365. (vo_is_hidden_para in tparavarsym(para2[i2]).varoptions) do
  1366. inc(i2);
  1367. end;
  1368. while (i1<para1.count) and (i2<para2.count) do
  1369. begin
  1370. eq:=te_incompatible;
  1371. currpara1:=tparavarsym(para1[i1]);
  1372. currpara2:=tparavarsym(para2[i2]);
  1373. { Unique types must match exact }
  1374. if ((df_unique in currpara1.vardef.defoptions) or (df_unique in currpara2.vardef.defoptions)) and
  1375. (currpara1.vardef<>currpara2.vardef) then
  1376. exit;
  1377. { Handle hidden parameters separately, because self is
  1378. defined as voidpointer for methodpointers }
  1379. if (vo_is_hidden_para in currpara1.varoptions) or
  1380. (vo_is_hidden_para in currpara2.varoptions) then
  1381. begin
  1382. { both must be hidden }
  1383. if (vo_is_hidden_para in currpara1.varoptions)<>(vo_is_hidden_para in currpara2.varoptions) then
  1384. exit;
  1385. eq:=te_equal;
  1386. if not(vo_is_self in currpara1.varoptions) and
  1387. not(vo_is_self in currpara2.varoptions) then
  1388. begin
  1389. if (currpara1.varspez<>currpara2.varspez) then
  1390. exit;
  1391. eq:=compare_defs_ext(currpara1.vardef,currpara2.vardef,nothingn,
  1392. convtype,hpd,cdoptions);
  1393. end;
  1394. end
  1395. else
  1396. begin
  1397. case acp of
  1398. cp_value_equal_const :
  1399. begin
  1400. if (
  1401. (currpara1.varspez<>currpara2.varspez) and
  1402. ((currpara1.varspez in [vs_var,vs_out]) or
  1403. (currpara2.varspez in [vs_var,vs_out]))
  1404. ) then
  1405. exit;
  1406. eq:=compare_defs_ext(currpara1.vardef,currpara2.vardef,nothingn,
  1407. convtype,hpd,cdoptions);
  1408. end;
  1409. cp_all :
  1410. begin
  1411. if (currpara1.varspez<>currpara2.varspez) then
  1412. exit;
  1413. eq:=compare_defs_ext(currpara1.vardef,currpara2.vardef,nothingn,
  1414. convtype,hpd,cdoptions);
  1415. end;
  1416. cp_procvar :
  1417. begin
  1418. if (currpara1.varspez<>currpara2.varspez) then
  1419. exit;
  1420. eq:=compare_defs_ext(currpara1.vardef,currpara2.vardef,nothingn,
  1421. convtype,hpd,cdoptions);
  1422. { Parameters must be at least equal otherwise the are incompatible }
  1423. if (eq<te_equal) then
  1424. eq:=te_incompatible;
  1425. end;
  1426. else
  1427. eq:=compare_defs_ext(currpara1.vardef,currpara2.vardef,nothingn,
  1428. convtype,hpd,cdoptions);
  1429. end;
  1430. end;
  1431. { check type }
  1432. if eq=te_incompatible then
  1433. exit;
  1434. if eq<lowesteq then
  1435. lowesteq:=eq;
  1436. { also check default value if both have it declared }
  1437. if (cpo_comparedefaultvalue in cpoptions) and
  1438. assigned(currpara1.defaultconstsym) and
  1439. assigned(currpara2.defaultconstsym) then
  1440. begin
  1441. if not equal_constsym(tconstsym(currpara1.defaultconstsym),tconstsym(currpara2.defaultconstsym)) then
  1442. exit;
  1443. end;
  1444. inc(i1);
  1445. inc(i2);
  1446. if cpo_ignorehidden in cpoptions then
  1447. begin
  1448. while (i1<para1.count) and
  1449. (vo_is_hidden_para in tparavarsym(para1[i1]).varoptions) do
  1450. inc(i1);
  1451. while (i2<para2.count) and
  1452. (vo_is_hidden_para in tparavarsym(para2[i2]).varoptions) do
  1453. inc(i2);
  1454. end;
  1455. end;
  1456. { when both lists are empty then the parameters are equal. Also
  1457. when one list is empty and the other has a parameter with default
  1458. value assigned then the parameters are also equal }
  1459. if ((i1>=para1.count) and (i2>=para2.count)) or
  1460. ((cpo_allowdefaults in cpoptions) and
  1461. (((i1<para1.count) and assigned(tparavarsym(para1[i1]).defaultconstsym)) or
  1462. ((i2<para2.count) and assigned(tparavarsym(para2[i2]).defaultconstsym)))) then
  1463. compare_paras:=lowesteq;
  1464. end;
  1465. function proc_to_procvar_equal(def1:tabstractprocdef;def2:tprocvardef):tequaltype;
  1466. var
  1467. eq : tequaltype;
  1468. po_comp : tprocoptions;
  1469. begin
  1470. proc_to_procvar_equal:=te_incompatible;
  1471. if not(assigned(def1)) or not(assigned(def2)) then
  1472. exit;
  1473. { check for method pointer }
  1474. if (def1.is_methodpointer xor def2.is_methodpointer) or
  1475. (def1.is_addressonly xor def2.is_addressonly) then
  1476. exit;
  1477. { check return value and options, methodpointer is already checked }
  1478. po_comp:=[po_staticmethod,po_interrupt,
  1479. po_iocheck,po_varargs];
  1480. if (m_delphi in current_settings.modeswitches) then
  1481. exclude(po_comp,po_varargs);
  1482. if (def1.proccalloption=def2.proccalloption) and
  1483. ((po_comp * def1.procoptions)= (po_comp * def2.procoptions)) and
  1484. equal_defs(def1.returndef,def2.returndef) then
  1485. begin
  1486. { return equal type based on the parameters, but a proc->procvar
  1487. is never exact, so map an exact match of the parameters to
  1488. te_equal }
  1489. eq:=compare_paras(def1.paras,def2.paras,cp_procvar,[]);
  1490. if eq=te_exact then
  1491. eq:=te_equal;
  1492. proc_to_procvar_equal:=eq;
  1493. end;
  1494. end;
  1495. function compatible_childmethod_resultdef(parentretdef, childretdef: tdef): boolean;
  1496. begin
  1497. compatible_childmethod_resultdef :=
  1498. (equal_defs(parentretdef,childretdef)) or
  1499. ((parentretdef.typ=objectdef) and
  1500. (childretdef.typ=objectdef) and
  1501. is_class_or_interface(parentretdef) and
  1502. is_class_or_interface(childretdef) and
  1503. (tobjectdef(childretdef).is_related(tobjectdef(parentretdef))))
  1504. end;
  1505. end.