defcmp.pas 85 KB

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