defcmp.pas 69 KB

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