defcmp.pas 65 KB

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