defcmp.pas 88 KB

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