defcmp.pas 63 KB

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