defcmp.pas 65 KB

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