defcmp.pas 87 KB

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