defcmp.pas 133 KB

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