defcmp.pas 68 KB

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