defcmp.pas 97 KB

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