defcmp.pas 117 KB

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