defcmp.pas 124 KB

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