defcmp.pas 86 KB

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