defcmp.pas 86 KB

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