defcmp.pas 70 KB

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