defcmp.pas 86 KB

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