defcmp.pas 93 KB

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