defcmp.pas 115 KB

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