2
0

defcmp.pas 119 KB

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