defcmp.pas 86 KB

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