defcmp.pas 115 KB

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