defcmp.pas 115 KB

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