defcmp.pas 114 KB

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