defcmp.pas 92 KB

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