defcmp.pas 72 KB

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