defcmp.pas 116 KB

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