defcmp.pas 96 KB

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