defcmp.pas 114 KB

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