defcmp.pas 114 KB

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