defcmp.pas 116 KB

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