defcmp.pas 69 KB

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