defcmp.pas 89 KB

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