defcmp.pas 123 KB

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