defcmp.pas 111 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486
  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. cpo_generic // two different undefined defs (or a constraint in the forward) alone or in open arrays are
  41. // treated as exactly equal (also in open arrays) if they are owned by their respective procdefs
  42. );
  43. tcompare_paras_options = set of tcompare_paras_option;
  44. tcompare_defs_option = (
  45. cdo_internal,
  46. cdo_explicit,
  47. cdo_check_operator,
  48. cdo_allow_variant,
  49. cdo_parameter,
  50. cdo_warn_incompatible_univ,
  51. cdo_strict_undefined_check // undefined defs are incompatible to everything except other undefined defs
  52. );
  53. tcompare_defs_options = set of tcompare_defs_option;
  54. tconverttype = (tc_none,
  55. tc_equal,
  56. tc_not_possible,
  57. tc_string_2_string,
  58. tc_char_2_string,
  59. tc_char_2_chararray,
  60. tc_pchar_2_string,
  61. tc_cchar_2_pchar,
  62. tc_cstring_2_pchar,
  63. tc_cstring_2_int,
  64. tc_ansistring_2_pchar,
  65. tc_string_2_chararray,
  66. tc_chararray_2_string,
  67. tc_array_2_pointer,
  68. tc_pointer_2_array,
  69. tc_int_2_int,
  70. tc_int_2_bool,
  71. tc_bool_2_bool,
  72. tc_bool_2_int,
  73. tc_real_2_real,
  74. tc_int_2_real,
  75. tc_real_2_currency,
  76. tc_proc_2_procvar,
  77. tc_nil_2_methodprocvar,
  78. tc_arrayconstructor_2_set,
  79. tc_set_to_set,
  80. tc_cord_2_pointer,
  81. tc_intf_2_string,
  82. tc_intf_2_guid,
  83. tc_class_2_intf,
  84. tc_char_2_char,
  85. tc_dynarray_2_openarray,
  86. tc_pwchar_2_string,
  87. tc_variant_2_dynarray,
  88. tc_dynarray_2_variant,
  89. tc_variant_2_enum,
  90. tc_enum_2_variant,
  91. tc_interface_2_variant,
  92. tc_variant_2_interface,
  93. tc_array_2_dynarray,
  94. tc_elem_2_openarray
  95. );
  96. function compare_defs_ext(def_from,def_to : tdef;
  97. fromtreetype : tnodetype;
  98. var doconv : tconverttype;
  99. var operatorpd : tprocdef;
  100. cdoptions:tcompare_defs_options):tequaltype;
  101. { Returns if the type def_from can be converted to def_to or if both types are equal }
  102. function compare_defs(def_from,def_to:tdef;fromtreetype:tnodetype):tequaltype;
  103. { Returns true, if def1 and def2 are semantically the same }
  104. function equal_defs(def_from,def_to:tdef):boolean;
  105. { Checks for type compatibility (subgroups of type)
  106. used for case statements... probably missing stuff
  107. to use on other types }
  108. function is_subequal(def1, def2: tdef): boolean;
  109. {# true, if two parameter lists are equal
  110. if acp is cp_all, all have to match exactly
  111. if acp is cp_value_equal_const call by value
  112. and call by const parameter are assumed as
  113. equal
  114. if acp is cp_procvar then the varspez have to match,
  115. and all parameter types must be at least te_equal
  116. if acp is cp_none, then we don't check the varspez at all
  117. allowdefaults indicates if default value parameters
  118. are allowed (in this case, the search order will first
  119. search for a routine with default parameters, before
  120. searching for the same definition with no parameters)
  121. para1 is expected to be parameter list of the first encountered
  122. declaration (interface, forward), and para2 that of the second one
  123. (important in case of cpo_comparedefaultvalue)
  124. }
  125. function compare_paras(para1,para2 : TFPObjectList; acp : tcompare_paras_type; cpoptions: tcompare_paras_options):tequaltype;
  126. { True if a function can be assigned to a procvar }
  127. { changed first argument type to pabstractprocdef so that it can also be }
  128. { used to test compatibility between two pprocvardefs (JM) }
  129. function proc_to_procvar_equal(def1:tabstractprocdef;def2:tprocvardef;checkincompatibleuniv: boolean):tequaltype;
  130. { Parentdef is the definition of a method defined in a parent class or interface }
  131. { Childdef is the definition of a method defined in a child class, interface or }
  132. { a class implementing an interface with parentdef. }
  133. { Returns true if the resultdef of childdef can be used to implement/override }
  134. { parentdef's resultdef }
  135. function compatible_childmethod_resultdef(parentretdef, childretdef: tdef): boolean;
  136. { Checks whether the class impldef or one of its parent classes implements }
  137. { the interface intfdef and returns the corresponding "implementation link }
  138. function find_implemented_interface(impldef,intfdef:tobjectdef):timplementedinterface;
  139. { Checks whether to defs are related to each other. Thereby the following }
  140. { cases of curdef are implemented: }
  141. { - stringdef: on JVM JLObject, JLString and AnsiString are compatible }
  142. { - recorddef: on JVM records are compatible to java_fpcbaserecordtype }
  143. { and JLObject }
  144. { - objectdef: if it inherits from otherdef or they are equal }
  145. function def_is_related(curdef,otherdef:tdef):boolean;
  146. implementation
  147. uses
  148. verbose,systems,constexp,
  149. symtable,symsym,symcpu,
  150. defutil,symutil;
  151. function compare_defs_ext(def_from,def_to : tdef;
  152. fromtreetype : tnodetype;
  153. var doconv : tconverttype;
  154. var operatorpd : tprocdef;
  155. cdoptions:tcompare_defs_options):tequaltype;
  156. { tordtype:
  157. uvoid,
  158. u8bit,u16bit,u32bit,u64bit,
  159. s8bit,s16bit,s32bit,s64bit,
  160. pasbool, bool8bit,bool16bit,bool32bit,bool64bit,
  161. uchar,uwidechar,scurrency }
  162. type
  163. tbasedef=(bvoid,bchar,bint,bbool);
  164. const
  165. basedeftbl:array[tordtype] of tbasedef =
  166. (bvoid,
  167. bint,bint,bint,bint,bint,
  168. bint,bint,bint,bint,bint,
  169. bbool,bbool,bbool,bbool,
  170. bbool,bbool,bbool,bbool,
  171. bchar,bchar,bint);
  172. basedefconvertsimplicit : array[tbasedef,tbasedef] of tconverttype =
  173. { void, char, int, bool }
  174. ((tc_not_possible,tc_not_possible,tc_not_possible,tc_not_possible),
  175. (tc_not_possible,tc_char_2_char,tc_not_possible,tc_not_possible),
  176. (tc_not_possible,tc_not_possible,tc_int_2_int,tc_not_possible),
  177. (tc_not_possible,tc_not_possible,tc_not_possible,tc_bool_2_bool));
  178. basedefconvertsexplicit : array[tbasedef,tbasedef] of tconverttype =
  179. { void, char, int, bool }
  180. ((tc_not_possible,tc_not_possible,tc_not_possible,tc_not_possible),
  181. (tc_not_possible,tc_char_2_char,tc_int_2_int,tc_int_2_bool),
  182. (tc_not_possible,tc_int_2_int,tc_int_2_int,tc_int_2_bool),
  183. (tc_not_possible,tc_bool_2_int,tc_bool_2_int,tc_bool_2_bool));
  184. var
  185. subeq,eq : tequaltype;
  186. hd1,hd2 : tdef;
  187. def_generic : tstoreddef;
  188. hct : tconverttype;
  189. hobjdef : tobjectdef;
  190. hpd : tprocdef;
  191. i : longint;
  192. diff : boolean;
  193. symfrom,symto : tsym;
  194. begin
  195. eq:=te_incompatible;
  196. doconv:=tc_not_possible;
  197. { safety check }
  198. if not(assigned(def_from) and assigned(def_to)) then
  199. begin
  200. compare_defs_ext:=te_incompatible;
  201. exit;
  202. end;
  203. { resolve anonymous external definitions }
  204. if def_from.typ=objectdef then
  205. def_from:=find_real_class_definition(tobjectdef(def_from),false);
  206. if def_to.typ=objectdef then
  207. def_to:=find_real_class_definition(tobjectdef(def_to),false);
  208. { same def? then we've an exact match }
  209. if def_from=def_to then
  210. begin
  211. doconv:=tc_equal;
  212. compare_defs_ext:=te_exact;
  213. exit;
  214. end;
  215. if cdo_strict_undefined_check in cdoptions then
  216. begin
  217. { two different undefined defs are not considered equal }
  218. if (def_from.typ=undefineddef) and
  219. (def_to.typ=undefineddef) then
  220. begin
  221. doconv:=tc_not_possible;
  222. compare_defs_ext:=te_incompatible;
  223. exit;
  224. end;
  225. { if only one def is a undefined def then they are not considered as
  226. equal}
  227. if (
  228. (def_from.typ=undefineddef) or
  229. assigned(tstoreddef(def_from).genconstraintdata)
  230. ) or (
  231. (def_to.typ=undefineddef) or
  232. assigned(tstoreddef(def_to).genconstraintdata)
  233. ) then
  234. begin
  235. doconv:=tc_not_possible;
  236. compare_defs_ext:=te_incompatible;
  237. exit;
  238. end;
  239. end
  240. else
  241. begin
  242. { undefined defs are considered equal to everything }
  243. if (def_from.typ=undefineddef) or
  244. (def_to.typ=undefineddef) then
  245. begin
  246. doconv:=tc_equal;
  247. compare_defs_ext:=te_exact;
  248. exit;
  249. end;
  250. { either type has constraints }
  251. if assigned(tstoreddef(def_from).genconstraintdata) or
  252. assigned(tstoreddef(def_to).genconstraintdata) then
  253. begin
  254. if def_from.typ<>def_to.typ then
  255. begin
  256. { not compatible anyway }
  257. doconv:=tc_not_possible;
  258. compare_defs_ext:=te_incompatible;
  259. exit;
  260. end;
  261. { one is definitely a constraint, for the other we don't
  262. care right now }
  263. doconv:=tc_equal;
  264. compare_defs_ext:=te_exact;
  265. exit;
  266. end;
  267. end;
  268. { two specializations are considered equal if they specialize the same
  269. generic with the same types }
  270. if (df_specialization in def_from.defoptions) and
  271. (df_specialization in def_to.defoptions) and
  272. (tstoreddef(def_from).genericdef=tstoreddef(def_to).genericdef) then
  273. begin
  274. if assigned(tstoreddef(def_from).genericparas) xor
  275. assigned(tstoreddef(def_to).genericparas) then
  276. internalerror(2013030901);
  277. diff:=false;
  278. if assigned(tstoreddef(def_from).genericparas) then
  279. begin
  280. if tstoreddef(def_from).genericparas.count<>tstoreddef(def_to).genericparas.count then
  281. internalerror(2012091301);
  282. for i:=0 to tstoreddef(def_from).genericparas.count-1 do
  283. begin
  284. if tstoreddef(def_from).genericparas.nameofindex(i)<>tstoreddef(def_to).genericparas.nameofindex(i) then
  285. internalerror(2012091302);
  286. symfrom:=ttypesym(tstoreddef(def_from).genericparas[i]);
  287. symto:=ttypesym(tstoreddef(def_to).genericparas[i]);
  288. if not (symfrom.typ=typesym) or not (symto.typ=typesym) then
  289. internalerror(2012121401);
  290. if not equal_defs(ttypesym(symfrom).typedef,ttypesym(symto).typedef) then
  291. diff:=true;
  292. if diff then
  293. break;
  294. end;
  295. end;
  296. if not diff then
  297. begin
  298. doconv:=tc_equal;
  299. { the definitions are not exactly the same, but only equal }
  300. compare_defs_ext:=te_equal;
  301. exit;
  302. end;
  303. end;
  304. { handling of partial specializations }
  305. if (
  306. (df_generic in def_to.defoptions) and
  307. (df_specialization in def_from.defoptions) and
  308. (tstoreddef(def_from).genericdef=def_to)
  309. ) or (
  310. (df_generic in def_from.defoptions) and
  311. (df_specialization in def_to.defoptions) and
  312. (tstoreddef(def_to).genericdef=def_from)
  313. ) then
  314. begin
  315. if tstoreddef(def_from).genericdef=def_to then
  316. def_generic:=tstoreddef(def_to)
  317. else
  318. def_generic:=tstoreddef(def_from);
  319. if not assigned(def_generic.genericparas) then
  320. internalerror(2014052306);
  321. diff:=false;
  322. for i:=0 to def_generic.genericparas.count-1 do
  323. begin
  324. symfrom:=tsym(def_generic.genericparas[i]);
  325. if symfrom.typ<>typesym then
  326. internalerror(2014052307);
  327. if ttypesym(symfrom).typedef.typ<>undefineddef then
  328. diff:=true;
  329. if diff then
  330. break;
  331. end;
  332. if not diff then
  333. begin
  334. doconv:=tc_equal;
  335. { the definitions are not exactly the same, but only equal }
  336. compare_defs_ext:=te_equal;
  337. exit;
  338. end;
  339. end;
  340. { we walk the wanted (def_to) types and check then the def_from
  341. types if there is a conversion possible }
  342. case def_to.typ of
  343. orddef :
  344. begin
  345. case def_from.typ of
  346. orddef :
  347. begin
  348. if (torddef(def_from).ordtype=torddef(def_to).ordtype) then
  349. begin
  350. case torddef(def_from).ordtype of
  351. uchar,uwidechar,
  352. u8bit,u16bit,u32bit,u64bit,
  353. s8bit,s16bit,s32bit,s64bit:
  354. begin
  355. if (torddef(def_from).low>=torddef(def_to).low) and
  356. (torddef(def_from).high<=torddef(def_to).high) then
  357. eq:=te_equal
  358. else
  359. begin
  360. doconv:=tc_int_2_int;
  361. eq:=te_convert_l1;
  362. end;
  363. end;
  364. uvoid,
  365. pasbool8,pasbool16,pasbool32,pasbool64,
  366. bool8bit,bool16bit,bool32bit,bool64bit:
  367. eq:=te_equal;
  368. else
  369. internalerror(200210061);
  370. end;
  371. end
  372. { currency cannot be implicitly converted to an ordinal
  373. type }
  374. else if not is_currency(def_from) or
  375. (cdo_explicit in cdoptions) then
  376. begin
  377. if cdo_explicit in cdoptions then
  378. doconv:=basedefconvertsexplicit[basedeftbl[torddef(def_from).ordtype],basedeftbl[torddef(def_to).ordtype]]
  379. else
  380. doconv:=basedefconvertsimplicit[basedeftbl[torddef(def_from).ordtype],basedeftbl[torddef(def_to).ordtype]];
  381. if (doconv=tc_not_possible) then
  382. eq:=te_incompatible
  383. else if (not is_in_limit(def_from,def_to)) then
  384. { "punish" bad type conversions :) (JM) }
  385. eq:=te_convert_l3
  386. else
  387. eq:=te_convert_l1;
  388. end;
  389. end;
  390. enumdef :
  391. begin
  392. { needed for char(enum) }
  393. if cdo_explicit in cdoptions then
  394. begin
  395. doconv:=tc_int_2_int;
  396. eq:=te_convert_l1;
  397. end;
  398. end;
  399. floatdef :
  400. begin
  401. if is_currency(def_to) then
  402. begin
  403. doconv:=tc_real_2_currency;
  404. eq:=te_convert_l2;
  405. end;
  406. end;
  407. objectdef:
  408. begin
  409. if (m_delphi in current_settings.modeswitches) and
  410. is_implicit_pointer_object_type(def_from) and
  411. (cdo_explicit in cdoptions) then
  412. begin
  413. eq:=te_convert_l1;
  414. if (fromtreetype=niln) then
  415. begin
  416. { will be handled by the constant folding }
  417. doconv:=tc_equal;
  418. end
  419. else
  420. doconv:=tc_int_2_int;
  421. end;
  422. end;
  423. classrefdef,
  424. procvardef,
  425. pointerdef :
  426. begin
  427. if cdo_explicit in cdoptions then
  428. begin
  429. eq:=te_convert_l1;
  430. if (fromtreetype=niln) then
  431. begin
  432. { will be handled by the constant folding }
  433. doconv:=tc_equal;
  434. end
  435. else
  436. doconv:=tc_int_2_int;
  437. end;
  438. end;
  439. arraydef :
  440. begin
  441. if (m_mac in current_settings.modeswitches) and
  442. (fromtreetype=stringconstn) then
  443. begin
  444. eq:=te_convert_l3;
  445. doconv:=tc_cstring_2_int;
  446. end;
  447. end;
  448. end;
  449. end;
  450. stringdef :
  451. begin
  452. case def_from.typ of
  453. stringdef :
  454. begin
  455. { Constant string }
  456. if (fromtreetype=stringconstn) and
  457. is_shortstring(def_from) and
  458. is_shortstring(def_to) then
  459. eq:=te_equal
  460. else if (tstringdef(def_to).stringtype=st_ansistring) and
  461. (tstringdef(def_from).stringtype=st_ansistring) then
  462. begin
  463. { don't convert ansistrings if any condition is true:
  464. 1) same encoding
  465. 2) from explicit codepage ansistring to ansistring and vice versa
  466. 3) from any ansistring to rawbytestring
  467. 4) from rawbytestring to any ansistring }
  468. if (tstringdef(def_from).encoding=tstringdef(def_to).encoding) or
  469. ((tstringdef(def_to).encoding=0) and (tstringdef(def_from).encoding=getansistringcodepage)) or
  470. ((tstringdef(def_to).encoding=getansistringcodepage) and (tstringdef(def_from).encoding=0)) or
  471. (tstringdef(def_to).encoding=globals.CP_NONE) or
  472. (tstringdef(def_from).encoding=globals.CP_NONE) then
  473. begin
  474. eq:=te_equal;
  475. end
  476. else
  477. begin
  478. doconv := tc_string_2_string;
  479. { prefere conversion to utf8 codepage }
  480. if tstringdef(def_to).encoding = globals.CP_UTF8 then
  481. eq:=te_convert_l1
  482. { else to AnsiString type }
  483. else if def_to=getansistringdef then
  484. eq:=te_convert_l2
  485. { else to AnsiString with other codepage }
  486. else
  487. eq:=te_convert_l3;
  488. end
  489. end
  490. else
  491. { same string type ? }
  492. if (tstringdef(def_from).stringtype=tstringdef(def_to).stringtype) and
  493. { for shortstrings also the length must match }
  494. ((tstringdef(def_from).stringtype<>st_shortstring) or
  495. (tstringdef(def_from).len=tstringdef(def_to).len)) and
  496. { for ansi- and unicodestrings also the encoding must match }
  497. (not(tstringdef(def_from).stringtype in [st_ansistring,st_unicodestring]) or
  498. (tstringdef(def_from).encoding=tstringdef(def_to).encoding)) then
  499. eq:=te_equal
  500. else
  501. begin
  502. doconv:=tc_string_2_string;
  503. case tstringdef(def_from).stringtype of
  504. st_widestring :
  505. begin
  506. case tstringdef(def_to).stringtype of
  507. { Prefer conversions to unicodestring }
  508. st_unicodestring: eq:=te_convert_l1;
  509. { else prefer conversions to ansistring }
  510. st_ansistring: eq:=te_convert_l2;
  511. else
  512. eq:=te_convert_l3;
  513. end;
  514. end;
  515. st_unicodestring :
  516. begin
  517. case tstringdef(def_to).stringtype of
  518. { Prefer conversions to widestring }
  519. st_widestring: eq:=te_convert_l1;
  520. { else prefer conversions to ansistring }
  521. st_ansistring: eq:=te_convert_l2;
  522. else
  523. eq:=te_convert_l3;
  524. end;
  525. end;
  526. st_shortstring :
  527. begin
  528. { Prefer shortstrings of different length or conversions
  529. from shortstring to ansistring }
  530. case tstringdef(def_to).stringtype of
  531. st_shortstring: eq:=te_convert_l1;
  532. st_ansistring:
  533. if tstringdef(def_to).encoding=globals.CP_UTF8 then
  534. eq:=te_convert_l2
  535. else if def_to=getansistringdef then
  536. eq:=te_convert_l3
  537. else
  538. eq:=te_convert_l4;
  539. st_unicodestring: eq:=te_convert_l5;
  540. else
  541. eq:=te_convert_l6;
  542. end;
  543. end;
  544. st_ansistring :
  545. begin
  546. { Prefer conversion to widestrings }
  547. case tstringdef(def_to).stringtype of
  548. st_unicodestring: eq:=te_convert_l4;
  549. st_widestring: eq:=te_convert_l5;
  550. else
  551. eq:=te_convert_l6;
  552. end;
  553. end;
  554. end;
  555. end;
  556. end;
  557. orddef :
  558. begin
  559. { char to string}
  560. if is_char(def_from) then
  561. begin
  562. doconv:=tc_char_2_string;
  563. case tstringdef(def_to).stringtype of
  564. st_shortstring: eq:=te_convert_l1;
  565. st_ansistring: eq:=te_convert_l2;
  566. st_unicodestring: eq:=te_convert_l3;
  567. st_widestring: eq:=te_convert_l4;
  568. else
  569. eq:=te_convert_l5;
  570. end;
  571. end
  572. else
  573. if is_widechar(def_from) then
  574. begin
  575. doconv:=tc_char_2_string;
  576. case tstringdef(def_to).stringtype of
  577. st_unicodestring: eq:=te_convert_l1;
  578. st_widestring: eq:=te_convert_l2;
  579. st_ansistring: eq:=te_convert_l3;
  580. st_shortstring: eq:=te_convert_l4;
  581. else
  582. eq:=te_convert_l5;
  583. end;
  584. end;
  585. end;
  586. arraydef :
  587. begin
  588. { array of char to string, the length check is done by the firstpass of this node }
  589. if (is_chararray(def_from) or
  590. is_open_chararray(def_from)) and
  591. { bitpacked arrays of char whose element bitsize is not
  592. 8 cannot be auto-converted to strings }
  593. (not is_packed_array(def_from) or
  594. (tarraydef(def_from).elementdef.packedbitsize=8)) then
  595. begin
  596. { "Untyped" stringconstn is an array of char }
  597. if fromtreetype=stringconstn then
  598. begin
  599. doconv:=tc_string_2_string;
  600. { prefered string type depends on the $H switch }
  601. if (m_default_unicodestring in current_settings.modeswitches) and
  602. (cs_refcountedstrings in current_settings.localswitches) then
  603. case tstringdef(def_to).stringtype of
  604. st_unicodestring: eq:=te_equal;
  605. st_widestring: eq:=te_convert_l1;
  606. // widechar: eq:=te_convert_l2;
  607. // ansichar: eq:=te_convert_l3;
  608. st_ansistring: eq:=te_convert_l4;
  609. st_shortstring: eq:=te_convert_l5;
  610. else
  611. eq:=te_convert_l6;
  612. end
  613. else if not(cs_refcountedstrings in current_settings.localswitches) and
  614. (tstringdef(def_to).stringtype=st_shortstring) then
  615. eq:=te_equal
  616. else if not(m_default_unicodestring in current_settings.modeswitches) and
  617. (cs_refcountedstrings in current_settings.localswitches) and
  618. (tstringdef(def_to).stringtype=st_ansistring) then
  619. eq:=te_equal
  620. else if tstringdef(def_to).stringtype in [st_widestring,st_unicodestring] then
  621. eq:=te_convert_l3
  622. else
  623. eq:=te_convert_l1;
  624. end
  625. else
  626. begin
  627. doconv:=tc_chararray_2_string;
  628. if is_open_array(def_from) then
  629. begin
  630. if is_ansistring(def_to) then
  631. eq:=te_convert_l1
  632. else if is_wide_or_unicode_string(def_to) then
  633. eq:=te_convert_l3
  634. else
  635. eq:=te_convert_l2;
  636. end
  637. else
  638. begin
  639. if is_shortstring(def_to) then
  640. begin
  641. { Only compatible with arrays that fit
  642. smaller than 255 chars }
  643. if (def_from.size <= 255) then
  644. eq:=te_convert_l1;
  645. end
  646. else if is_ansistring(def_to) then
  647. begin
  648. if (def_from.size > 255) then
  649. eq:=te_convert_l1
  650. else
  651. eq:=te_convert_l2;
  652. end
  653. else if is_wide_or_unicode_string(def_to) then
  654. eq:=te_convert_l3
  655. else
  656. eq:=te_convert_l2;
  657. end;
  658. end;
  659. end
  660. else
  661. { array of widechar to string, the length check is done by the firstpass of this node }
  662. if is_widechararray(def_from) or is_open_widechararray(def_from) then
  663. begin
  664. doconv:=tc_chararray_2_string;
  665. if is_wide_or_unicode_string(def_to) then
  666. eq:=te_convert_l1
  667. else
  668. { size of widechar array is double due the sizeof a widechar }
  669. if not(is_shortstring(def_to) and (is_open_widechararray(def_from) or (def_from.size>255*sizeof(widechar)))) then
  670. eq:=te_convert_l3
  671. else
  672. eq:=te_convert_l2;
  673. end;
  674. end;
  675. pointerdef :
  676. begin
  677. { pchar can be assigned to short/ansistrings,
  678. but not in tp7 compatible mode }
  679. if not(m_tp7 in current_settings.modeswitches) then
  680. begin
  681. if is_pchar(def_from) then
  682. begin
  683. doconv:=tc_pchar_2_string;
  684. { prefer ansistrings/unicodestrings because pchars
  685. can overflow shortstrings; don't use l1/l2/l3
  686. because then pchar -> ansistring has the same
  687. preference as conststring -> pchar, and this
  688. breaks webtbs/tw3328.pp }
  689. if is_ansistring(def_to) then
  690. eq:=te_convert_l2
  691. else if is_wide_or_unicode_string(def_to) then
  692. eq:=te_convert_l3
  693. else
  694. eq:=te_convert_l4
  695. end
  696. else if is_pwidechar(def_from) then
  697. begin
  698. doconv:=tc_pwchar_2_string;
  699. if is_wide_or_unicode_string(def_to) then
  700. eq:=te_convert_l1
  701. else
  702. { shortstring and ansistring can both result in
  703. data loss, so don't prefer one over the other }
  704. eq:=te_convert_l3;
  705. end;
  706. end;
  707. end;
  708. objectdef :
  709. begin
  710. { corba interface -> id string }
  711. if is_interfacecorba(def_from) then
  712. begin
  713. doconv:=tc_intf_2_string;
  714. eq:=te_convert_l1;
  715. end
  716. else if (def_from=java_jlstring) then
  717. begin
  718. if is_wide_or_unicode_string(def_to) then
  719. begin
  720. doconv:=tc_equal;
  721. eq:=te_equal;
  722. end
  723. else if def_to.typ=stringdef then
  724. begin
  725. doconv:=tc_string_2_string;
  726. if is_ansistring(def_to) then
  727. eq:=te_convert_l2
  728. else
  729. eq:=te_convert_l3
  730. end;
  731. end;
  732. end;
  733. end;
  734. end;
  735. floatdef :
  736. begin
  737. case def_from.typ of
  738. orddef :
  739. begin { ordinal to real }
  740. { only for implicit and internal typecasts in tp/delphi }
  741. if (([cdo_explicit,cdo_internal] * cdoptions <> [cdo_explicit]) or
  742. ([m_tp7,m_delphi] * current_settings.modeswitches = [])) and
  743. (is_integer(def_from) or
  744. (is_currency(def_from) and
  745. (s64currencytype.typ = floatdef))) then
  746. begin
  747. doconv:=tc_int_2_real;
  748. { prefer single over others }
  749. if is_single(def_to) then
  750. eq:=te_convert_l3
  751. else
  752. eq:=te_convert_l4;
  753. end
  754. else if is_currency(def_from)
  755. { and (s64currencytype.typ = orddef)) } then
  756. begin
  757. { prefer conversion to orddef in this case, unless }
  758. { the orddef < currency (then it will get convert l3, }
  759. { and conversion to float is favoured) }
  760. doconv:=tc_int_2_real;
  761. eq:=te_convert_l2;
  762. end;
  763. end;
  764. floatdef :
  765. begin
  766. if tfloatdef(def_from).floattype=tfloatdef(def_to).floattype then
  767. eq:=te_equal
  768. else
  769. begin
  770. { Delphi does not allow explicit type conversions for float types like:
  771. single_var:=single(double_var);
  772. But if such conversion is inserted by compiler (internal) for some purpose,
  773. it should be allowed even in Delphi mode. }
  774. if (fromtreetype=realconstn) or
  775. not((cdoptions*[cdo_explicit,cdo_internal]=[cdo_explicit]) and
  776. (m_delphi in current_settings.modeswitches)) then
  777. begin
  778. doconv:=tc_real_2_real;
  779. { do we lose precision? }
  780. if (def_to.size<def_from.size) or
  781. (is_currency(def_from) and (tfloatdef(def_to).floattype in [s32real,s64real])) then
  782. eq:=te_convert_l2
  783. else
  784. eq:=te_convert_l1;
  785. end;
  786. end;
  787. end;
  788. end;
  789. end;
  790. enumdef :
  791. begin
  792. case def_from.typ of
  793. enumdef :
  794. begin
  795. if cdo_explicit in cdoptions then
  796. begin
  797. eq:=te_convert_l1;
  798. doconv:=tc_int_2_int;
  799. end
  800. else
  801. begin
  802. hd1:=def_from;
  803. while assigned(tenumdef(hd1).basedef) do
  804. hd1:=tenumdef(hd1).basedef;
  805. hd2:=def_to;
  806. while assigned(tenumdef(hd2).basedef) do
  807. hd2:=tenumdef(hd2).basedef;
  808. if (hd1=hd2) then
  809. begin
  810. eq:=te_convert_l1;
  811. { because of packenum they can have different sizes! (JM) }
  812. doconv:=tc_int_2_int;
  813. end
  814. else
  815. begin
  816. { assignment of an enum symbol to an unique type? }
  817. if (fromtreetype=ordconstn) and
  818. (tenumsym(tenumdef(hd1).getfirstsym)=tenumsym(tenumdef(hd2).getfirstsym)) then
  819. begin
  820. { because of packenum they can have different sizes! (JM) }
  821. eq:=te_convert_l1;
  822. doconv:=tc_int_2_int;
  823. end;
  824. end;
  825. end;
  826. end;
  827. orddef :
  828. begin
  829. if cdo_explicit in cdoptions then
  830. begin
  831. eq:=te_convert_l1;
  832. doconv:=tc_int_2_int;
  833. end;
  834. end;
  835. variantdef :
  836. begin
  837. eq:=te_convert_l1;
  838. doconv:=tc_variant_2_enum;
  839. end;
  840. pointerdef :
  841. begin
  842. { ugly, but delphi allows it }
  843. if cdo_explicit in cdoptions then
  844. begin
  845. if target_info.system in systems_jvm then
  846. begin
  847. doconv:=tc_equal;
  848. eq:=te_convert_l1;
  849. end
  850. else if m_delphi in current_settings.modeswitches then
  851. begin
  852. doconv:=tc_int_2_int;
  853. eq:=te_convert_l1;
  854. end
  855. end;
  856. end;
  857. objectdef:
  858. begin
  859. { ugly, but delphi allows it }
  860. if (cdo_explicit in cdoptions) and
  861. is_class_or_interface_or_dispinterface_or_objc_or_java(def_from) then
  862. begin
  863. { in Java enums /are/ class instances, and hence such
  864. typecasts must not be treated as integer-like
  865. conversions
  866. }
  867. if target_info.system in systems_jvm then
  868. begin
  869. doconv:=tc_equal;
  870. eq:=te_convert_l1;
  871. end
  872. else if m_delphi in current_settings.modeswitches then
  873. begin
  874. doconv:=tc_int_2_int;
  875. eq:=te_convert_l1;
  876. end;
  877. end;
  878. end;
  879. end;
  880. end;
  881. arraydef :
  882. begin
  883. { open array is also compatible with a single element of its base type.
  884. the extra check for deftyp is needed because equal defs can also return
  885. true if the def types are not the same, for example with dynarray to pointer. }
  886. if is_open_array(def_to) and
  887. (def_from.typ=tarraydef(def_to).elementdef.typ) and
  888. equal_defs(def_from,tarraydef(def_to).elementdef) then
  889. begin
  890. doconv:=tc_elem_2_openarray;
  891. { also update in htypechk.pas/var_para_allowed if changed
  892. here }
  893. eq:=te_convert_l3;
  894. end
  895. else
  896. begin
  897. case def_from.typ of
  898. arraydef :
  899. begin
  900. { from/to packed array -- packed chararrays are }
  901. { strings in ISO Pascal (at least if the lower bound }
  902. { is 1, but GPC makes all equal-length chararrays }
  903. { compatible), so treat those the same as regular }
  904. { char arrays -- except if they use subrange types }
  905. if (is_packed_array(def_from) and
  906. (not is_chararray(def_from) or
  907. (tarraydef(def_from).elementdef.packedbitsize<>8)) and
  908. not is_widechararray(def_from)) xor
  909. (is_packed_array(def_to) and
  910. (not is_chararray(def_to) or
  911. (tarraydef(def_to).elementdef.packedbitsize<>8)) and
  912. not is_widechararray(def_to)) then
  913. { both must be packed }
  914. begin
  915. compare_defs_ext:=te_incompatible;
  916. exit;
  917. end
  918. { to dynamic array }
  919. else if is_dynamic_array(def_to) then
  920. begin
  921. if equal_defs(tarraydef(def_from).elementdef,tarraydef(def_to).elementdef) then
  922. begin
  923. { dynamic array -> dynamic array }
  924. if is_dynamic_array(def_from) then
  925. eq:=te_equal
  926. { fpc modes only: array -> dyn. array }
  927. else if (current_settings.modeswitches*[m_objfpc,m_fpc]<>[]) and
  928. not(is_special_array(def_from)) and
  929. is_zero_based_array(def_from) then
  930. begin
  931. eq:=te_convert_l2;
  932. doconv:=tc_array_2_dynarray;
  933. end;
  934. end
  935. end
  936. else
  937. { to open array }
  938. if is_open_array(def_to) then
  939. begin
  940. { array constructor -> open array }
  941. if is_array_constructor(def_from) then
  942. begin
  943. if is_void(tarraydef(def_from).elementdef) then
  944. begin
  945. doconv:=tc_equal;
  946. eq:=te_convert_l1;
  947. end
  948. else
  949. begin
  950. subeq:=compare_defs_ext(tarraydef(def_from).elementdef,
  951. tarraydef(def_to).elementdef,
  952. { reason for cdo_allow_variant: see webtbs/tw7070a and webtbs/tw7070b }
  953. arrayconstructorn,hct,hpd,[cdo_check_operator,cdo_allow_variant]);
  954. if (subeq>=te_equal) then
  955. begin
  956. doconv:=tc_equal;
  957. eq:=te_convert_l1;
  958. end
  959. else
  960. { an array constructor is not an open array, so
  961. use a lower level of compatibility than that one of
  962. of the elements }
  963. if subeq>te_convert_l6 then
  964. begin
  965. doconv:=hct;
  966. eq:=pred(subeq);
  967. end
  968. else
  969. eq:=subeq;
  970. end;
  971. end
  972. else
  973. { dynamic array -> open array }
  974. if is_dynamic_array(def_from) and
  975. equal_defs(tarraydef(def_from).elementdef,tarraydef(def_to).elementdef) then
  976. begin
  977. doconv:=tc_dynarray_2_openarray;
  978. eq:=te_convert_l2;
  979. end
  980. else
  981. { open array -> open array }
  982. if is_open_array(def_from) and
  983. equal_defs(tarraydef(def_from).elementdef,tarraydef(def_to).elementdef) then
  984. if tarraydef(def_from).elementdef=tarraydef(def_to).elementdef then
  985. eq:=te_exact
  986. else
  987. eq:=te_equal
  988. else
  989. { array -> open array }
  990. if not(cdo_parameter in cdoptions) and
  991. equal_defs(tarraydef(def_from).elementdef,tarraydef(def_to).elementdef) then
  992. begin
  993. if fromtreetype=stringconstn then
  994. eq:=te_convert_l1
  995. else
  996. eq:=te_equal;
  997. end;
  998. end
  999. else
  1000. { to array of const }
  1001. if is_array_of_const(def_to) then
  1002. begin
  1003. if is_array_of_const(def_from) or
  1004. is_array_constructor(def_from) then
  1005. begin
  1006. eq:=te_equal;
  1007. end
  1008. else
  1009. { array of tvarrec -> array of const }
  1010. if equal_defs(tarraydef(def_to).elementdef,tarraydef(def_from).elementdef) then
  1011. begin
  1012. doconv:=tc_equal;
  1013. eq:=te_convert_l1;
  1014. end;
  1015. end
  1016. else
  1017. { to array of char, from "Untyped" stringconstn (array of char) }
  1018. if (fromtreetype=stringconstn) and
  1019. ((is_chararray(def_to) and
  1020. { bitpacked arrays of char whose element bitsize is not
  1021. 8 cannot be auto-converted from strings }
  1022. (not is_packed_array(def_to) or
  1023. (tarraydef(def_to).elementdef.packedbitsize=8))) or
  1024. is_widechararray(def_to)) then
  1025. begin
  1026. eq:=te_convert_l1;
  1027. doconv:=tc_string_2_chararray;
  1028. end
  1029. else
  1030. { other arrays }
  1031. begin
  1032. { open array -> array }
  1033. if not(cdo_parameter in cdoptions) and
  1034. is_open_array(def_from) and
  1035. equal_defs(tarraydef(def_from).elementdef,tarraydef(def_to).elementdef) then
  1036. begin
  1037. eq:=te_equal
  1038. end
  1039. else
  1040. { array -> array }
  1041. if not(m_tp7 in current_settings.modeswitches) and
  1042. not(m_delphi in current_settings.modeswitches) and
  1043. (tarraydef(def_from).lowrange=tarraydef(def_to).lowrange) and
  1044. (tarraydef(def_from).highrange=tarraydef(def_to).highrange) and
  1045. equal_defs(tarraydef(def_from).elementdef,tarraydef(def_to).elementdef) and
  1046. equal_defs(tarraydef(def_from).rangedef,tarraydef(def_to).rangedef) then
  1047. begin
  1048. eq:=te_equal
  1049. end;
  1050. end;
  1051. end;
  1052. pointerdef :
  1053. begin
  1054. { nil and voidpointers are compatible with dyn. arrays }
  1055. if is_dynamic_array(def_to) and
  1056. ((fromtreetype=niln) or
  1057. is_voidpointer(def_from)) then
  1058. begin
  1059. doconv:=tc_equal;
  1060. eq:=te_convert_l1;
  1061. end
  1062. else
  1063. if is_zero_based_array(def_to) and
  1064. equal_defs(tpointerdef(def_from).pointeddef,tarraydef(def_to).elementdef) then
  1065. begin
  1066. doconv:=tc_pointer_2_array;
  1067. eq:=te_convert_l1;
  1068. end;
  1069. end;
  1070. stringdef :
  1071. begin
  1072. { string to char array }
  1073. if not is_special_array(def_to) and
  1074. ((is_char(tarraydef(def_to).elementdef) and
  1075. { bitpacked arrays of char whose element bitsize is not
  1076. 8 cannot be auto-converted from strings }
  1077. (not is_packed_array(def_to) or
  1078. (tarraydef(def_to).elementdef.packedbitsize=8))) or
  1079. is_widechar(tarraydef(def_to).elementdef)) then
  1080. begin
  1081. doconv:=tc_string_2_chararray;
  1082. eq:=te_convert_l1;
  1083. end;
  1084. end;
  1085. orddef:
  1086. begin
  1087. if is_chararray(def_to) and
  1088. is_char(def_from) then
  1089. begin
  1090. doconv:=tc_char_2_chararray;
  1091. eq:=te_convert_l2;
  1092. end;
  1093. end;
  1094. recorddef :
  1095. begin
  1096. { tvarrec -> array of const }
  1097. if is_array_of_const(def_to) and
  1098. equal_defs(def_from,tarraydef(def_to).elementdef) then
  1099. begin
  1100. doconv:=tc_equal;
  1101. eq:=te_convert_l1;
  1102. end;
  1103. end;
  1104. variantdef :
  1105. begin
  1106. if is_dynamic_array(def_to) then
  1107. begin
  1108. doconv:=tc_variant_2_dynarray;
  1109. eq:=te_convert_l1;
  1110. end;
  1111. end;
  1112. end;
  1113. end;
  1114. end;
  1115. variantdef :
  1116. begin
  1117. if (cdo_allow_variant in cdoptions) then
  1118. begin
  1119. case def_from.typ of
  1120. enumdef :
  1121. begin
  1122. doconv:=tc_enum_2_variant;
  1123. eq:=te_convert_l1;
  1124. end;
  1125. arraydef :
  1126. begin
  1127. if is_dynamic_array(def_from) then
  1128. begin
  1129. doconv:=tc_dynarray_2_variant;
  1130. eq:=te_convert_l1;
  1131. end;
  1132. end;
  1133. objectdef :
  1134. begin
  1135. { corbainterfaces not accepted, until we have
  1136. runtime support for them in Variants (sergei) }
  1137. if is_interfacecom_or_dispinterface(def_from) then
  1138. begin
  1139. doconv:=tc_interface_2_variant;
  1140. eq:=te_convert_l1;
  1141. end;
  1142. end;
  1143. variantdef :
  1144. begin
  1145. { doing this in the compiler avoids a lot of unncessary
  1146. copying }
  1147. if (tvariantdef(def_from).varianttype=vt_olevariant) and
  1148. (tvariantdef(def_to).varianttype=vt_normalvariant) then
  1149. begin
  1150. doconv:=tc_equal;
  1151. eq:=te_convert_l1;
  1152. end;
  1153. end;
  1154. end;
  1155. end;
  1156. end;
  1157. pointerdef :
  1158. begin
  1159. case def_from.typ of
  1160. stringdef :
  1161. begin
  1162. { string constant (which can be part of array constructor)
  1163. to zero terminated string constant }
  1164. if (fromtreetype = stringconstn) and
  1165. (is_pchar(def_to) or is_pwidechar(def_to)) then
  1166. begin
  1167. doconv:=tc_cstring_2_pchar;
  1168. if is_pwidechar(def_to)=(m_default_unicodestring in current_settings.modeswitches) then
  1169. eq:=te_convert_l2
  1170. else
  1171. eq:=te_convert_l3
  1172. end
  1173. else
  1174. if (cdo_explicit in cdoptions) or (fromtreetype = arrayconstructorn) then
  1175. begin
  1176. { pchar(ansistring) }
  1177. if is_pchar(def_to) and
  1178. is_ansistring(def_from) then
  1179. begin
  1180. doconv:=tc_ansistring_2_pchar;
  1181. eq:=te_convert_l1;
  1182. end
  1183. else
  1184. { pwidechar(widestring) }
  1185. if is_pwidechar(def_to) and
  1186. is_wide_or_unicode_string(def_from) then
  1187. begin
  1188. doconv:=tc_ansistring_2_pchar;
  1189. eq:=te_convert_l1;
  1190. end;
  1191. end;
  1192. end;
  1193. orddef :
  1194. begin
  1195. { char constant to zero terminated string constant }
  1196. if (fromtreetype in [ordconstn,arrayconstructorn]) then
  1197. begin
  1198. if (is_char(def_from) or is_widechar(def_from)) and
  1199. (is_pchar(def_to) or is_pwidechar(def_to)) then
  1200. begin
  1201. doconv:=tc_cchar_2_pchar;
  1202. if is_pwidechar(def_to)=(m_default_unicodestring in current_settings.modeswitches) then
  1203. eq:=te_convert_l1
  1204. else
  1205. eq:=te_convert_l2
  1206. end
  1207. else
  1208. if (m_delphi in current_settings.modeswitches) and is_integer(def_from) then
  1209. begin
  1210. doconv:=tc_cord_2_pointer;
  1211. eq:=te_convert_l5;
  1212. end;
  1213. end;
  1214. { allow explicit typecasts from ordinals to pointer.
  1215. Support for delphi compatibility
  1216. Support constructs like pointer(cardinal-cardinal) or pointer(longint+cardinal) where
  1217. the result of the ordinal operation is int64 also on 32 bit platforms.
  1218. It is also used by the compiler internally for inc(pointer,ordinal) }
  1219. if (eq=te_incompatible) and
  1220. not is_void(def_from) and
  1221. (
  1222. (
  1223. (cdo_explicit in cdoptions) and
  1224. (
  1225. (m_delphi in current_settings.modeswitches) or
  1226. { Don't allow pchar(char) in fpc modes }
  1227. is_integer(def_from)
  1228. )
  1229. ) or
  1230. (cdo_internal in cdoptions)
  1231. ) then
  1232. begin
  1233. doconv:=tc_int_2_int;
  1234. eq:=te_convert_l1;
  1235. end;
  1236. end;
  1237. enumdef :
  1238. begin
  1239. { allow explicit typecasts from enums to pointer.
  1240. Support for delphi compatibility
  1241. }
  1242. { in Java enums /are/ class instances, and hence such
  1243. typecasts must not be treated as integer-like conversions
  1244. }
  1245. if (((cdo_explicit in cdoptions) and
  1246. ((m_delphi in current_settings.modeswitches) or
  1247. (target_info.system in systems_jvm)
  1248. )
  1249. ) or
  1250. (cdo_internal in cdoptions)
  1251. ) then
  1252. begin
  1253. { in Java enums /are/ class instances, and hence such
  1254. typecasts must not be treated as integer-like
  1255. conversions
  1256. }
  1257. if target_info.system in systems_jvm then
  1258. begin
  1259. doconv:=tc_equal;
  1260. eq:=te_convert_l1;
  1261. end
  1262. else if m_delphi in current_settings.modeswitches then
  1263. begin
  1264. doconv:=tc_int_2_int;
  1265. eq:=te_convert_l1;
  1266. end;
  1267. end;
  1268. end;
  1269. arraydef :
  1270. begin
  1271. { string constant (which can be part of array constructor)
  1272. to zero terminated string constant }
  1273. if (((fromtreetype = arrayconstructorn) and
  1274. { can't use is_chararray, because returns false for }
  1275. { array constructors }
  1276. is_char(tarraydef(def_from).elementdef)) or
  1277. (fromtreetype = stringconstn)) and
  1278. (is_pchar(def_to) or is_pwidechar(def_to)) then
  1279. begin
  1280. doconv:=tc_cstring_2_pchar;
  1281. if ((m_default_unicodestring in current_settings.modeswitches) xor
  1282. is_pchar(def_to)) then
  1283. eq:=te_convert_l2
  1284. else
  1285. eq:=te_convert_l3;
  1286. end
  1287. else
  1288. { chararray to pointer }
  1289. if (is_zero_based_array(def_from) or
  1290. is_open_array(def_from)) and
  1291. equal_defs(tarraydef(def_from).elementdef,tpointerdef(def_to).pointeddef) then
  1292. begin
  1293. doconv:=tc_array_2_pointer;
  1294. { don't prefer the pchar overload when a constant
  1295. string was passed }
  1296. if fromtreetype=stringconstn then
  1297. eq:=te_convert_l2
  1298. else
  1299. eq:=te_convert_l1;
  1300. end
  1301. else
  1302. { dynamic array to pointer, delphi only }
  1303. if (m_delphi in current_settings.modeswitches) and
  1304. is_dynamic_array(def_from) and
  1305. is_voidpointer(def_to) then
  1306. begin
  1307. eq:=te_equal;
  1308. end;
  1309. end;
  1310. pointerdef :
  1311. begin
  1312. { check for far pointers }
  1313. if not tpointerdef(def_from).compatible_with_pointerdef_size(tpointerdef(def_to)) then
  1314. begin
  1315. if fromtreetype=niln then
  1316. eq:=te_equal
  1317. else
  1318. eq:=te_incompatible;
  1319. end
  1320. { the types can be forward type, handle before normal type check !! }
  1321. else
  1322. if assigned(def_to.typesym) and
  1323. ((tpointerdef(def_to).pointeddef.typ=forwarddef) or
  1324. (tpointerdef(def_from).pointeddef.typ=forwarddef)) then
  1325. begin
  1326. if (def_from.typesym=def_to.typesym) or
  1327. (fromtreetype=niln) then
  1328. eq:=te_equal
  1329. end
  1330. else
  1331. { same types }
  1332. if equal_defs(tpointerdef(def_from).pointeddef,tpointerdef(def_to).pointeddef) then
  1333. begin
  1334. eq:=te_equal
  1335. end
  1336. else
  1337. { child class pointer can be assigned to anchestor pointers }
  1338. if (
  1339. (tpointerdef(def_from).pointeddef.typ=objectdef) and
  1340. (tpointerdef(def_to).pointeddef.typ=objectdef) and
  1341. def_is_related(tobjectdef(tpointerdef(def_from).pointeddef),
  1342. tobjectdef(tpointerdef(def_to).pointeddef))
  1343. ) then
  1344. begin
  1345. doconv:=tc_equal;
  1346. eq:=te_convert_l1;
  1347. end
  1348. else
  1349. { all pointers can be assigned to void-pointer }
  1350. if is_void(tpointerdef(def_to).pointeddef) then
  1351. begin
  1352. doconv:=tc_equal;
  1353. { give pwidechar,pchar a penalty so it prefers
  1354. conversion to ansistring }
  1355. if is_pchar(def_from) or
  1356. is_pwidechar(def_from) then
  1357. eq:=te_convert_l2
  1358. else
  1359. eq:=te_convert_l1;
  1360. end
  1361. else
  1362. { all pointers can be assigned from void-pointer }
  1363. if is_void(tpointerdef(def_from).pointeddef) or
  1364. { all pointers can be assigned from void-pointer or formaldef pointer, check
  1365. tw3777.pp if you change this }
  1366. (tpointerdef(def_from).pointeddef.typ=formaldef) then
  1367. begin
  1368. doconv:=tc_equal;
  1369. { give pwidechar a penalty so it prefers
  1370. conversion to pchar }
  1371. if is_pwidechar(def_to) then
  1372. eq:=te_convert_l2
  1373. else
  1374. eq:=te_convert_l1;
  1375. end
  1376. { id = generic class instance. metaclasses are also
  1377. class instances themselves. }
  1378. else if ((def_from=objc_idtype) and
  1379. (def_to=objc_metaclasstype)) or
  1380. ((def_to=objc_idtype) and
  1381. (def_from=objc_metaclasstype)) then
  1382. begin
  1383. doconv:=tc_equal;
  1384. eq:=te_convert_l2;
  1385. end;
  1386. end;
  1387. procvardef :
  1388. begin
  1389. { procedure variable can be assigned to an void pointer,
  1390. this is not allowed for complex procvars }
  1391. if (is_void(tpointerdef(def_to).pointeddef) or
  1392. (m_mac_procvar in current_settings.modeswitches)) and
  1393. tprocvardef(def_from).compatible_with_pointerdef_size(tpointerdef(def_to)) then
  1394. begin
  1395. doconv:=tc_equal;
  1396. eq:=te_convert_l1;
  1397. end;
  1398. end;
  1399. procdef :
  1400. begin
  1401. { procedure variable can be assigned to an void pointer,
  1402. this not allowed for methodpointers }
  1403. if (m_mac_procvar in current_settings.modeswitches) and
  1404. tprocdef(def_from).compatible_with_pointerdef_size(tpointerdef(def_to)) then
  1405. begin
  1406. doconv:=tc_proc_2_procvar;
  1407. eq:=te_convert_l2;
  1408. end;
  1409. end;
  1410. classrefdef,
  1411. objectdef :
  1412. begin
  1413. { implicit pointer object and class reference types
  1414. can be assigned to void pointers, but it is less
  1415. preferred than assigning to a related objectdef }
  1416. if (
  1417. is_implicit_pointer_object_type(def_from) or
  1418. (def_from.typ=classrefdef)
  1419. ) and
  1420. (tpointerdef(def_to).pointeddef.typ=orddef) and
  1421. (torddef(tpointerdef(def_to).pointeddef).ordtype=uvoid) then
  1422. begin
  1423. doconv:=tc_equal;
  1424. eq:=te_convert_l2;
  1425. end
  1426. else if (is_objc_class_or_protocol(def_from) and
  1427. (def_to=objc_idtype)) or
  1428. { classrefs are also instances in Objective-C,
  1429. hence they're also assignment-cpmpatible with
  1430. id }
  1431. (is_objcclassref(def_from) and
  1432. ((def_to=objc_metaclasstype) or
  1433. (def_to=objc_idtype))) then
  1434. begin
  1435. doconv:=tc_equal;
  1436. eq:=te_convert_l2;
  1437. end;
  1438. end;
  1439. end;
  1440. end;
  1441. setdef :
  1442. begin
  1443. case def_from.typ of
  1444. setdef :
  1445. begin
  1446. if assigned(tsetdef(def_from).elementdef) and
  1447. assigned(tsetdef(def_to).elementdef) then
  1448. begin
  1449. { sets with the same size (packset setting), element
  1450. base type and the same range are equal }
  1451. if equal_defs(tsetdef(def_from).elementdef,tsetdef(def_to).elementdef) and
  1452. (tsetdef(def_from).setbase=tsetdef(def_to).setbase) and
  1453. (tsetdef(def_from).setmax=tsetdef(def_to).setmax) and
  1454. (def_from.size=def_to.size) then
  1455. eq:=te_equal
  1456. else if is_subequal(tsetdef(def_from).elementdef,tsetdef(def_to).elementdef) then
  1457. begin
  1458. eq:=te_convert_l1;
  1459. doconv:=tc_set_to_set;
  1460. end;
  1461. end
  1462. else
  1463. begin
  1464. { empty set is compatible with everything }
  1465. eq:=te_convert_l1;
  1466. doconv:=tc_set_to_set;
  1467. end;
  1468. end;
  1469. arraydef :
  1470. begin
  1471. { automatic arrayconstructor -> set conversion }
  1472. if is_array_constructor(def_from) then
  1473. begin
  1474. doconv:=tc_arrayconstructor_2_set;
  1475. eq:=te_convert_l1;
  1476. end;
  1477. end;
  1478. end;
  1479. end;
  1480. procvardef :
  1481. begin
  1482. case def_from.typ of
  1483. procdef :
  1484. begin
  1485. { proc -> procvar }
  1486. if (m_tp_procvar in current_settings.modeswitches) or
  1487. (m_mac_procvar in current_settings.modeswitches) then
  1488. begin
  1489. subeq:=proc_to_procvar_equal(tprocdef(def_from),tprocvardef(def_to),cdo_warn_incompatible_univ in cdoptions);
  1490. if subeq>te_incompatible then
  1491. begin
  1492. doconv:=tc_proc_2_procvar;
  1493. if subeq>te_convert_l5 then
  1494. eq:=pred(subeq)
  1495. else
  1496. eq:=subeq;
  1497. end;
  1498. end;
  1499. end;
  1500. procvardef :
  1501. begin
  1502. { procvar -> procvar }
  1503. eq:=proc_to_procvar_equal(tprocvardef(def_from),tprocvardef(def_to),cdo_warn_incompatible_univ in cdoptions);
  1504. if eq<te_equal then
  1505. doconv:=tc_proc_2_procvar
  1506. else
  1507. doconv:=tc_equal;
  1508. end;
  1509. pointerdef :
  1510. begin
  1511. { nil is compatible with procvars }
  1512. if (fromtreetype=niln) then
  1513. begin
  1514. if not Tprocvardef(def_to).is_addressonly then
  1515. {Nil to method pointers requires to convert a single
  1516. pointer nil value to a two pointer procvardef.}
  1517. doconv:=tc_nil_2_methodprocvar
  1518. else
  1519. doconv:=tc_equal;
  1520. eq:=te_convert_l1;
  1521. end
  1522. else
  1523. { for example delphi allows the assignement from pointers }
  1524. { to procedure variables }
  1525. if (m_pointer_2_procedure in current_settings.modeswitches) and
  1526. is_void(tpointerdef(def_from).pointeddef) and
  1527. tprocvardef(def_to).is_addressonly then
  1528. begin
  1529. doconv:=tc_equal;
  1530. eq:=te_convert_l1;
  1531. end;
  1532. end;
  1533. end;
  1534. end;
  1535. objectdef :
  1536. begin
  1537. { object pascal objects }
  1538. if (def_from.typ=objectdef) and
  1539. (def_is_related(tobjectdef(def_from),tobjectdef(def_to))) then
  1540. begin
  1541. doconv:=tc_equal;
  1542. { also update in htypechk.pas/var_para_allowed if changed
  1543. here }
  1544. eq:=te_convert_l3;
  1545. end
  1546. { string -> java.lang.string }
  1547. else if (def_to=java_jlstring) and
  1548. ((def_from.typ=stringdef) or
  1549. (fromtreetype=stringconstn)) then
  1550. begin
  1551. if is_wide_or_unicode_string(def_from) or
  1552. ((fromtreetype=stringconstn) and
  1553. (cs_refcountedstrings in current_settings.localswitches) and
  1554. (m_default_unicodestring in current_settings.modeswitches)) then
  1555. begin
  1556. doconv:=tc_equal;
  1557. eq:=te_equal
  1558. end
  1559. else
  1560. begin
  1561. doconv:=tc_string_2_string;
  1562. eq:=te_convert_l2;
  1563. end;
  1564. end
  1565. else if (def_to=java_jlstring) and
  1566. is_anychar(def_from) then
  1567. begin
  1568. doconv:=tc_char_2_string;
  1569. eq:=te_convert_l2
  1570. end
  1571. else
  1572. { specific to implicit pointer object types }
  1573. if is_implicit_pointer_object_type(def_to) then
  1574. begin
  1575. { void pointer also for delphi mode }
  1576. if (m_delphi in current_settings.modeswitches) and
  1577. is_voidpointer(def_from) then
  1578. begin
  1579. doconv:=tc_equal;
  1580. { prefer pointer-pointer assignments }
  1581. eq:=te_convert_l2;
  1582. end
  1583. else
  1584. { nil is compatible with class instances and interfaces }
  1585. if (fromtreetype=niln) then
  1586. begin
  1587. doconv:=tc_equal;
  1588. eq:=te_convert_l1;
  1589. end
  1590. { All Objective-C classes are compatible with ID }
  1591. else if is_objc_class_or_protocol(def_to) and
  1592. (def_from=objc_idtype) then
  1593. begin
  1594. doconv:=tc_equal;
  1595. eq:=te_convert_l2;
  1596. end
  1597. { classes can be assigned to interfaces
  1598. (same with objcclass and objcprotocol) }
  1599. else if ((is_interface(def_to) and
  1600. is_class(def_from)) or
  1601. (is_objcprotocol(def_to) and
  1602. is_objcclass(def_from)) or
  1603. (is_javainterface(def_to) and
  1604. is_javaclass(def_from))) and
  1605. assigned(tobjectdef(def_from).ImplementedInterfaces) then
  1606. begin
  1607. { we've to search in parent classes as well }
  1608. hobjdef:=tobjectdef(def_from);
  1609. while assigned(hobjdef) do
  1610. begin
  1611. if find_implemented_interface(hobjdef,tobjectdef(def_to))<>nil then
  1612. begin
  1613. if is_interface(def_to) then
  1614. doconv:=tc_class_2_intf
  1615. else
  1616. { for Objective-C, we don't have to do anything special }
  1617. doconv:=tc_equal;
  1618. { don't prefer this over objectdef->objectdef }
  1619. eq:=te_convert_l2;
  1620. break;
  1621. end;
  1622. hobjdef:=hobjdef.childof;
  1623. end;
  1624. end
  1625. { Interface 2 GUID handling }
  1626. else if (def_to=tdef(rec_tguid)) and
  1627. (fromtreetype=typen) and
  1628. is_interface(def_from) and
  1629. assigned(tobjectdef(def_from).iidguid) then
  1630. begin
  1631. eq:=te_convert_l1;
  1632. doconv:=tc_equal;
  1633. end
  1634. else if (def_from.typ=variantdef) and is_interfacecom_or_dispinterface(def_to) then
  1635. begin
  1636. { corbainterfaces not accepted, until we have
  1637. runtime support for them in Variants (sergei) }
  1638. doconv:=tc_variant_2_interface;
  1639. eq:=te_convert_l2;
  1640. end
  1641. { ugly, but delphi allows it (enables typecasting ordinals/
  1642. enums of any size to pointer-based object defs) }
  1643. { in Java enums /are/ class instances, and hence such
  1644. typecasts must not be treated as integer-like conversions;
  1645. arbitrary constants cannot be converted into classes/
  1646. pointer-based values either on the JVM -> always return
  1647. false and let it be handled by the regular explicit type
  1648. casting code
  1649. }
  1650. else if (not(target_info.system in systems_jvm) and
  1651. ((def_from.typ=enumdef) or
  1652. (def_from.typ=orddef))) and
  1653. (m_delphi in current_settings.modeswitches) and
  1654. (cdo_explicit in cdoptions) then
  1655. begin
  1656. doconv:=tc_int_2_int;
  1657. eq:=te_convert_l1;
  1658. end;
  1659. end;
  1660. end;
  1661. classrefdef :
  1662. begin
  1663. { similar to pointerdef wrt forwards }
  1664. if assigned(def_to.typesym) and
  1665. (tclassrefdef(def_to).pointeddef.typ=forwarddef) or
  1666. ((def_from.typ=classrefdef) and
  1667. (tclassrefdef(def_from).pointeddef.typ=forwarddef)) then
  1668. begin
  1669. if (def_from.typesym=def_to.typesym) or
  1670. (fromtreetype=niln) then
  1671. eq:=te_equal;
  1672. end
  1673. else
  1674. { class reference types }
  1675. if (def_from.typ=classrefdef) then
  1676. begin
  1677. if equal_defs(tclassrefdef(def_from).pointeddef,tclassrefdef(def_to).pointeddef) then
  1678. begin
  1679. eq:=te_equal;
  1680. end
  1681. else
  1682. begin
  1683. doconv:=tc_equal;
  1684. if (cdo_explicit in cdoptions) or
  1685. def_is_related(tobjectdef(tclassrefdef(def_from).pointeddef),
  1686. tobjectdef(tclassrefdef(def_to).pointeddef)) then
  1687. eq:=te_convert_l1;
  1688. end;
  1689. end
  1690. else
  1691. if (m_delphi in current_settings.modeswitches) and
  1692. is_voidpointer(def_from) then
  1693. begin
  1694. doconv:=tc_equal;
  1695. { prefer pointer-pointer assignments }
  1696. eq:=te_convert_l2;
  1697. end
  1698. else
  1699. { nil is compatible with class references }
  1700. if (fromtreetype=niln) then
  1701. begin
  1702. doconv:=tc_equal;
  1703. eq:=te_convert_l1;
  1704. end
  1705. else
  1706. { id is compatible with all classref types }
  1707. if (def_from=objc_idtype) then
  1708. begin
  1709. doconv:=tc_equal;
  1710. eq:=te_convert_l1;
  1711. end;
  1712. end;
  1713. filedef :
  1714. begin
  1715. { typed files are all equal to the abstract file type
  1716. name TYPEDFILE in system.pp in is_equal in types.pas
  1717. the problem is that it sholud be also compatible to FILE
  1718. but this would leed to a problem for ASSIGN RESET and REWRITE
  1719. when trying to find the good overloaded function !!
  1720. so all file function are doubled in system.pp
  1721. this is not very beautiful !!}
  1722. if (def_from.typ=filedef) then
  1723. begin
  1724. if (tfiledef(def_from).filetyp=tfiledef(def_to).filetyp) then
  1725. begin
  1726. if
  1727. (
  1728. (tfiledef(def_from).typedfiledef=nil) and
  1729. (tfiledef(def_to).typedfiledef=nil)
  1730. ) or
  1731. (
  1732. (tfiledef(def_from).typedfiledef<>nil) and
  1733. (tfiledef(def_to).typedfiledef<>nil) and
  1734. equal_defs(tfiledef(def_from).typedfiledef,tfiledef(def_to).typedfiledef)
  1735. ) or
  1736. (
  1737. (tfiledef(def_from).filetyp = ft_typed) and
  1738. (tfiledef(def_to).filetyp = ft_typed) and
  1739. (
  1740. (tfiledef(def_from).typedfiledef = tdef(voidtype)) or
  1741. (tfiledef(def_to).typedfiledef = tdef(voidtype))
  1742. )
  1743. ) then
  1744. begin
  1745. eq:=te_equal;
  1746. end;
  1747. end
  1748. else
  1749. if ((tfiledef(def_from).filetyp = ft_untyped) and
  1750. (tfiledef(def_to).filetyp = ft_typed)) or
  1751. ((tfiledef(def_from).filetyp = ft_typed) and
  1752. (tfiledef(def_to).filetyp = ft_untyped)) then
  1753. begin
  1754. doconv:=tc_equal;
  1755. eq:=te_convert_l1;
  1756. end;
  1757. end;
  1758. end;
  1759. recorddef :
  1760. begin
  1761. { interface -> guid }
  1762. if (def_to=rec_tguid) and
  1763. (is_interfacecom_or_dispinterface(def_from)) then
  1764. begin
  1765. doconv:=tc_intf_2_guid;
  1766. eq:=te_convert_l1;
  1767. end;
  1768. end;
  1769. formaldef :
  1770. begin
  1771. doconv:=tc_equal;
  1772. if (def_from.typ=formaldef) then
  1773. eq:=te_equal
  1774. else
  1775. { Just about everything can be converted to a formaldef...}
  1776. if not (def_from.typ in [abstractdef,errordef]) then
  1777. eq:=te_convert_l2;
  1778. end;
  1779. end;
  1780. { if we didn't find an appropriate type conversion yet
  1781. then we search also the := operator }
  1782. if (eq=te_incompatible) and
  1783. { make sure there is not a single variant if variants }
  1784. { are not allowed (otherwise if only cdo_check_operator }
  1785. { and e.g. fromdef=stringdef and todef=variantdef, then }
  1786. { the test will still succeed }
  1787. ((cdo_allow_variant in cdoptions) or
  1788. ((def_from.typ<>variantdef) and
  1789. (def_to.typ<>variantdef) and
  1790. { internal typeconversions always have to be bitcasts (except for
  1791. variants) }
  1792. not(cdo_internal in cdoptions)
  1793. )
  1794. ) and
  1795. (
  1796. { Check for variants? }
  1797. (
  1798. (cdo_allow_variant in cdoptions) and
  1799. ((def_from.typ=variantdef) or (def_to.typ=variantdef))
  1800. ) or
  1801. { Check for operators? }
  1802. (
  1803. (cdo_check_operator in cdoptions) and
  1804. ((def_from.typ<>variantdef) or (def_to.typ<>variantdef))
  1805. )
  1806. ) then
  1807. begin
  1808. operatorpd:=search_assignment_operator(def_from,def_to,cdo_explicit in cdoptions);
  1809. if assigned(operatorpd) then
  1810. eq:=te_convert_operator;
  1811. end;
  1812. { update convtype for te_equal when it is not yet set }
  1813. if (eq=te_equal) and
  1814. (doconv=tc_not_possible) then
  1815. doconv:=tc_equal;
  1816. compare_defs_ext:=eq;
  1817. end;
  1818. function equal_defs(def_from,def_to:tdef):boolean;
  1819. var
  1820. convtyp : tconverttype;
  1821. pd : tprocdef;
  1822. begin
  1823. { Compare defs with nothingn and no explicit typecasts and
  1824. searching for overloaded operators is not needed }
  1825. equal_defs:=(compare_defs_ext(def_from,def_to,nothingn,convtyp,pd,[])>=te_equal);
  1826. end;
  1827. function compare_defs(def_from,def_to:tdef;fromtreetype:tnodetype):tequaltype;
  1828. var
  1829. doconv : tconverttype;
  1830. pd : tprocdef;
  1831. begin
  1832. compare_defs:=compare_defs_ext(def_from,def_to,fromtreetype,doconv,pd,[cdo_check_operator,cdo_allow_variant]);
  1833. end;
  1834. function is_subequal(def1, def2: tdef): boolean;
  1835. var
  1836. basedef1,basedef2 : tenumdef;
  1837. Begin
  1838. is_subequal := false;
  1839. if assigned(def1) and assigned(def2) then
  1840. Begin
  1841. if (def1.typ = orddef) and (def2.typ = orddef) then
  1842. Begin
  1843. { see p.47 of Turbo Pascal 7.01 manual for the separation of types }
  1844. { range checking for case statements is done with testrange }
  1845. case torddef(def1).ordtype of
  1846. u8bit,u16bit,u32bit,u64bit,
  1847. s8bit,s16bit,s32bit,s64bit :
  1848. is_subequal:=(torddef(def2).ordtype in [s64bit,u64bit,s32bit,u32bit,u8bit,s8bit,s16bit,u16bit]);
  1849. pasbool8,pasbool16,pasbool32,pasbool64,
  1850. bool8bit,bool16bit,bool32bit,bool64bit :
  1851. is_subequal:=(torddef(def2).ordtype in [pasbool8,pasbool16,pasbool32,pasbool64,bool8bit,bool16bit,bool32bit,bool64bit]);
  1852. uchar :
  1853. is_subequal:=(torddef(def2).ordtype=uchar);
  1854. uwidechar :
  1855. is_subequal:=(torddef(def2).ordtype=uwidechar);
  1856. end;
  1857. end
  1858. else
  1859. Begin
  1860. { Check if both basedefs are equal }
  1861. if (def1.typ=enumdef) and (def2.typ=enumdef) then
  1862. Begin
  1863. { get both basedefs }
  1864. basedef1:=tenumdef(def1);
  1865. while assigned(basedef1.basedef) do
  1866. basedef1:=basedef1.basedef;
  1867. basedef2:=tenumdef(def2);
  1868. while assigned(basedef2.basedef) do
  1869. basedef2:=basedef2.basedef;
  1870. is_subequal:=(basedef1=basedef2);
  1871. end;
  1872. end;
  1873. end;
  1874. end;
  1875. function potentially_incompatible_univ_paras(def1, def2: tdef): boolean;
  1876. begin
  1877. result :=
  1878. { not entirely safe: different records can be passed differently
  1879. depending on the types of their fields, but they're hard to compare
  1880. (variant records, bitpacked vs non-bitpacked) }
  1881. ((def1.typ in [floatdef,recorddef,arraydef,filedef,variantdef]) and
  1882. (def1.typ<>def2.typ)) or
  1883. { pointers, ordinals and small sets are all passed the same}
  1884. (((def1.typ in [orddef,enumdef,pointerdef,procvardef,classrefdef]) or
  1885. (is_class_or_interface_or_objc(def1)) or
  1886. is_dynamic_array(def1) or
  1887. is_smallset(def1) or
  1888. is_ansistring(def1) or
  1889. is_unicodestring(def1)) <>
  1890. (def2.typ in [orddef,enumdef,pointerdef,procvardef,classrefdef]) or
  1891. (is_class_or_interface_or_objc(def2)) or
  1892. is_dynamic_array(def2) or
  1893. is_smallset(def2) or
  1894. is_ansistring(def2) or
  1895. is_unicodestring(def2)) or
  1896. { shortstrings }
  1897. (is_shortstring(def1)<>
  1898. is_shortstring(def2)) or
  1899. { winlike widestrings }
  1900. (is_widestring(def1)<>
  1901. is_widestring(def2)) or
  1902. { TP-style objects }
  1903. (is_object(def1) <>
  1904. is_object(def2));
  1905. end;
  1906. function compare_paras(para1,para2 : TFPObjectList; acp : tcompare_paras_type; cpoptions: tcompare_paras_options):tequaltype;
  1907. var
  1908. currpara1,
  1909. currpara2 : tparavarsym;
  1910. function equal_genfunc_paradefs(def1,def2:tdef):boolean;
  1911. begin
  1912. result:=false;
  1913. if (sp_generic_para in def1.typesym.symoptions) and
  1914. (sp_generic_para in def2.typesym.symoptions) and
  1915. (def1.owner=currpara1.owner) and
  1916. (def2.owner=currpara2.owner) then
  1917. begin
  1918. { the forward declaration may have constraints }
  1919. if not (df_genconstraint in def2.defoptions) and (def2.typ=undefineddef) and
  1920. ((def1.typ=undefineddef) or (df_genconstraint in def1.defoptions)) then
  1921. result:=true;
  1922. end
  1923. end;
  1924. var
  1925. eq,lowesteq : tequaltype;
  1926. hpd : tprocdef;
  1927. convtype : tconverttype;
  1928. cdoptions : tcompare_defs_options;
  1929. i1,i2 : byte;
  1930. begin
  1931. compare_paras:=te_incompatible;
  1932. cdoptions:=[cdo_parameter,cdo_check_operator,cdo_allow_variant,cdo_strict_undefined_check];
  1933. { we need to parse the list from left-right so the
  1934. not-default parameters are checked first }
  1935. lowesteq:=high(tequaltype);
  1936. i1:=0;
  1937. i2:=0;
  1938. if cpo_ignorehidden in cpoptions then
  1939. begin
  1940. while (i1<para1.count) and
  1941. (vo_is_hidden_para in tparavarsym(para1[i1]).varoptions) do
  1942. inc(i1);
  1943. while (i2<para2.count) and
  1944. (vo_is_hidden_para in tparavarsym(para2[i2]).varoptions) do
  1945. inc(i2);
  1946. end;
  1947. if cpo_ignoreframepointer in cpoptions then
  1948. begin
  1949. if (i1<para1.count) and
  1950. (vo_is_parentfp in tparavarsym(para1[i1]).varoptions) then
  1951. inc(i1);
  1952. if (i2<para2.count) and
  1953. (vo_is_parentfp in tparavarsym(para2[i2]).varoptions) then
  1954. inc(i2);
  1955. end;
  1956. while (i1<para1.count) and (i2<para2.count) do
  1957. begin
  1958. eq:=te_incompatible;
  1959. currpara1:=tparavarsym(para1[i1]);
  1960. currpara2:=tparavarsym(para2[i2]);
  1961. { Unique types must match exact }
  1962. if ((df_unique in currpara1.vardef.defoptions) or (df_unique in currpara2.vardef.defoptions)) and
  1963. (currpara1.vardef<>currpara2.vardef) then
  1964. exit;
  1965. { Handle hidden parameters separately, because self is
  1966. defined as voidpointer for methodpointers }
  1967. if (vo_is_hidden_para in currpara1.varoptions) or
  1968. (vo_is_hidden_para in currpara2.varoptions) then
  1969. begin
  1970. { both must be hidden }
  1971. if (vo_is_hidden_para in currpara1.varoptions)<>(vo_is_hidden_para in currpara2.varoptions) then
  1972. exit;
  1973. eq:=te_exact;
  1974. if (([vo_is_self,vo_is_vmt]*currpara1.varoptions)=[]) and
  1975. (([vo_is_self,vo_is_vmt]*currpara2.varoptions)=[]) then
  1976. begin
  1977. if not(cpo_ignorevarspez in cpoptions) and
  1978. (currpara1.varspez<>currpara2.varspez) then
  1979. exit;
  1980. eq:=compare_defs_ext(currpara1.vardef,currpara2.vardef,nothingn,
  1981. convtype,hpd,cdoptions);
  1982. end
  1983. else if ([vo_is_self,vo_is_vmt]*currpara1.varoptions)<>
  1984. ([vo_is_self,vo_is_vmt]*currpara2.varoptions) then
  1985. eq:=te_incompatible;
  1986. end
  1987. else
  1988. begin
  1989. case acp of
  1990. cp_value_equal_const :
  1991. begin
  1992. { this one is used for matching parameters from a call
  1993. statement to a procdef -> univ state can't be equal
  1994. in any case since the call statement does not contain
  1995. any information about that }
  1996. if (
  1997. not(cpo_ignorevarspez in cpoptions) and
  1998. (currpara1.varspez<>currpara2.varspez) and
  1999. ((currpara1.varspez in [vs_var,vs_out,vs_constref]) or
  2000. (currpara2.varspez in [vs_var,vs_out,vs_constref]))
  2001. ) then
  2002. exit;
  2003. eq:=compare_defs_ext(currpara1.vardef,currpara2.vardef,nothingn,
  2004. convtype,hpd,cdoptions);
  2005. end;
  2006. cp_all :
  2007. begin
  2008. { used to resolve forward definitions -> headers must
  2009. match exactly, including the "univ" specifier }
  2010. if (not(cpo_ignorevarspez in cpoptions) and
  2011. (currpara1.varspez<>currpara2.varspez)) or
  2012. (currpara1.univpara<>currpara2.univpara) then
  2013. exit;
  2014. eq:=compare_defs_ext(currpara1.vardef,currpara2.vardef,nothingn,
  2015. convtype,hpd,cdoptions);
  2016. end;
  2017. cp_procvar :
  2018. begin
  2019. if not(cpo_ignorevarspez in cpoptions) and
  2020. (currpara1.varspez<>currpara2.varspez) then
  2021. exit;
  2022. { "univ" state doesn't matter here: from univ to non-univ
  2023. matches if the types are compatible (i.e., as usual),
  2024. from from non-univ to univ also matches if the types
  2025. have the same size (checked below) }
  2026. eq:=compare_defs_ext(currpara1.vardef,currpara2.vardef,nothingn,
  2027. convtype,hpd,cdoptions);
  2028. { Parameters must be at least equal otherwise the are incompatible }
  2029. if (eq<te_equal) then
  2030. eq:=te_incompatible;
  2031. end;
  2032. else
  2033. eq:=compare_defs_ext(currpara1.vardef,currpara2.vardef,nothingn,
  2034. convtype,hpd,cdoptions);
  2035. end;
  2036. end;
  2037. { check type }
  2038. if eq=te_incompatible then
  2039. begin
  2040. { special case: "univ" parameters match if their size is equal }
  2041. if not(cpo_ignoreuniv in cpoptions) and
  2042. currpara2.univpara and
  2043. is_valid_univ_para_type(currpara1.vardef) and
  2044. (currpara1.vardef.size=currpara2.vardef.size) then
  2045. begin
  2046. { only pick as last choice }
  2047. eq:=te_convert_l5;
  2048. if (acp=cp_procvar) and
  2049. (cpo_warn_incompatible_univ in cpoptions) then
  2050. begin
  2051. { if the types may be passed in different ways by the
  2052. calling convention then this can lead to crashes
  2053. (note: not an exhaustive check, and failing this
  2054. this check does not mean things will crash on all
  2055. platforms) }
  2056. if potentially_incompatible_univ_paras(currpara1.vardef,currpara2.vardef) then
  2057. Message2(type_w_procvar_univ_conflicting_para,currpara1.vardef.typename,currpara2.vardef.typename)
  2058. end;
  2059. end
  2060. else if (cpo_generic in cpoptions) then
  2061. begin
  2062. if equal_genfunc_paradefs(currpara1.vardef,currpara2.vardef) then
  2063. eq:=te_exact
  2064. else
  2065. exit;
  2066. end
  2067. else
  2068. exit;
  2069. end;
  2070. if (eq=te_equal) and
  2071. (cpo_generic in cpoptions) then
  2072. begin
  2073. if is_open_array(currpara1.vardef) and
  2074. is_open_array(currpara2.vardef) then
  2075. begin
  2076. if equal_genfunc_paradefs(tarraydef(currpara1.vardef).elementdef,tarraydef(currpara2.vardef).elementdef) then
  2077. eq:=te_exact;
  2078. end
  2079. else
  2080. { for the purpose of forward declarations two equal specializations
  2081. are considered as exactly equal }
  2082. if tstoreddef(currpara1.vardef).is_specialization and
  2083. tstoreddef(currpara2.vardef).is_specialization then
  2084. eq:=te_exact;
  2085. end;
  2086. { open strings can never match exactly, since you cannot define }
  2087. { a separate "open string" type -> we have to be able to }
  2088. { consider those as exact when resolving forward definitions. }
  2089. { The same goes for array of const. Open arrays are handled }
  2090. { already (if their element types match exactly, they are }
  2091. { considered to be an exact match) }
  2092. { And also for "inline defined" function parameter definitions }
  2093. { (i.e., function types directly declared in a parameter list) }
  2094. if (is_array_of_const(currpara1.vardef) or
  2095. is_open_string(currpara1.vardef) or
  2096. ((currpara1.vardef.typ = procvardef) and
  2097. not(assigned(currpara1.vardef.typesym)))) and
  2098. (eq=te_equal) and
  2099. (cpo_openequalisexact in cpoptions) then
  2100. eq:=te_exact;
  2101. if eq<lowesteq then
  2102. lowesteq:=eq;
  2103. { also check default value if both have it declared }
  2104. if (cpo_comparedefaultvalue in cpoptions) then
  2105. begin
  2106. if assigned(currpara1.defaultconstsym) and
  2107. assigned(currpara2.defaultconstsym) then
  2108. begin
  2109. if not equal_constsym(tconstsym(currpara1.defaultconstsym),tconstsym(currpara2.defaultconstsym),true) then
  2110. exit;
  2111. end
  2112. { cannot have that the second (= implementation) has a default value declared and the
  2113. other (interface) doesn't }
  2114. else if not assigned(currpara1.defaultconstsym) and assigned(currpara2.defaultconstsym) then
  2115. exit;
  2116. end;
  2117. if not(cpo_compilerproc in cpoptions) and
  2118. not(cpo_rtlproc in cpoptions) and
  2119. is_ansistring(currpara1.vardef) and
  2120. is_ansistring(currpara2.vardef) and
  2121. (tstringdef(currpara1.vardef).encoding<>tstringdef(currpara2.vardef).encoding) and
  2122. ((tstringdef(currpara1.vardef).encoding=globals.CP_NONE) or
  2123. (tstringdef(currpara2.vardef).encoding=globals.CP_NONE)
  2124. ) then
  2125. eq:=te_convert_l1;
  2126. if eq<lowesteq then
  2127. lowesteq:=eq;
  2128. inc(i1);
  2129. inc(i2);
  2130. if cpo_ignorehidden in cpoptions then
  2131. begin
  2132. while (i1<para1.count) and
  2133. (vo_is_hidden_para in tparavarsym(para1[i1]).varoptions) do
  2134. inc(i1);
  2135. while (i2<para2.count) and
  2136. (vo_is_hidden_para in tparavarsym(para2[i2]).varoptions) do
  2137. inc(i2);
  2138. end;
  2139. if cpo_ignoreframepointer in cpoptions then
  2140. begin
  2141. if (i1<para1.count) and
  2142. (vo_is_parentfp in tparavarsym(para1[i1]).varoptions) then
  2143. inc(i1);
  2144. if (i2<para2.count) and
  2145. (vo_is_parentfp in tparavarsym(para2[i2]).varoptions) then
  2146. inc(i2);
  2147. end;
  2148. end;
  2149. { when both lists are empty then the parameters are equal. Also
  2150. when one list is empty and the other has a parameter with default
  2151. value assigned then the parameters are also equal }
  2152. if ((i1>=para1.count) and (i2>=para2.count)) or
  2153. ((cpo_allowdefaults in cpoptions) and
  2154. (((i1<para1.count) and assigned(tparavarsym(para1[i1]).defaultconstsym)) or
  2155. ((i2<para2.count) and assigned(tparavarsym(para2[i2]).defaultconstsym)))) then
  2156. compare_paras:=lowesteq;
  2157. end;
  2158. function proc_to_procvar_equal(def1:tabstractprocdef;def2:tprocvardef;checkincompatibleuniv: boolean):tequaltype;
  2159. var
  2160. eq: tequaltype;
  2161. po_comp: tprocoptions;
  2162. pa_comp: tcompare_paras_options;
  2163. begin
  2164. proc_to_procvar_equal:=te_incompatible;
  2165. if not(assigned(def1)) or not(assigned(def2)) then
  2166. exit;
  2167. { check for method pointer and local procedure pointer:
  2168. a) anything but procvars can be assigned to blocks
  2169. b) if one is a procedure of object, the other also has to be one
  2170. ("object static procedure" is equal to procedure as well)
  2171. (except for block)
  2172. c) if one is a pure address, the other also has to be one
  2173. except if def1 is a global proc and def2 is a nested procdef
  2174. (global procedures can be converted into nested procvars)
  2175. d) if def1 is a nested procedure, then def2 has to be a nested
  2176. procvar and def1 has to have the po_delphi_nested_cc option
  2177. e) if def1 is a procvar, def1 and def2 both have to be nested or
  2178. non-nested (we don't allow assignments from non-nested to
  2179. nested procvars to make sure that we can still implement
  2180. nested procvars using trampolines -- e.g., this would be
  2181. necessary for LLVM or CIL as long as they do not have support
  2182. for Delphi-style frame pointer parameter passing) }
  2183. if is_block(def2) then { a) }
  2184. { can't explicitly check against procvars here, because
  2185. def1 may already be a procvar due to a proc_to_procvar;
  2186. this is checked in the type conversion node itself -> ok }
  2187. else if
  2188. ((def1.is_methodpointer and not (po_staticmethod in def1.procoptions))<> { b) }
  2189. (def2.is_methodpointer and not (po_staticmethod in def2.procoptions))) or
  2190. ((def1.is_addressonly<>def2.is_addressonly) and { c) }
  2191. (is_nested_pd(def1) or
  2192. not is_nested_pd(def2))) or
  2193. ((def1.typ=procdef) and { d) }
  2194. is_nested_pd(def1) and
  2195. (not(po_delphi_nested_cc in def1.procoptions) or
  2196. not is_nested_pd(def2))) or
  2197. ((def1.typ=procvardef) and { e) }
  2198. (is_nested_pd(def1)<>is_nested_pd(def2))) then
  2199. exit;
  2200. pa_comp:=[cpo_ignoreframepointer];
  2201. if is_block(def2) then
  2202. include(pa_comp,cpo_ignorehidden);
  2203. if checkincompatibleuniv then
  2204. include(pa_comp,cpo_warn_incompatible_univ);
  2205. { check return value and options, methodpointer is already checked }
  2206. po_comp:=[po_interrupt,po_iocheck,po_varargs];
  2207. { check static only if we compare method pointers }
  2208. if def1.is_methodpointer and def2.is_methodpointer then
  2209. include(po_comp,po_staticmethod);
  2210. if (m_delphi in current_settings.modeswitches) then
  2211. exclude(po_comp,po_varargs);
  2212. { for blocks, the calling convention doesn't matter because we have to
  2213. generate a wrapper anyway }
  2214. if ((po_is_block in def2.procoptions) or
  2215. (def1.proccalloption=def2.proccalloption)) and
  2216. ((po_comp * def1.procoptions)= (po_comp * def2.procoptions)) and
  2217. equal_defs(def1.returndef,def2.returndef) then
  2218. begin
  2219. { return equal type based on the parameters, but a proc->procvar
  2220. is never exact, so map an exact match of the parameters to
  2221. te_equal }
  2222. eq:=compare_paras(def1.paras,def2.paras,cp_procvar,pa_comp);
  2223. if eq=te_exact then
  2224. eq:=te_equal;
  2225. if (eq=te_equal) then
  2226. begin
  2227. { prefer non-nested to non-nested over non-nested to nested }
  2228. if (is_nested_pd(def1)<>is_nested_pd(def2)) then
  2229. eq:=te_convert_l1;
  2230. { in case of non-block to block, we need a type conversion }
  2231. if (po_is_block in def1.procoptions) <> (po_is_block in def2.procoptions) then
  2232. eq:=te_convert_l1;
  2233. end;
  2234. proc_to_procvar_equal:=eq;
  2235. end;
  2236. end;
  2237. function compatible_childmethod_resultdef(parentretdef, childretdef: tdef): boolean;
  2238. begin
  2239. compatible_childmethod_resultdef :=
  2240. (equal_defs(parentretdef,childretdef)) or
  2241. ((parentretdef.typ=objectdef) and
  2242. (childretdef.typ=objectdef) and
  2243. is_class_or_interface_or_objc_or_java(parentretdef) and
  2244. is_class_or_interface_or_objc_or_java(childretdef) and
  2245. (def_is_related(tobjectdef(childretdef),tobjectdef(parentretdef))))
  2246. end;
  2247. function find_implemented_interface(impldef,intfdef:tobjectdef):timplementedinterface;
  2248. var
  2249. implintf : timplementedinterface;
  2250. i : longint;
  2251. begin
  2252. if not assigned(impldef) then
  2253. internalerror(2013102301);
  2254. if not assigned(intfdef) then
  2255. internalerror(2013102302);
  2256. result:=nil;
  2257. if not assigned(impldef.implementedinterfaces) then
  2258. exit;
  2259. for i:=0 to impldef.implementedinterfaces.count-1 do
  2260. begin
  2261. implintf:=timplementedinterface(impldef.implementedinterfaces[i]);
  2262. if equal_defs(implintf.intfdef,intfdef) then
  2263. begin
  2264. result:=implintf;
  2265. exit;
  2266. end;
  2267. end;
  2268. end;
  2269. function stringdef_is_related(curdef:tstringdef;otherdef:tdef):boolean;
  2270. begin
  2271. result:=
  2272. (target_info.system in systems_jvm) and
  2273. (((curdef.stringtype in [st_unicodestring,st_widestring]) and
  2274. ((otherdef=java_jlobject) or
  2275. (otherdef=java_jlstring))) or
  2276. ((curdef.stringtype=st_ansistring) and
  2277. ((otherdef=java_jlobject) or
  2278. (otherdef=java_ansistring))));
  2279. end;
  2280. function recorddef_is_related(curdef:trecorddef;otherdef:tdef):boolean;
  2281. begin
  2282. { records are implemented via classes in the JVM target, and are
  2283. all descendents of the java_fpcbaserecordtype class }
  2284. result:=false;
  2285. if (target_info.system in systems_jvm) then
  2286. begin
  2287. if otherdef.typ=objectdef then
  2288. begin
  2289. otherdef:=find_real_class_definition(tobjectdef(otherdef),false);
  2290. if (otherdef=java_jlobject) or
  2291. (otherdef=java_fpcbaserecordtype) then
  2292. result:=true
  2293. end;
  2294. end;
  2295. end;
  2296. { true if prot implements d (or if they are equal) }
  2297. function is_related_interface_multiple(prot:tobjectdef;d:tdef):boolean;
  2298. var
  2299. i : longint;
  2300. begin
  2301. { objcprotocols have multiple inheritance, all protocols from which
  2302. the current protocol inherits are stored in implementedinterfaces }
  2303. result:=prot=d;
  2304. if result then
  2305. exit;
  2306. for i:=0 to prot.implementedinterfaces.count-1 do
  2307. begin
  2308. result:=is_related_interface_multiple(timplementedinterface(prot.implementedinterfaces[i]).intfdef,d);
  2309. if result then
  2310. exit;
  2311. end;
  2312. end;
  2313. function objectdef_is_related(curdef:tobjectdef;otherdef:tdef):boolean;
  2314. var
  2315. realself,
  2316. hp : tobjectdef;
  2317. begin
  2318. if (otherdef.typ=objectdef) then
  2319. otherdef:=find_real_class_definition(tobjectdef(otherdef),false);
  2320. realself:=find_real_class_definition(curdef,false);
  2321. if realself=otherdef then
  2322. begin
  2323. result:=true;
  2324. exit;
  2325. end;
  2326. if (otherdef.typ<>objectdef) then
  2327. begin
  2328. result:=false;
  2329. exit;
  2330. end;
  2331. { Objective-C protocols and Java interfaces can use multiple
  2332. inheritance }
  2333. if (realself.objecttype in [odt_objcprotocol,odt_interfacejava]) then
  2334. begin
  2335. result:=is_related_interface_multiple(realself,otherdef);
  2336. exit;
  2337. end;
  2338. { formally declared Objective-C and Java classes match Objective-C/Java
  2339. classes with the same name. In case of Java, the package must also
  2340. match (still required even though we looked up the real definitions
  2341. above, because these may be two different formal declarations that
  2342. cannot be resolved yet) }
  2343. if (realself.objecttype in [odt_objcclass,odt_javaclass]) and
  2344. (tobjectdef(otherdef).objecttype=curdef.objecttype) and
  2345. ((oo_is_formal in curdef.objectoptions) or
  2346. (oo_is_formal in tobjectdef(otherdef).objectoptions)) and
  2347. (curdef.objrealname^=tobjectdef(otherdef).objrealname^) then
  2348. begin
  2349. { check package name for Java }
  2350. if curdef.objecttype=odt_objcclass then
  2351. result:=true
  2352. else
  2353. begin
  2354. result:=
  2355. assigned(curdef.import_lib)=assigned(tobjectdef(otherdef).import_lib);
  2356. if result and
  2357. assigned(curdef.import_lib) then
  2358. result:=curdef.import_lib^=tobjectdef(otherdef).import_lib^;
  2359. end;
  2360. exit;
  2361. end;
  2362. hp:=realself.childof;
  2363. while assigned(hp) do
  2364. begin
  2365. if equal_defs(hp,otherdef) then
  2366. begin
  2367. result:=true;
  2368. exit;
  2369. end;
  2370. hp:=hp.childof;
  2371. end;
  2372. result:=false;
  2373. end;
  2374. function def_is_related(curdef,otherdef:tdef):boolean;
  2375. begin
  2376. if not assigned(curdef) then
  2377. internalerror(2013102303);
  2378. case curdef.typ of
  2379. stringdef:
  2380. result:=stringdef_is_related(tstringdef(curdef),otherdef);
  2381. recorddef:
  2382. result:=recorddef_is_related(trecorddef(curdef),otherdef);
  2383. objectdef:
  2384. result:=objectdef_is_related(tobjectdef(curdef),otherdef);
  2385. else
  2386. result:=false;
  2387. end;
  2388. end;
  2389. end.