defcmp.pas 71 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656
  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. eq:=te_equal;
  677. end
  678. else
  679. { to array of const }
  680. if is_array_of_const(def_to) then
  681. begin
  682. if is_array_of_const(def_from) or
  683. is_array_constructor(def_from) then
  684. begin
  685. eq:=te_equal;
  686. end
  687. else
  688. { array of tvarrec -> array of const }
  689. if equal_defs(tarraydef(def_to).elementdef,tarraydef(def_from).elementdef) then
  690. begin
  691. doconv:=tc_equal;
  692. eq:=te_convert_l1;
  693. end;
  694. end
  695. else
  696. { to array of char, from "Untyped" stringconstn (array of char) }
  697. if (fromtreetype=stringconstn) and
  698. (is_chararray(def_to) or
  699. is_widechararray(def_to)) then
  700. begin
  701. eq:=te_convert_l1;
  702. doconv:=tc_string_2_chararray;
  703. end
  704. else
  705. { other arrays }
  706. begin
  707. { open array -> array }
  708. if not(cdo_parameter in cdoptions) and
  709. is_open_array(def_from) and
  710. equal_defs(tarraydef(def_from).elementdef,tarraydef(def_to).elementdef) then
  711. begin
  712. eq:=te_equal
  713. end
  714. else
  715. { array -> array }
  716. if not(m_tp7 in current_settings.modeswitches) and
  717. not(m_delphi in current_settings.modeswitches) and
  718. (tarraydef(def_from).lowrange=tarraydef(def_to).lowrange) and
  719. (tarraydef(def_from).highrange=tarraydef(def_to).highrange) and
  720. equal_defs(tarraydef(def_from).elementdef,tarraydef(def_to).elementdef) and
  721. equal_defs(tarraydef(def_from).rangedef,tarraydef(def_to).rangedef) then
  722. begin
  723. eq:=te_equal
  724. end;
  725. end;
  726. end;
  727. pointerdef :
  728. begin
  729. { nil and voidpointers are compatible with dyn. arrays }
  730. if is_dynamic_array(def_to) and
  731. ((fromtreetype=niln) or
  732. is_voidpointer(def_from)) then
  733. begin
  734. doconv:=tc_equal;
  735. eq:=te_convert_l1;
  736. end
  737. else
  738. if is_zero_based_array(def_to) and
  739. equal_defs(tpointerdef(def_from).pointeddef,tarraydef(def_to).elementdef) then
  740. begin
  741. doconv:=tc_pointer_2_array;
  742. eq:=te_convert_l1;
  743. end;
  744. end;
  745. stringdef :
  746. begin
  747. { string to char array }
  748. if (not is_special_array(def_to)) and
  749. (is_char(tarraydef(def_to).elementdef)or
  750. is_widechar(tarraydef(def_to).elementdef)) then
  751. begin
  752. doconv:=tc_string_2_chararray;
  753. eq:=te_convert_l1;
  754. end;
  755. end;
  756. orddef:
  757. begin
  758. if is_chararray(def_to) and
  759. is_char(def_from) then
  760. begin
  761. doconv:=tc_char_2_chararray;
  762. eq:=te_convert_l2;
  763. end;
  764. end;
  765. recorddef :
  766. begin
  767. { tvarrec -> array of const }
  768. if is_array_of_const(def_to) and
  769. equal_defs(def_from,tarraydef(def_to).elementdef) then
  770. begin
  771. doconv:=tc_equal;
  772. eq:=te_convert_l1;
  773. end;
  774. end;
  775. variantdef :
  776. begin
  777. if is_dynamic_array(def_to) then
  778. begin
  779. doconv:=tc_variant_2_dynarray;
  780. eq:=te_convert_l1;
  781. end;
  782. end;
  783. end;
  784. end;
  785. end;
  786. variantdef :
  787. begin
  788. if (cdo_allow_variant in cdoptions) then
  789. begin
  790. case def_from.typ of
  791. enumdef :
  792. begin
  793. doconv:=tc_enum_2_variant;
  794. eq:=te_convert_l1;
  795. end;
  796. arraydef :
  797. begin
  798. if is_dynamic_array(def_from) then
  799. begin
  800. doconv:=tc_dynarray_2_variant;
  801. eq:=te_convert_l1;
  802. end;
  803. end;
  804. objectdef :
  805. begin
  806. if is_interface(def_from) then
  807. begin
  808. doconv:=tc_interface_2_variant;
  809. eq:=te_convert_l1;
  810. end;
  811. end;
  812. variantdef :
  813. begin
  814. { doing this in the compiler avoids a lot of unncessary
  815. copying }
  816. if (tvariantdef(def_from).varianttype=vt_olevariant) and
  817. (tvariantdef(def_to).varianttype=vt_normalvariant) then
  818. begin
  819. doconv:=tc_equal;
  820. eq:=te_convert_l1;
  821. end;
  822. end;
  823. end;
  824. end;
  825. end;
  826. pointerdef :
  827. begin
  828. case def_from.typ of
  829. stringdef :
  830. begin
  831. { string constant (which can be part of array constructor)
  832. to zero terminated string constant }
  833. if (((fromtreetype = arrayconstructorn) and
  834. { can't use is_chararray, because returns false for }
  835. { array constructors }
  836. is_char(tarraydef(def_from).elementdef)) or
  837. (fromtreetype = stringconstn)) and
  838. (is_pchar(def_to) or is_pwidechar(def_to)) then
  839. begin
  840. doconv:=tc_cstring_2_pchar;
  841. eq:=te_convert_l2;
  842. end
  843. else
  844. if cdo_explicit in cdoptions then
  845. begin
  846. { pchar(ansistring) }
  847. if is_pchar(def_to) and
  848. is_ansistring(def_from) then
  849. begin
  850. doconv:=tc_ansistring_2_pchar;
  851. eq:=te_convert_l1;
  852. end
  853. else
  854. { pwidechar(widestring) }
  855. if is_pwidechar(def_to) and
  856. is_widestring(def_from) then
  857. begin
  858. doconv:=tc_ansistring_2_pchar;
  859. eq:=te_convert_l1;
  860. end;
  861. end;
  862. end;
  863. orddef :
  864. begin
  865. { char constant to zero terminated string constant }
  866. if (fromtreetype in [ordconstn,arrayconstructorn]) then
  867. begin
  868. if (is_char(def_from) or is_widechar(def_from)) and
  869. (is_pchar(def_to) or is_pwidechar(def_to)) then
  870. begin
  871. doconv:=tc_cchar_2_pchar;
  872. eq:=te_convert_l1;
  873. end
  874. else
  875. if (m_delphi in current_settings.modeswitches) and is_integer(def_from) then
  876. begin
  877. doconv:=tc_cord_2_pointer;
  878. eq:=te_convert_l5;
  879. end;
  880. end;
  881. { allow explicit typecasts from ordinals to pointer.
  882. Support for delphi compatibility
  883. Support constructs like pointer(cardinal-cardinal) or pointer(longint+cardinal) where
  884. the result of the ordinal operation is int64 also on 32 bit platforms.
  885. It is also used by the compiler internally for inc(pointer,ordinal) }
  886. if (eq=te_incompatible) and
  887. not is_void(def_from) and
  888. (
  889. (
  890. (cdo_explicit in cdoptions) and
  891. (
  892. (m_delphi in current_settings.modeswitches) or
  893. { Don't allow pchar(char) in fpc modes }
  894. is_integer(def_from)
  895. )
  896. ) or
  897. (cdo_internal in cdoptions)
  898. ) then
  899. begin
  900. doconv:=tc_int_2_int;
  901. eq:=te_convert_l1;
  902. end;
  903. end;
  904. enumdef :
  905. begin
  906. { allow explicit typecasts from enums to pointer.
  907. Support for delphi compatibility
  908. }
  909. if (eq=te_incompatible) and
  910. (((cdo_explicit in cdoptions) and
  911. (m_delphi in current_settings.modeswitches)
  912. ) or
  913. (cdo_internal in cdoptions)
  914. ) then
  915. begin
  916. doconv:=tc_int_2_int;
  917. eq:=te_convert_l1;
  918. end;
  919. end;
  920. arraydef :
  921. begin
  922. { string constant (which can be part of array constructor)
  923. to zero terminated string constant }
  924. if (((fromtreetype = arrayconstructorn) and
  925. { can't use is_chararray, because returns false for }
  926. { array constructors }
  927. is_char(tarraydef(def_from).elementdef)) or
  928. (fromtreetype = stringconstn)) and
  929. (is_pchar(def_to) or is_pwidechar(def_to)) then
  930. begin
  931. doconv:=tc_cstring_2_pchar;
  932. eq:=te_convert_l2;
  933. end
  934. else
  935. { chararray to pointer }
  936. if (is_zero_based_array(def_from) or
  937. is_open_array(def_from)) and
  938. equal_defs(tarraydef(def_from).elementdef,tpointerdef(def_to).pointeddef) then
  939. begin
  940. doconv:=tc_array_2_pointer;
  941. { don't prefer the pchar overload when a constant
  942. string was passed }
  943. if fromtreetype=stringconstn then
  944. eq:=te_convert_l2
  945. else
  946. eq:=te_convert_l1;
  947. end
  948. else
  949. { dynamic array to pointer, delphi only }
  950. if (m_delphi in current_settings.modeswitches) and
  951. is_dynamic_array(def_from) then
  952. begin
  953. eq:=te_equal;
  954. end;
  955. end;
  956. pointerdef :
  957. begin
  958. { check for far pointers }
  959. if (tpointerdef(def_from).is_far<>tpointerdef(def_to).is_far) then
  960. begin
  961. eq:=te_incompatible;
  962. end
  963. else
  964. { the types can be forward type, handle before normal type check !! }
  965. if assigned(def_to.typesym) and
  966. (tpointerdef(def_to).pointeddef.typ=forwarddef) then
  967. begin
  968. if (def_from.typesym=def_to.typesym) then
  969. eq:=te_equal
  970. end
  971. else
  972. { same types }
  973. if equal_defs(tpointerdef(def_from).pointeddef,tpointerdef(def_to).pointeddef) then
  974. begin
  975. eq:=te_equal
  976. end
  977. else
  978. { child class pointer can be assigned to anchestor pointers }
  979. if (
  980. (tpointerdef(def_from).pointeddef.typ=objectdef) and
  981. (tpointerdef(def_to).pointeddef.typ=objectdef) and
  982. tobjectdef(tpointerdef(def_from).pointeddef).is_related(
  983. tobjectdef(tpointerdef(def_to).pointeddef))
  984. ) then
  985. begin
  986. doconv:=tc_equal;
  987. eq:=te_convert_l1;
  988. end
  989. else
  990. { all pointers can be assigned to void-pointer }
  991. if is_void(tpointerdef(def_to).pointeddef) then
  992. begin
  993. doconv:=tc_equal;
  994. { give pwidechar,pchar a penalty so it prefers
  995. conversion to ansistring }
  996. if is_pchar(def_from) or
  997. is_pwidechar(def_from) then
  998. eq:=te_convert_l2
  999. else
  1000. eq:=te_convert_l1;
  1001. end
  1002. else
  1003. { all pointers can be assigned from void-pointer }
  1004. if is_void(tpointerdef(def_from).pointeddef) or
  1005. { all pointers can be assigned from void-pointer or formaldef pointer, check
  1006. tw3777.pp if you change this }
  1007. (tpointerdef(def_from).pointeddef.typ=formaldef) then
  1008. begin
  1009. doconv:=tc_equal;
  1010. { give pwidechar a penalty so it prefers
  1011. conversion to pchar }
  1012. if is_pwidechar(def_to) then
  1013. eq:=te_convert_l2
  1014. else
  1015. eq:=te_convert_l1;
  1016. end;
  1017. end;
  1018. procvardef :
  1019. begin
  1020. { procedure variable can be assigned to an void pointer,
  1021. this not allowed for methodpointers }
  1022. if (is_void(tpointerdef(def_to).pointeddef) or
  1023. (m_mac_procvar in current_settings.modeswitches)) and
  1024. tprocvardef(def_from).is_addressonly then
  1025. begin
  1026. doconv:=tc_equal;
  1027. eq:=te_convert_l1;
  1028. end;
  1029. end;
  1030. procdef :
  1031. begin
  1032. { procedure variable can be assigned to an void pointer,
  1033. this not allowed for methodpointers }
  1034. if (m_mac_procvar in current_settings.modeswitches) and
  1035. tprocdef(def_from).is_addressonly then
  1036. begin
  1037. doconv:=tc_proc_2_procvar;
  1038. eq:=te_convert_l2;
  1039. end;
  1040. end;
  1041. classrefdef,
  1042. objectdef :
  1043. begin
  1044. { class types and class reference type
  1045. can be assigned to void pointers, but it is less
  1046. preferred than assigning to a related objectdef }
  1047. if (
  1048. is_class_or_interface_or_dispinterface(def_from) or
  1049. (def_from.typ=classrefdef)
  1050. ) and
  1051. (tpointerdef(def_to).pointeddef.typ=orddef) and
  1052. (torddef(tpointerdef(def_to).pointeddef).ordtype=uvoid) then
  1053. begin
  1054. doconv:=tc_equal;
  1055. eq:=te_convert_l2;
  1056. end;
  1057. end;
  1058. end;
  1059. end;
  1060. setdef :
  1061. begin
  1062. case def_from.typ of
  1063. setdef :
  1064. begin
  1065. if assigned(tsetdef(def_from).elementdef) and
  1066. assigned(tsetdef(def_to).elementdef) then
  1067. begin
  1068. { sets with the same element base type and the same range are equal }
  1069. if equal_defs(tsetdef(def_from).elementdef,tsetdef(def_to).elementdef) and
  1070. (tsetdef(def_from).setbase=tsetdef(def_to).setbase) and
  1071. (tsetdef(def_from).setmax=tsetdef(def_to).setmax) then
  1072. eq:=te_equal
  1073. else if is_subequal(tsetdef(def_from).elementdef,tsetdef(def_to).elementdef) then
  1074. begin
  1075. eq:=te_convert_l1;
  1076. doconv:=tc_set_to_set;
  1077. end;
  1078. end
  1079. else
  1080. begin
  1081. { empty set is compatible with everything }
  1082. eq:=te_convert_l1;
  1083. doconv:=tc_set_to_set;
  1084. end;
  1085. end;
  1086. arraydef :
  1087. begin
  1088. { automatic arrayconstructor -> set conversion }
  1089. if is_array_constructor(def_from) then
  1090. begin
  1091. doconv:=tc_arrayconstructor_2_set;
  1092. eq:=te_convert_l1;
  1093. end;
  1094. end;
  1095. end;
  1096. end;
  1097. procvardef :
  1098. begin
  1099. case def_from.typ of
  1100. procdef :
  1101. begin
  1102. { proc -> procvar }
  1103. if (m_tp_procvar in current_settings.modeswitches) or
  1104. (m_mac_procvar in current_settings.modeswitches) then
  1105. begin
  1106. subeq:=proc_to_procvar_equal(tprocdef(def_from),tprocvardef(def_to));
  1107. if subeq>te_incompatible then
  1108. begin
  1109. doconv:=tc_proc_2_procvar;
  1110. eq:=te_convert_l1;
  1111. end;
  1112. end;
  1113. end;
  1114. procvardef :
  1115. begin
  1116. { procvar -> procvar }
  1117. eq:=proc_to_procvar_equal(tprocvardef(def_from),tprocvardef(def_to));
  1118. end;
  1119. pointerdef :
  1120. begin
  1121. { nil is compatible with procvars }
  1122. if (fromtreetype=niln) then
  1123. begin
  1124. if not Tprocvardef(def_to).is_addressonly then
  1125. {Nil to method pointers requires to convert a single
  1126. pointer nil value to a two pointer procvardef.}
  1127. doconv:=tc_nil_2_methodprocvar
  1128. else
  1129. doconv:=tc_equal;
  1130. eq:=te_convert_l1;
  1131. end
  1132. else
  1133. { for example delphi allows the assignement from pointers }
  1134. { to procedure variables }
  1135. if (m_pointer_2_procedure in current_settings.modeswitches) and
  1136. is_void(tpointerdef(def_from).pointeddef) and
  1137. tprocvardef(def_to).is_addressonly then
  1138. begin
  1139. doconv:=tc_equal;
  1140. eq:=te_convert_l1;
  1141. end;
  1142. end;
  1143. end;
  1144. end;
  1145. objectdef :
  1146. begin
  1147. { object pascal objects }
  1148. if (def_from.typ=objectdef) and
  1149. (tobjectdef(def_from).is_related(tobjectdef(def_to))) then
  1150. begin
  1151. doconv:=tc_equal;
  1152. eq:=te_convert_l1;
  1153. end
  1154. else
  1155. { Class/interface specific }
  1156. if is_class_or_interface_or_dispinterface(def_to) then
  1157. begin
  1158. { void pointer also for delphi mode }
  1159. if (m_delphi in current_settings.modeswitches) and
  1160. is_voidpointer(def_from) then
  1161. begin
  1162. doconv:=tc_equal;
  1163. { prefer pointer-pointer assignments }
  1164. eq:=te_convert_l2;
  1165. end
  1166. else
  1167. { nil is compatible with class instances and interfaces }
  1168. if (fromtreetype=niln) then
  1169. begin
  1170. doconv:=tc_equal;
  1171. eq:=te_convert_l1;
  1172. end
  1173. { classes can be assigned to interfaces }
  1174. else if is_interface(def_to) and
  1175. is_class(def_from) and
  1176. assigned(tobjectdef(def_from).ImplementedInterfaces) then
  1177. begin
  1178. { we've to search in parent classes as well }
  1179. hobjdef:=tobjectdef(def_from);
  1180. while assigned(hobjdef) do
  1181. begin
  1182. if hobjdef.find_implemented_interface(tobjectdef(def_to))<>nil then
  1183. begin
  1184. doconv:=tc_class_2_intf;
  1185. { don't prefer this over objectdef->objectdef }
  1186. eq:=te_convert_l2;
  1187. break;
  1188. end;
  1189. hobjdef:=hobjdef.childof;
  1190. end;
  1191. end
  1192. { Interface 2 GUID handling }
  1193. else if (def_to=tdef(rec_tguid)) and
  1194. (fromtreetype=typen) and
  1195. is_interface(def_from) and
  1196. assigned(tobjectdef(def_from).iidguid) then
  1197. begin
  1198. eq:=te_convert_l1;
  1199. doconv:=tc_equal;
  1200. end
  1201. else if (def_from.typ=variantdef) and is_interface(def_to) then
  1202. begin
  1203. doconv:=tc_variant_2_interface;
  1204. eq:=te_convert_l2;
  1205. end
  1206. { ugly, but delphi allows it }
  1207. else if (eq=te_incompatible) and
  1208. (def_from.typ=orddef) and
  1209. (m_delphi in current_settings.modeswitches) and
  1210. (cdo_explicit in cdoptions) then
  1211. begin
  1212. doconv:=tc_int_2_int;
  1213. eq:=te_convert_l1;
  1214. end;
  1215. end;
  1216. end;
  1217. classrefdef :
  1218. begin
  1219. { similar to pointerdef wrt forwards }
  1220. if assigned(def_to.typesym) and
  1221. (tclassrefdef(def_to).pointeddef.typ=forwarddef) then
  1222. begin
  1223. if (def_from.typesym=def_to.typesym) then
  1224. eq:=te_equal;
  1225. end
  1226. else
  1227. { class reference types }
  1228. if (def_from.typ=classrefdef) then
  1229. begin
  1230. if equal_defs(tclassrefdef(def_from).pointeddef,tclassrefdef(def_to).pointeddef) then
  1231. begin
  1232. eq:=te_equal;
  1233. end
  1234. else
  1235. begin
  1236. doconv:=tc_equal;
  1237. if (cdo_explicit in cdoptions) or
  1238. tobjectdef(tclassrefdef(def_from).pointeddef).is_related(
  1239. tobjectdef(tclassrefdef(def_to).pointeddef)) then
  1240. eq:=te_convert_l1;
  1241. end;
  1242. end
  1243. else
  1244. if (m_delphi in current_settings.modeswitches) and
  1245. is_voidpointer(def_from) then
  1246. begin
  1247. doconv:=tc_equal;
  1248. { prefer pointer-pointer assignments }
  1249. eq:=te_convert_l2;
  1250. end
  1251. else
  1252. { nil is compatible with class references }
  1253. if (fromtreetype=niln) then
  1254. begin
  1255. doconv:=tc_equal;
  1256. eq:=te_convert_l1;
  1257. end;
  1258. end;
  1259. filedef :
  1260. begin
  1261. { typed files are all equal to the abstract file type
  1262. name TYPEDFILE in system.pp in is_equal in types.pas
  1263. the problem is that it sholud be also compatible to FILE
  1264. but this would leed to a problem for ASSIGN RESET and REWRITE
  1265. when trying to find the good overloaded function !!
  1266. so all file function are doubled in system.pp
  1267. this is not very beautiful !!}
  1268. if (def_from.typ=filedef) then
  1269. begin
  1270. if (tfiledef(def_from).filetyp=tfiledef(def_to).filetyp) then
  1271. begin
  1272. if
  1273. (
  1274. (tfiledef(def_from).typedfiledef=nil) and
  1275. (tfiledef(def_to).typedfiledef=nil)
  1276. ) or
  1277. (
  1278. (tfiledef(def_from).typedfiledef<>nil) and
  1279. (tfiledef(def_to).typedfiledef<>nil) and
  1280. equal_defs(tfiledef(def_from).typedfiledef,tfiledef(def_to).typedfiledef)
  1281. ) or
  1282. (
  1283. (tfiledef(def_from).filetyp = ft_typed) and
  1284. (tfiledef(def_to).filetyp = ft_typed) and
  1285. (
  1286. (tfiledef(def_from).typedfiledef = tdef(voidtype)) or
  1287. (tfiledef(def_to).typedfiledef = tdef(voidtype))
  1288. )
  1289. ) then
  1290. begin
  1291. eq:=te_equal;
  1292. end;
  1293. end
  1294. else
  1295. if ((tfiledef(def_from).filetyp = ft_untyped) and
  1296. (tfiledef(def_to).filetyp = ft_typed)) or
  1297. ((tfiledef(def_from).filetyp = ft_typed) and
  1298. (tfiledef(def_to).filetyp = ft_untyped)) then
  1299. begin
  1300. doconv:=tc_equal;
  1301. eq:=te_convert_l1;
  1302. end;
  1303. end;
  1304. end;
  1305. recorddef :
  1306. begin
  1307. { interface -> guid }
  1308. if is_interface(def_from) and
  1309. (def_to=rec_tguid) then
  1310. begin
  1311. doconv:=tc_intf_2_guid;
  1312. eq:=te_convert_l1;
  1313. end;
  1314. end;
  1315. formaldef :
  1316. begin
  1317. doconv:=tc_equal;
  1318. if (def_from.typ=formaldef) then
  1319. eq:=te_equal
  1320. else
  1321. { Just about everything can be converted to a formaldef...}
  1322. if not (def_from.typ in [abstractdef,errordef]) then
  1323. eq:=te_convert_l2;
  1324. end;
  1325. end;
  1326. { if we didn't find an appropriate type conversion yet
  1327. then we search also the := operator }
  1328. if (eq=te_incompatible) and
  1329. { make sure there is not a single variant if variants }
  1330. { are not allowed (otherwise if only cdo_check_operator }
  1331. { and e.g. fromdef=stringdef and todef=variantdef, then }
  1332. { the test will still succeed }
  1333. ((cdo_allow_variant in cdoptions) or
  1334. ((def_from.typ<>variantdef) and (def_to.typ<>variantdef))
  1335. ) and
  1336. (
  1337. { Check for variants? }
  1338. (
  1339. (cdo_allow_variant in cdoptions) and
  1340. ((def_from.typ=variantdef) or (def_to.typ=variantdef))
  1341. ) or
  1342. { Check for operators? }
  1343. (
  1344. (cdo_check_operator in cdoptions) and
  1345. ((def_from.typ in [objectdef,recorddef,arraydef,stringdef]) or
  1346. (def_to.typ in [objectdef,recorddef,arraydef,stringdef]))
  1347. )
  1348. ) then
  1349. begin
  1350. operatorpd:=search_assignment_operator(def_from,def_to);
  1351. if assigned(operatorpd) then
  1352. eq:=te_convert_operator;
  1353. end;
  1354. { update convtype for te_equal when it is not yet set }
  1355. if (eq=te_equal) and
  1356. (doconv=tc_not_possible) then
  1357. doconv:=tc_equal;
  1358. compare_defs_ext:=eq;
  1359. end;
  1360. function equal_defs(def_from,def_to:tdef):boolean;
  1361. var
  1362. convtyp : tconverttype;
  1363. pd : tprocdef;
  1364. begin
  1365. { Compare defs with nothingn and no explicit typecasts and
  1366. searching for overloaded operators is not needed }
  1367. equal_defs:=(compare_defs_ext(def_from,def_to,nothingn,convtyp,pd,[])>=te_equal);
  1368. end;
  1369. function compare_defs(def_from,def_to:tdef;fromtreetype:tnodetype):tequaltype;
  1370. var
  1371. doconv : tconverttype;
  1372. pd : tprocdef;
  1373. begin
  1374. compare_defs:=compare_defs_ext(def_from,def_to,fromtreetype,doconv,pd,[cdo_check_operator,cdo_allow_variant]);
  1375. end;
  1376. function is_subequal(def1, def2: tdef): boolean;
  1377. var
  1378. basedef1,basedef2 : tenumdef;
  1379. Begin
  1380. is_subequal := false;
  1381. if assigned(def1) and assigned(def2) then
  1382. Begin
  1383. if (def1.typ = orddef) and (def2.typ = orddef) then
  1384. Begin
  1385. { see p.47 of Turbo Pascal 7.01 manual for the separation of types }
  1386. { range checking for case statements is done with testrange }
  1387. case torddef(def1).ordtype of
  1388. u8bit,u16bit,u32bit,u64bit,
  1389. s8bit,s16bit,s32bit,s64bit :
  1390. is_subequal:=(torddef(def2).ordtype in [s64bit,u64bit,s32bit,u32bit,u8bit,s8bit,s16bit,u16bit]);
  1391. bool8bit,bool16bit,bool32bit,bool64bit :
  1392. is_subequal:=(torddef(def2).ordtype in [bool8bit,bool16bit,bool32bit,bool64bit]);
  1393. uchar :
  1394. is_subequal:=(torddef(def2).ordtype=uchar);
  1395. uwidechar :
  1396. is_subequal:=(torddef(def2).ordtype=uwidechar);
  1397. end;
  1398. end
  1399. else
  1400. Begin
  1401. { Check if both basedefs are equal }
  1402. if (def1.typ=enumdef) and (def2.typ=enumdef) then
  1403. Begin
  1404. { get both basedefs }
  1405. basedef1:=tenumdef(def1);
  1406. while assigned(basedef1.basedef) do
  1407. basedef1:=basedef1.basedef;
  1408. basedef2:=tenumdef(def2);
  1409. while assigned(basedef2.basedef) do
  1410. basedef2:=basedef2.basedef;
  1411. is_subequal:=(basedef1=basedef2);
  1412. end;
  1413. end;
  1414. end;
  1415. end;
  1416. function compare_paras(para1,para2 : TFPObjectList; acp : tcompare_paras_type; cpoptions: tcompare_paras_options):tequaltype;
  1417. var
  1418. currpara1,
  1419. currpara2 : tparavarsym;
  1420. eq,lowesteq : tequaltype;
  1421. hpd : tprocdef;
  1422. convtype : tconverttype;
  1423. cdoptions : tcompare_defs_options;
  1424. i1,i2 : byte;
  1425. begin
  1426. compare_paras:=te_incompatible;
  1427. cdoptions:=[cdo_parameter,cdo_check_operator,cdo_allow_variant];
  1428. { we need to parse the list from left-right so the
  1429. not-default parameters are checked first }
  1430. lowesteq:=high(tequaltype);
  1431. i1:=0;
  1432. i2:=0;
  1433. if cpo_ignorehidden in cpoptions then
  1434. begin
  1435. while (i1<para1.count) and
  1436. (vo_is_hidden_para in tparavarsym(para1[i1]).varoptions) do
  1437. inc(i1);
  1438. while (i2<para2.count) and
  1439. (vo_is_hidden_para in tparavarsym(para2[i2]).varoptions) do
  1440. inc(i2);
  1441. end;
  1442. while (i1<para1.count) and (i2<para2.count) do
  1443. begin
  1444. eq:=te_incompatible;
  1445. currpara1:=tparavarsym(para1[i1]);
  1446. currpara2:=tparavarsym(para2[i2]);
  1447. { Unique types must match exact }
  1448. if ((df_unique in currpara1.vardef.defoptions) or (df_unique in currpara2.vardef.defoptions)) and
  1449. (currpara1.vardef<>currpara2.vardef) then
  1450. exit;
  1451. { Handle hidden parameters separately, because self is
  1452. defined as voidpointer for methodpointers }
  1453. if (vo_is_hidden_para in currpara1.varoptions) or
  1454. (vo_is_hidden_para in currpara2.varoptions) then
  1455. begin
  1456. { both must be hidden }
  1457. if (vo_is_hidden_para in currpara1.varoptions)<>(vo_is_hidden_para in currpara2.varoptions) then
  1458. exit;
  1459. eq:=te_exact;
  1460. if not(vo_is_self in currpara1.varoptions) and
  1461. not(vo_is_self in currpara2.varoptions) then
  1462. begin
  1463. if (currpara1.varspez<>currpara2.varspez) then
  1464. exit;
  1465. eq:=compare_defs_ext(currpara1.vardef,currpara2.vardef,nothingn,
  1466. convtype,hpd,cdoptions);
  1467. end;
  1468. end
  1469. else
  1470. begin
  1471. case acp of
  1472. cp_value_equal_const :
  1473. begin
  1474. if (
  1475. (currpara1.varspez<>currpara2.varspez) and
  1476. ((currpara1.varspez in [vs_var,vs_out]) or
  1477. (currpara2.varspez in [vs_var,vs_out]))
  1478. ) then
  1479. exit;
  1480. eq:=compare_defs_ext(currpara1.vardef,currpara2.vardef,nothingn,
  1481. convtype,hpd,cdoptions);
  1482. end;
  1483. cp_all :
  1484. begin
  1485. if (currpara1.varspez<>currpara2.varspez) then
  1486. exit;
  1487. eq:=compare_defs_ext(currpara1.vardef,currpara2.vardef,nothingn,
  1488. convtype,hpd,cdoptions);
  1489. end;
  1490. cp_procvar :
  1491. begin
  1492. if (currpara1.varspez<>currpara2.varspez) then
  1493. exit;
  1494. eq:=compare_defs_ext(currpara1.vardef,currpara2.vardef,nothingn,
  1495. convtype,hpd,cdoptions);
  1496. { Parameters must be at least equal otherwise the are incompatible }
  1497. if (eq<te_equal) then
  1498. eq:=te_incompatible;
  1499. end;
  1500. else
  1501. eq:=compare_defs_ext(currpara1.vardef,currpara2.vardef,nothingn,
  1502. convtype,hpd,cdoptions);
  1503. end;
  1504. end;
  1505. { check type }
  1506. if eq=te_incompatible then
  1507. exit;
  1508. { open strings can never match exactly, since you cannot define }
  1509. { a separate "open string" type -> we have to be able to }
  1510. { consider those as exact when resolving forward definitions. }
  1511. { The same goes for array of const. Open arrays are handled }
  1512. { already (if their element types match exactly, they are }
  1513. { considered to be an exact match) }
  1514. if (is_array_of_const(currpara1.vardef) or
  1515. is_open_string(currpara1.vardef)) and
  1516. (eq=te_equal) and
  1517. (cpo_openequalisexact in cpoptions) then
  1518. eq:=te_exact;
  1519. if eq<lowesteq then
  1520. lowesteq:=eq;
  1521. { also check default value if both have it declared }
  1522. if (cpo_comparedefaultvalue in cpoptions) and
  1523. assigned(currpara1.defaultconstsym) and
  1524. assigned(currpara2.defaultconstsym) then
  1525. begin
  1526. if not equal_constsym(tconstsym(currpara1.defaultconstsym),tconstsym(currpara2.defaultconstsym)) then
  1527. exit;
  1528. end;
  1529. inc(i1);
  1530. inc(i2);
  1531. if cpo_ignorehidden in cpoptions then
  1532. begin
  1533. while (i1<para1.count) and
  1534. (vo_is_hidden_para in tparavarsym(para1[i1]).varoptions) do
  1535. inc(i1);
  1536. while (i2<para2.count) and
  1537. (vo_is_hidden_para in tparavarsym(para2[i2]).varoptions) do
  1538. inc(i2);
  1539. end;
  1540. end;
  1541. { when both lists are empty then the parameters are equal. Also
  1542. when one list is empty and the other has a parameter with default
  1543. value assigned then the parameters are also equal }
  1544. if ((i1>=para1.count) and (i2>=para2.count)) or
  1545. ((cpo_allowdefaults in cpoptions) and
  1546. (((i1<para1.count) and assigned(tparavarsym(para1[i1]).defaultconstsym)) or
  1547. ((i2<para2.count) and assigned(tparavarsym(para2[i2]).defaultconstsym)))) then
  1548. compare_paras:=lowesteq;
  1549. end;
  1550. function proc_to_procvar_equal(def1:tabstractprocdef;def2:tprocvardef):tequaltype;
  1551. var
  1552. eq : tequaltype;
  1553. po_comp : tprocoptions;
  1554. begin
  1555. proc_to_procvar_equal:=te_incompatible;
  1556. if not(assigned(def1)) or not(assigned(def2)) then
  1557. exit;
  1558. { check for method pointer }
  1559. if (def1.is_methodpointer xor def2.is_methodpointer) or
  1560. (def1.is_addressonly xor def2.is_addressonly) then
  1561. exit;
  1562. { check return value and options, methodpointer is already checked }
  1563. po_comp:=[po_staticmethod,po_interrupt,
  1564. po_iocheck,po_varargs];
  1565. if (m_delphi in current_settings.modeswitches) then
  1566. exclude(po_comp,po_varargs);
  1567. if (def1.proccalloption=def2.proccalloption) and
  1568. ((po_comp * def1.procoptions)= (po_comp * def2.procoptions)) and
  1569. equal_defs(def1.returndef,def2.returndef) then
  1570. begin
  1571. { return equal type based on the parameters, but a proc->procvar
  1572. is never exact, so map an exact match of the parameters to
  1573. te_equal }
  1574. eq:=compare_paras(def1.paras,def2.paras,cp_procvar,[]);
  1575. if eq=te_exact then
  1576. eq:=te_equal;
  1577. proc_to_procvar_equal:=eq;
  1578. end;
  1579. end;
  1580. function compatible_childmethod_resultdef(parentretdef, childretdef: tdef): boolean;
  1581. begin
  1582. compatible_childmethod_resultdef :=
  1583. (equal_defs(parentretdef,childretdef)) or
  1584. ((parentretdef.typ=objectdef) and
  1585. (childretdef.typ=objectdef) and
  1586. is_class_or_interface(parentretdef) and
  1587. is_class_or_interface(childretdef) and
  1588. (tobjectdef(childretdef).is_related(tobjectdef(parentretdef))))
  1589. end;
  1590. end.