defcmp.pas 67 KB

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