defcmp.pas 79 KB

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