defcmp.pas 72 KB

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