defcmp.pas 71 KB

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