defcmp.pas 110 KB

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