defcmp.pas 85 KB

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