defcmp.pas 73 KB

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