defcmp.pas 110 KB

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