defcmp.pas 118 KB

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