defcmp.pas 88 KB

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