defcmp.pas 106 KB

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