defcmp.pas 110 KB

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