defcmp.pas 112 KB

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