defcmp.pas 98 KB

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