defcmp.pas 86 KB

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