defcmp.pas 66 KB

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