defcmp.pas 133 KB

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