defcmp.pas 96 KB

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