defcmp.pas 72 KB

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