defcmp.pas 98 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181
  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. eq:=te_incompatible;
  1242. end
  1243. else
  1244. {$endif x86}
  1245. { the types can be forward type, handle before normal type check !! }
  1246. if assigned(def_to.typesym) and
  1247. ((tpointerdef(def_to).pointeddef.typ=forwarddef) or
  1248. (tpointerdef(def_from).pointeddef.typ=forwarddef)) then
  1249. begin
  1250. if (def_from.typesym=def_to.typesym) or
  1251. (fromtreetype=niln) then
  1252. eq:=te_equal
  1253. end
  1254. else
  1255. { same types }
  1256. if equal_defs(tpointerdef(def_from).pointeddef,tpointerdef(def_to).pointeddef) then
  1257. begin
  1258. eq:=te_equal
  1259. end
  1260. else
  1261. { child class pointer can be assigned to anchestor pointers }
  1262. if (
  1263. (tpointerdef(def_from).pointeddef.typ=objectdef) and
  1264. (tpointerdef(def_to).pointeddef.typ=objectdef) and
  1265. tobjectdef(tpointerdef(def_from).pointeddef).is_related(
  1266. tobjectdef(tpointerdef(def_to).pointeddef))
  1267. ) then
  1268. begin
  1269. doconv:=tc_equal;
  1270. eq:=te_convert_l1;
  1271. end
  1272. else
  1273. { all pointers can be assigned to void-pointer }
  1274. if is_void(tpointerdef(def_to).pointeddef) then
  1275. begin
  1276. doconv:=tc_equal;
  1277. { give pwidechar,pchar a penalty so it prefers
  1278. conversion to ansistring }
  1279. if is_pchar(def_from) or
  1280. is_pwidechar(def_from) then
  1281. eq:=te_convert_l2
  1282. else
  1283. eq:=te_convert_l1;
  1284. end
  1285. else
  1286. { all pointers can be assigned from void-pointer }
  1287. if is_void(tpointerdef(def_from).pointeddef) or
  1288. { all pointers can be assigned from void-pointer or formaldef pointer, check
  1289. tw3777.pp if you change this }
  1290. (tpointerdef(def_from).pointeddef.typ=formaldef) then
  1291. begin
  1292. doconv:=tc_equal;
  1293. { give pwidechar a penalty so it prefers
  1294. conversion to pchar }
  1295. if is_pwidechar(def_to) then
  1296. eq:=te_convert_l2
  1297. else
  1298. eq:=te_convert_l1;
  1299. end
  1300. { id = generic class instance. metaclasses are also
  1301. class instances themselves. }
  1302. else if ((def_from=objc_idtype) and
  1303. (def_to=objc_metaclasstype)) or
  1304. ((def_to=objc_idtype) and
  1305. (def_from=objc_metaclasstype)) then
  1306. begin
  1307. doconv:=tc_equal;
  1308. eq:=te_convert_l2;
  1309. end;
  1310. end;
  1311. procvardef :
  1312. begin
  1313. { procedure variable can be assigned to an void pointer,
  1314. this is not allowed for complex procvars }
  1315. if (is_void(tpointerdef(def_to).pointeddef) or
  1316. (m_mac_procvar in current_settings.modeswitches)) and
  1317. tprocvardef(def_from).is_addressonly then
  1318. begin
  1319. doconv:=tc_equal;
  1320. eq:=te_convert_l1;
  1321. end;
  1322. end;
  1323. procdef :
  1324. begin
  1325. { procedure variable can be assigned to an void pointer,
  1326. this not allowed for methodpointers }
  1327. if (m_mac_procvar in current_settings.modeswitches) and
  1328. tprocdef(def_from).is_addressonly then
  1329. begin
  1330. doconv:=tc_proc_2_procvar;
  1331. eq:=te_convert_l2;
  1332. end;
  1333. end;
  1334. classrefdef,
  1335. objectdef :
  1336. begin
  1337. { implicit pointer object and class reference types
  1338. can be assigned to void pointers, but it is less
  1339. preferred than assigning to a related objectdef }
  1340. if (
  1341. is_implicit_pointer_object_type(def_from) or
  1342. (def_from.typ=classrefdef)
  1343. ) and
  1344. (tpointerdef(def_to).pointeddef.typ=orddef) and
  1345. (torddef(tpointerdef(def_to).pointeddef).ordtype=uvoid) then
  1346. begin
  1347. doconv:=tc_equal;
  1348. eq:=te_convert_l2;
  1349. end
  1350. else if (is_objc_class_or_protocol(def_from) and
  1351. (def_to=objc_idtype)) or
  1352. { classrefs are also instances in Objective-C,
  1353. hence they're also assignment-cpmpatible with
  1354. id }
  1355. (is_objcclassref(def_from) and
  1356. ((def_to=objc_metaclasstype) or
  1357. (def_to=objc_idtype))) then
  1358. begin
  1359. doconv:=tc_equal;
  1360. eq:=te_convert_l2;
  1361. end;
  1362. end;
  1363. end;
  1364. end;
  1365. setdef :
  1366. begin
  1367. case def_from.typ of
  1368. setdef :
  1369. begin
  1370. if assigned(tsetdef(def_from).elementdef) and
  1371. assigned(tsetdef(def_to).elementdef) then
  1372. begin
  1373. { sets with the same size (packset setting), element
  1374. base type and the same range are equal }
  1375. if equal_defs(tsetdef(def_from).elementdef,tsetdef(def_to).elementdef) and
  1376. (tsetdef(def_from).setbase=tsetdef(def_to).setbase) and
  1377. (tsetdef(def_from).setmax=tsetdef(def_to).setmax) and
  1378. (def_from.size=def_to.size) then
  1379. eq:=te_equal
  1380. else if is_subequal(tsetdef(def_from).elementdef,tsetdef(def_to).elementdef) then
  1381. begin
  1382. eq:=te_convert_l1;
  1383. doconv:=tc_set_to_set;
  1384. end;
  1385. end
  1386. else
  1387. begin
  1388. { empty set is compatible with everything }
  1389. eq:=te_convert_l1;
  1390. doconv:=tc_set_to_set;
  1391. end;
  1392. end;
  1393. arraydef :
  1394. begin
  1395. { automatic arrayconstructor -> set conversion }
  1396. if is_array_constructor(def_from) then
  1397. begin
  1398. doconv:=tc_arrayconstructor_2_set;
  1399. eq:=te_convert_l1;
  1400. end;
  1401. end;
  1402. end;
  1403. end;
  1404. procvardef :
  1405. begin
  1406. case def_from.typ of
  1407. procdef :
  1408. begin
  1409. { proc -> procvar }
  1410. if (m_tp_procvar in current_settings.modeswitches) or
  1411. (m_mac_procvar in current_settings.modeswitches) then
  1412. begin
  1413. subeq:=proc_to_procvar_equal(tprocdef(def_from),tprocvardef(def_to),cdo_warn_incompatible_univ in cdoptions);
  1414. if subeq>te_incompatible then
  1415. begin
  1416. doconv:=tc_proc_2_procvar;
  1417. if subeq>te_convert_l5 then
  1418. eq:=pred(subeq)
  1419. else
  1420. eq:=subeq;
  1421. end;
  1422. end;
  1423. end;
  1424. procvardef :
  1425. begin
  1426. { procvar -> procvar }
  1427. eq:=proc_to_procvar_equal(tprocvardef(def_from),tprocvardef(def_to),cdo_warn_incompatible_univ in cdoptions);
  1428. end;
  1429. pointerdef :
  1430. begin
  1431. { nil is compatible with procvars }
  1432. if (fromtreetype=niln) then
  1433. begin
  1434. if not Tprocvardef(def_to).is_addressonly then
  1435. {Nil to method pointers requires to convert a single
  1436. pointer nil value to a two pointer procvardef.}
  1437. doconv:=tc_nil_2_methodprocvar
  1438. else
  1439. doconv:=tc_equal;
  1440. eq:=te_convert_l1;
  1441. end
  1442. else
  1443. { for example delphi allows the assignement from pointers }
  1444. { to procedure variables }
  1445. if (m_pointer_2_procedure in current_settings.modeswitches) and
  1446. is_void(tpointerdef(def_from).pointeddef) and
  1447. tprocvardef(def_to).is_addressonly then
  1448. begin
  1449. doconv:=tc_equal;
  1450. eq:=te_convert_l1;
  1451. end;
  1452. end;
  1453. end;
  1454. end;
  1455. objectdef :
  1456. begin
  1457. { object pascal objects }
  1458. if (def_from.typ=objectdef) and
  1459. (tobjectdef(def_from).is_related(tobjectdef(def_to))) then
  1460. begin
  1461. doconv:=tc_equal;
  1462. { also update in htypechk.pas/var_para_allowed if changed
  1463. here }
  1464. eq:=te_convert_l3;
  1465. end
  1466. { string -> java.lang.string }
  1467. else if (def_to=java_jlstring) and
  1468. ((def_from.typ=stringdef) or
  1469. (fromtreetype=stringconstn)) then
  1470. begin
  1471. if is_wide_or_unicode_string(def_from) or
  1472. ((fromtreetype=stringconstn) and
  1473. (cs_refcountedstrings in current_settings.localswitches) and
  1474. (m_default_unicodestring in current_settings.modeswitches)) then
  1475. begin
  1476. doconv:=tc_equal;
  1477. eq:=te_equal
  1478. end
  1479. else
  1480. begin
  1481. doconv:=tc_string_2_string;
  1482. eq:=te_convert_l2;
  1483. end;
  1484. end
  1485. else if (def_to=java_jlstring) and
  1486. is_anychar(def_from) then
  1487. begin
  1488. doconv:=tc_char_2_string;
  1489. eq:=te_convert_l2
  1490. end
  1491. else
  1492. { specific to implicit pointer object types }
  1493. if is_implicit_pointer_object_type(def_to) then
  1494. begin
  1495. { void pointer also for delphi mode }
  1496. if (m_delphi in current_settings.modeswitches) and
  1497. is_voidpointer(def_from) then
  1498. begin
  1499. doconv:=tc_equal;
  1500. { prefer pointer-pointer assignments }
  1501. eq:=te_convert_l2;
  1502. end
  1503. else
  1504. { nil is compatible with class instances and interfaces }
  1505. if (fromtreetype=niln) then
  1506. begin
  1507. doconv:=tc_equal;
  1508. eq:=te_convert_l1;
  1509. end
  1510. { All Objective-C classes are compatible with ID }
  1511. else if is_objc_class_or_protocol(def_to) and
  1512. (def_from=objc_idtype) then
  1513. begin
  1514. doconv:=tc_equal;
  1515. eq:=te_convert_l2;
  1516. end
  1517. { classes can be assigned to interfaces
  1518. (same with objcclass and objcprotocol) }
  1519. else if ((is_interface(def_to) and
  1520. is_class(def_from)) or
  1521. (is_objcprotocol(def_to) and
  1522. is_objcclass(def_from)) or
  1523. (is_javainterface(def_to) and
  1524. is_javaclass(def_from))) and
  1525. assigned(tobjectdef(def_from).ImplementedInterfaces) then
  1526. begin
  1527. { we've to search in parent classes as well }
  1528. hobjdef:=tobjectdef(def_from);
  1529. while assigned(hobjdef) do
  1530. begin
  1531. if hobjdef.find_implemented_interface(tobjectdef(def_to))<>nil then
  1532. begin
  1533. if is_interface(def_to) then
  1534. doconv:=tc_class_2_intf
  1535. else
  1536. { for Objective-C, we don't have to do anything special }
  1537. doconv:=tc_equal;
  1538. { don't prefer this over objectdef->objectdef }
  1539. eq:=te_convert_l2;
  1540. break;
  1541. end;
  1542. hobjdef:=hobjdef.childof;
  1543. end;
  1544. end
  1545. { Interface 2 GUID handling }
  1546. else if (def_to=tdef(rec_tguid)) and
  1547. (fromtreetype=typen) and
  1548. is_interface(def_from) and
  1549. assigned(tobjectdef(def_from).iidguid) then
  1550. begin
  1551. eq:=te_convert_l1;
  1552. doconv:=tc_equal;
  1553. end
  1554. else if (def_from.typ=variantdef) and is_interfacecom_or_dispinterface(def_to) then
  1555. begin
  1556. { corbainterfaces not accepted, until we have
  1557. runtime support for them in Variants (sergei) }
  1558. doconv:=tc_variant_2_interface;
  1559. eq:=te_convert_l2;
  1560. end
  1561. { ugly, but delphi allows it (enables typecasting ordinals/
  1562. enums of any size to pointer-based object defs) }
  1563. { in Java enums /are/ class instances, and hence such
  1564. typecasts must not be treated as integer-like conversions;
  1565. arbitrary constants cannot be converted into classes/
  1566. pointer-based values either on the JVM -> always return
  1567. false and let it be handled by the regular explicit type
  1568. casting code
  1569. }
  1570. else if (not(target_info.system in systems_jvm) and
  1571. ((def_from.typ=enumdef) or
  1572. (def_from.typ=orddef))) and
  1573. (m_delphi in current_settings.modeswitches) and
  1574. (cdo_explicit in cdoptions) then
  1575. begin
  1576. doconv:=tc_int_2_int;
  1577. eq:=te_convert_l1;
  1578. end;
  1579. end;
  1580. end;
  1581. classrefdef :
  1582. begin
  1583. { similar to pointerdef wrt forwards }
  1584. if assigned(def_to.typesym) and
  1585. (tclassrefdef(def_to).pointeddef.typ=forwarddef) or
  1586. ((def_from.typ=classrefdef) and
  1587. (tclassrefdef(def_from).pointeddef.typ=forwarddef)) then
  1588. begin
  1589. if (def_from.typesym=def_to.typesym) or
  1590. (fromtreetype=niln) then
  1591. eq:=te_equal;
  1592. end
  1593. else
  1594. { class reference types }
  1595. if (def_from.typ=classrefdef) then
  1596. begin
  1597. if equal_defs(tclassrefdef(def_from).pointeddef,tclassrefdef(def_to).pointeddef) then
  1598. begin
  1599. eq:=te_equal;
  1600. end
  1601. else
  1602. begin
  1603. doconv:=tc_equal;
  1604. if (cdo_explicit in cdoptions) or
  1605. tobjectdef(tclassrefdef(def_from).pointeddef).is_related(
  1606. tobjectdef(tclassrefdef(def_to).pointeddef)) then
  1607. eq:=te_convert_l1;
  1608. end;
  1609. end
  1610. else
  1611. if (m_delphi in current_settings.modeswitches) and
  1612. is_voidpointer(def_from) then
  1613. begin
  1614. doconv:=tc_equal;
  1615. { prefer pointer-pointer assignments }
  1616. eq:=te_convert_l2;
  1617. end
  1618. else
  1619. { nil is compatible with class references }
  1620. if (fromtreetype=niln) then
  1621. begin
  1622. doconv:=tc_equal;
  1623. eq:=te_convert_l1;
  1624. end
  1625. else
  1626. { id is compatible with all classref types }
  1627. if (def_from=objc_idtype) then
  1628. begin
  1629. doconv:=tc_equal;
  1630. eq:=te_convert_l1;
  1631. end;
  1632. end;
  1633. filedef :
  1634. begin
  1635. { typed files are all equal to the abstract file type
  1636. name TYPEDFILE in system.pp in is_equal in types.pas
  1637. the problem is that it sholud be also compatible to FILE
  1638. but this would leed to a problem for ASSIGN RESET and REWRITE
  1639. when trying to find the good overloaded function !!
  1640. so all file function are doubled in system.pp
  1641. this is not very beautiful !!}
  1642. if (def_from.typ=filedef) then
  1643. begin
  1644. if (tfiledef(def_from).filetyp=tfiledef(def_to).filetyp) then
  1645. begin
  1646. if
  1647. (
  1648. (tfiledef(def_from).typedfiledef=nil) and
  1649. (tfiledef(def_to).typedfiledef=nil)
  1650. ) or
  1651. (
  1652. (tfiledef(def_from).typedfiledef<>nil) and
  1653. (tfiledef(def_to).typedfiledef<>nil) and
  1654. equal_defs(tfiledef(def_from).typedfiledef,tfiledef(def_to).typedfiledef)
  1655. ) or
  1656. (
  1657. (tfiledef(def_from).filetyp = ft_typed) and
  1658. (tfiledef(def_to).filetyp = ft_typed) and
  1659. (
  1660. (tfiledef(def_from).typedfiledef = tdef(voidtype)) or
  1661. (tfiledef(def_to).typedfiledef = tdef(voidtype))
  1662. )
  1663. ) then
  1664. begin
  1665. eq:=te_equal;
  1666. end;
  1667. end
  1668. else
  1669. if ((tfiledef(def_from).filetyp = ft_untyped) and
  1670. (tfiledef(def_to).filetyp = ft_typed)) or
  1671. ((tfiledef(def_from).filetyp = ft_typed) and
  1672. (tfiledef(def_to).filetyp = ft_untyped)) then
  1673. begin
  1674. doconv:=tc_equal;
  1675. eq:=te_convert_l1;
  1676. end;
  1677. end;
  1678. end;
  1679. recorddef :
  1680. begin
  1681. { interface -> guid }
  1682. if (def_to=rec_tguid) and
  1683. (is_interfacecom_or_dispinterface(def_from)) then
  1684. begin
  1685. doconv:=tc_intf_2_guid;
  1686. eq:=te_convert_l1;
  1687. end;
  1688. end;
  1689. formaldef :
  1690. begin
  1691. doconv:=tc_equal;
  1692. if (def_from.typ=formaldef) then
  1693. eq:=te_equal
  1694. else
  1695. { Just about everything can be converted to a formaldef...}
  1696. if not (def_from.typ in [abstractdef,errordef]) then
  1697. eq:=te_convert_l2;
  1698. end;
  1699. end;
  1700. { if we didn't find an appropriate type conversion yet
  1701. then we search also the := operator }
  1702. if (eq=te_incompatible) and
  1703. { make sure there is not a single variant if variants }
  1704. { are not allowed (otherwise if only cdo_check_operator }
  1705. { and e.g. fromdef=stringdef and todef=variantdef, then }
  1706. { the test will still succeed }
  1707. ((cdo_allow_variant in cdoptions) or
  1708. ((def_from.typ<>variantdef) and
  1709. (def_to.typ<>variantdef) and
  1710. { internal typeconversions always have to be bitcasts (except for
  1711. variants) }
  1712. not(cdo_internal in cdoptions)
  1713. )
  1714. ) and
  1715. (
  1716. { Check for variants? }
  1717. (
  1718. (cdo_allow_variant in cdoptions) and
  1719. ((def_from.typ=variantdef) or (def_to.typ=variantdef))
  1720. ) or
  1721. { Check for operators? }
  1722. (
  1723. (cdo_check_operator in cdoptions) and
  1724. ((def_from.typ<>variantdef) or (def_to.typ<>variantdef))
  1725. )
  1726. ) then
  1727. begin
  1728. operatorpd:=search_assignment_operator(def_from,def_to,cdo_explicit in cdoptions);
  1729. if assigned(operatorpd) then
  1730. eq:=te_convert_operator;
  1731. end;
  1732. { update convtype for te_equal when it is not yet set }
  1733. if (eq=te_equal) and
  1734. (doconv=tc_not_possible) then
  1735. doconv:=tc_equal;
  1736. compare_defs_ext:=eq;
  1737. end;
  1738. function equal_defs(def_from,def_to:tdef):boolean;
  1739. var
  1740. convtyp : tconverttype;
  1741. pd : tprocdef;
  1742. begin
  1743. { Compare defs with nothingn and no explicit typecasts and
  1744. searching for overloaded operators is not needed }
  1745. equal_defs:=(compare_defs_ext(def_from,def_to,nothingn,convtyp,pd,[])>=te_equal);
  1746. end;
  1747. function compare_defs(def_from,def_to:tdef;fromtreetype:tnodetype):tequaltype;
  1748. var
  1749. doconv : tconverttype;
  1750. pd : tprocdef;
  1751. begin
  1752. compare_defs:=compare_defs_ext(def_from,def_to,fromtreetype,doconv,pd,[cdo_check_operator,cdo_allow_variant]);
  1753. end;
  1754. function is_subequal(def1, def2: tdef): boolean;
  1755. var
  1756. basedef1,basedef2 : tenumdef;
  1757. Begin
  1758. is_subequal := false;
  1759. if assigned(def1) and assigned(def2) then
  1760. Begin
  1761. if (def1.typ = orddef) and (def2.typ = orddef) then
  1762. Begin
  1763. { see p.47 of Turbo Pascal 7.01 manual for the separation of types }
  1764. { range checking for case statements is done with testrange }
  1765. case torddef(def1).ordtype of
  1766. u8bit,u16bit,u32bit,u64bit,
  1767. s8bit,s16bit,s32bit,s64bit :
  1768. is_subequal:=(torddef(def2).ordtype in [s64bit,u64bit,s32bit,u32bit,u8bit,s8bit,s16bit,u16bit]);
  1769. pasbool8,pasbool16,pasbool32,pasbool64,
  1770. bool8bit,bool16bit,bool32bit,bool64bit :
  1771. is_subequal:=(torddef(def2).ordtype in [pasbool8,pasbool16,pasbool32,pasbool64,bool8bit,bool16bit,bool32bit,bool64bit]);
  1772. uchar :
  1773. is_subequal:=(torddef(def2).ordtype=uchar);
  1774. uwidechar :
  1775. is_subequal:=(torddef(def2).ordtype=uwidechar);
  1776. end;
  1777. end
  1778. else
  1779. Begin
  1780. { Check if both basedefs are equal }
  1781. if (def1.typ=enumdef) and (def2.typ=enumdef) then
  1782. Begin
  1783. { get both basedefs }
  1784. basedef1:=tenumdef(def1);
  1785. while assigned(basedef1.basedef) do
  1786. basedef1:=basedef1.basedef;
  1787. basedef2:=tenumdef(def2);
  1788. while assigned(basedef2.basedef) do
  1789. basedef2:=basedef2.basedef;
  1790. is_subequal:=(basedef1=basedef2);
  1791. end;
  1792. end;
  1793. end;
  1794. end;
  1795. function potentially_incompatible_univ_paras(def1, def2: tdef): boolean;
  1796. begin
  1797. result :=
  1798. { not entirely safe: different records can be passed differently
  1799. depending on the types of their fields, but they're hard to compare
  1800. (variant records, bitpacked vs non-bitpacked) }
  1801. ((def1.typ in [floatdef,recorddef,arraydef,filedef,variantdef]) and
  1802. (def1.typ<>def2.typ)) or
  1803. { pointers, ordinals and small sets are all passed the same}
  1804. (((def1.typ in [orddef,enumdef,pointerdef,procvardef,classrefdef]) or
  1805. (is_class_or_interface_or_objc(def1)) or
  1806. is_dynamic_array(def1) or
  1807. is_smallset(def1) or
  1808. is_ansistring(def1) or
  1809. is_unicodestring(def1)) <>
  1810. (def2.typ in [orddef,enumdef,pointerdef,procvardef,classrefdef]) or
  1811. (is_class_or_interface_or_objc(def2)) or
  1812. is_dynamic_array(def2) or
  1813. is_smallset(def2) or
  1814. is_ansistring(def2) or
  1815. is_unicodestring(def2)) or
  1816. { shortstrings }
  1817. (is_shortstring(def1)<>
  1818. is_shortstring(def2)) or
  1819. { winlike widestrings }
  1820. (is_widestring(def1)<>
  1821. is_widestring(def2)) or
  1822. { TP-style objects }
  1823. (is_object(def1) <>
  1824. is_object(def2));
  1825. end;
  1826. function compare_paras(para1,para2 : TFPObjectList; acp : tcompare_paras_type; cpoptions: tcompare_paras_options):tequaltype;
  1827. var
  1828. currpara1,
  1829. currpara2 : tparavarsym;
  1830. eq,lowesteq : tequaltype;
  1831. hpd : tprocdef;
  1832. convtype : tconverttype;
  1833. cdoptions : tcompare_defs_options;
  1834. i1,i2 : byte;
  1835. begin
  1836. compare_paras:=te_incompatible;
  1837. cdoptions:=[cdo_parameter,cdo_check_operator,cdo_allow_variant,cdo_strict_undefined_check];
  1838. { we need to parse the list from left-right so the
  1839. not-default parameters are checked first }
  1840. lowesteq:=high(tequaltype);
  1841. i1:=0;
  1842. i2:=0;
  1843. if cpo_ignorehidden in cpoptions then
  1844. begin
  1845. while (i1<para1.count) and
  1846. (vo_is_hidden_para in tparavarsym(para1[i1]).varoptions) do
  1847. inc(i1);
  1848. while (i2<para2.count) and
  1849. (vo_is_hidden_para in tparavarsym(para2[i2]).varoptions) do
  1850. inc(i2);
  1851. end;
  1852. if cpo_ignoreframepointer in cpoptions then
  1853. begin
  1854. if (i1<para1.count) and
  1855. (vo_is_parentfp in tparavarsym(para1[i1]).varoptions) then
  1856. inc(i1);
  1857. if (i2<para2.count) and
  1858. (vo_is_parentfp in tparavarsym(para2[i2]).varoptions) then
  1859. inc(i2);
  1860. end;
  1861. while (i1<para1.count) and (i2<para2.count) do
  1862. begin
  1863. eq:=te_incompatible;
  1864. currpara1:=tparavarsym(para1[i1]);
  1865. currpara2:=tparavarsym(para2[i2]);
  1866. { Unique types must match exact }
  1867. if ((df_unique in currpara1.vardef.defoptions) or (df_unique in currpara2.vardef.defoptions)) and
  1868. (currpara1.vardef<>currpara2.vardef) then
  1869. exit;
  1870. { Handle hidden parameters separately, because self is
  1871. defined as voidpointer for methodpointers }
  1872. if (vo_is_hidden_para in currpara1.varoptions) or
  1873. (vo_is_hidden_para in currpara2.varoptions) then
  1874. begin
  1875. { both must be hidden }
  1876. if (vo_is_hidden_para in currpara1.varoptions)<>(vo_is_hidden_para in currpara2.varoptions) then
  1877. exit;
  1878. eq:=te_exact;
  1879. if not(vo_is_self in currpara1.varoptions) and
  1880. not(vo_is_self in currpara2.varoptions) then
  1881. begin
  1882. if not(cpo_ignorevarspez in cpoptions) and
  1883. (currpara1.varspez<>currpara2.varspez) then
  1884. exit;
  1885. eq:=compare_defs_ext(currpara1.vardef,currpara2.vardef,nothingn,
  1886. convtype,hpd,cdoptions);
  1887. end;
  1888. end
  1889. else
  1890. begin
  1891. case acp of
  1892. cp_value_equal_const :
  1893. begin
  1894. { this one is used for matching parameters from a call
  1895. statement to a procdef -> univ state can't be equal
  1896. in any case since the call statement does not contain
  1897. any information about that }
  1898. if (
  1899. not(cpo_ignorevarspez in cpoptions) and
  1900. (currpara1.varspez<>currpara2.varspez) and
  1901. ((currpara1.varspez in [vs_var,vs_out,vs_constref]) or
  1902. (currpara2.varspez in [vs_var,vs_out,vs_constref]))
  1903. ) then
  1904. exit;
  1905. eq:=compare_defs_ext(currpara1.vardef,currpara2.vardef,nothingn,
  1906. convtype,hpd,cdoptions);
  1907. end;
  1908. cp_all :
  1909. begin
  1910. { used to resolve forward definitions -> headers must
  1911. match exactly, including the "univ" specifier }
  1912. if (not(cpo_ignorevarspez in cpoptions) and
  1913. (currpara1.varspez<>currpara2.varspez)) or
  1914. (currpara1.univpara<>currpara2.univpara) then
  1915. exit;
  1916. eq:=compare_defs_ext(currpara1.vardef,currpara2.vardef,nothingn,
  1917. convtype,hpd,cdoptions);
  1918. end;
  1919. cp_procvar :
  1920. begin
  1921. if not(cpo_ignorevarspez in cpoptions) and
  1922. (currpara1.varspez<>currpara2.varspez) then
  1923. exit;
  1924. { "univ" state doesn't matter here: from univ to non-univ
  1925. matches if the types are compatible (i.e., as usual),
  1926. from from non-univ to univ also matches if the types
  1927. have the same size (checked below) }
  1928. eq:=compare_defs_ext(currpara1.vardef,currpara2.vardef,nothingn,
  1929. convtype,hpd,cdoptions);
  1930. { Parameters must be at least equal otherwise the are incompatible }
  1931. if (eq<te_equal) then
  1932. eq:=te_incompatible;
  1933. end;
  1934. else
  1935. eq:=compare_defs_ext(currpara1.vardef,currpara2.vardef,nothingn,
  1936. convtype,hpd,cdoptions);
  1937. end;
  1938. end;
  1939. { check type }
  1940. if eq=te_incompatible then
  1941. begin
  1942. { special case: "univ" parameters match if their size is equal }
  1943. if not(cpo_ignoreuniv in cpoptions) and
  1944. currpara2.univpara and
  1945. is_valid_univ_para_type(currpara1.vardef) and
  1946. (currpara1.vardef.size=currpara2.vardef.size) then
  1947. begin
  1948. { only pick as last choice }
  1949. eq:=te_convert_l5;
  1950. if (acp=cp_procvar) and
  1951. (cpo_warn_incompatible_univ in cpoptions) then
  1952. begin
  1953. { if the types may be passed in different ways by the
  1954. calling convention then this can lead to crashes
  1955. (note: not an exhaustive check, and failing this
  1956. this check does not mean things will crash on all
  1957. platforms) }
  1958. if potentially_incompatible_univ_paras(currpara1.vardef,currpara2.vardef) then
  1959. Message2(type_w_procvar_univ_conflicting_para,currpara1.vardef.typename,currpara2.vardef.typename)
  1960. end;
  1961. end
  1962. else
  1963. exit;
  1964. end;
  1965. { open strings can never match exactly, since you cannot define }
  1966. { a separate "open string" type -> we have to be able to }
  1967. { consider those as exact when resolving forward definitions. }
  1968. { The same goes for array of const. Open arrays are handled }
  1969. { already (if their element types match exactly, they are }
  1970. { considered to be an exact match) }
  1971. { And also for "inline defined" function parameter definitions }
  1972. { (i.e., function types directly declared in a parameter list) }
  1973. if (is_array_of_const(currpara1.vardef) or
  1974. is_open_string(currpara1.vardef) or
  1975. ((currpara1.vardef.typ = procvardef) and
  1976. not(assigned(currpara1.vardef.typesym)))) and
  1977. (eq=te_equal) and
  1978. (cpo_openequalisexact in cpoptions) then
  1979. eq:=te_exact;
  1980. if eq<lowesteq then
  1981. lowesteq:=eq;
  1982. { also check default value if both have it declared }
  1983. if (cpo_comparedefaultvalue in cpoptions) then
  1984. begin
  1985. if assigned(currpara1.defaultconstsym) and
  1986. assigned(currpara2.defaultconstsym) then
  1987. begin
  1988. if not equal_constsym(tconstsym(currpara1.defaultconstsym),tconstsym(currpara2.defaultconstsym)) then
  1989. exit;
  1990. end
  1991. { cannot have that the second (= implementation) has a default value declared and the
  1992. other (interface) doesn't }
  1993. else if not assigned(currpara1.defaultconstsym) and assigned(currpara2.defaultconstsym) then
  1994. exit;
  1995. end;
  1996. if not(cpo_compilerproc in cpoptions) and
  1997. not(cpo_rtlproc in cpoptions) and
  1998. is_ansistring(currpara1.vardef) and
  1999. is_ansistring(currpara2.vardef) and
  2000. (tstringdef(currpara1.vardef).encoding<>tstringdef(currpara2.vardef).encoding) and
  2001. ((tstringdef(currpara1.vardef).encoding=globals.CP_NONE) or
  2002. (tstringdef(currpara2.vardef).encoding=globals.CP_NONE)
  2003. ) then
  2004. eq:=te_convert_l1;
  2005. if eq<lowesteq then
  2006. lowesteq:=eq;
  2007. inc(i1);
  2008. inc(i2);
  2009. if cpo_ignorehidden in cpoptions then
  2010. begin
  2011. while (i1<para1.count) and
  2012. (vo_is_hidden_para in tparavarsym(para1[i1]).varoptions) do
  2013. inc(i1);
  2014. while (i2<para2.count) and
  2015. (vo_is_hidden_para in tparavarsym(para2[i2]).varoptions) do
  2016. inc(i2);
  2017. end;
  2018. if cpo_ignoreframepointer in cpoptions then
  2019. begin
  2020. if (i1<para1.count) and
  2021. (vo_is_parentfp in tparavarsym(para1[i1]).varoptions) then
  2022. inc(i1);
  2023. if (i2<para2.count) and
  2024. (vo_is_parentfp in tparavarsym(para2[i2]).varoptions) then
  2025. inc(i2);
  2026. end;
  2027. end;
  2028. { when both lists are empty then the parameters are equal. Also
  2029. when one list is empty and the other has a parameter with default
  2030. value assigned then the parameters are also equal }
  2031. if ((i1>=para1.count) and (i2>=para2.count)) or
  2032. ((cpo_allowdefaults in cpoptions) and
  2033. (((i1<para1.count) and assigned(tparavarsym(para1[i1]).defaultconstsym)) or
  2034. ((i2<para2.count) and assigned(tparavarsym(para2[i2]).defaultconstsym)))) then
  2035. compare_paras:=lowesteq;
  2036. end;
  2037. function proc_to_procvar_equal(def1:tabstractprocdef;def2:tprocvardef;checkincompatibleuniv: boolean):tequaltype;
  2038. var
  2039. eq : tequaltype;
  2040. po_comp : tprocoptions;
  2041. pa_comp: tcompare_paras_options;
  2042. begin
  2043. proc_to_procvar_equal:=te_incompatible;
  2044. if not(assigned(def1)) or not(assigned(def2)) then
  2045. exit;
  2046. { check for method pointer and local procedure pointer:
  2047. a) if one is a procedure of object, the other also has to be one
  2048. b) if one is a pure address, the other also has to be one
  2049. except if def1 is a global proc and def2 is a nested procdef
  2050. (global procedures can be converted into nested procvars)
  2051. c) if def1 is a nested procedure, then def2 has to be a nested
  2052. procvar and def1 has to have the po_delphi_nested_cc option
  2053. d) if def1 is a procvar, def1 and def2 both have to be nested or
  2054. non-nested (we don't allow assignments from non-nested to
  2055. nested procvars to make sure that we can still implement
  2056. nested procvars using trampolines -- e.g., this would be
  2057. necessary for LLVM or CIL as long as they do not have support
  2058. for Delphi-style frame pointer parameter passing) }
  2059. if (def1.is_methodpointer<>def2.is_methodpointer) or { a) }
  2060. ((def1.is_addressonly<>def2.is_addressonly) and { b) }
  2061. (is_nested_pd(def1) or
  2062. not is_nested_pd(def2))) or
  2063. ((def1.typ=procdef) and { c) }
  2064. is_nested_pd(def1) and
  2065. (not(po_delphi_nested_cc in def1.procoptions) or
  2066. not is_nested_pd(def2))) or
  2067. ((def1.typ=procvardef) and { d) }
  2068. (is_nested_pd(def1)<>is_nested_pd(def2))) then
  2069. exit;
  2070. pa_comp:=[cpo_ignoreframepointer];
  2071. if checkincompatibleuniv then
  2072. include(pa_comp,cpo_warn_incompatible_univ);
  2073. { check return value and options, methodpointer is already checked }
  2074. po_comp:=[po_staticmethod,po_interrupt,
  2075. po_iocheck,po_varargs];
  2076. if (m_delphi in current_settings.modeswitches) then
  2077. exclude(po_comp,po_varargs);
  2078. if (def1.proccalloption=def2.proccalloption) and
  2079. ((po_comp * def1.procoptions)= (po_comp * def2.procoptions)) and
  2080. equal_defs(def1.returndef,def2.returndef) then
  2081. begin
  2082. { return equal type based on the parameters, but a proc->procvar
  2083. is never exact, so map an exact match of the parameters to
  2084. te_equal }
  2085. eq:=compare_paras(def1.paras,def2.paras,cp_procvar,pa_comp);
  2086. if eq=te_exact then
  2087. eq:=te_equal;
  2088. if (eq=te_equal) then
  2089. begin
  2090. { prefer non-nested to non-nested over non-nested to nested }
  2091. if (is_nested_pd(def1)<>is_nested_pd(def2)) then
  2092. eq:=te_convert_l1;
  2093. end;
  2094. proc_to_procvar_equal:=eq;
  2095. end;
  2096. end;
  2097. function compatible_childmethod_resultdef(parentretdef, childretdef: tdef): boolean;
  2098. begin
  2099. compatible_childmethod_resultdef :=
  2100. (equal_defs(parentretdef,childretdef)) or
  2101. ((parentretdef.typ=objectdef) and
  2102. (childretdef.typ=objectdef) and
  2103. is_class_or_interface_or_objc_or_java(parentretdef) and
  2104. is_class_or_interface_or_objc_or_java(childretdef) and
  2105. (tobjectdef(childretdef).is_related(tobjectdef(parentretdef))))
  2106. end;
  2107. end.