defcmp.pas 111 KB

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