defcmp.pas 73 KB

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