defcmp.pas 93 KB

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