defcmp.pas 129 KB

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