defcmp.pas 93 KB

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