defcmp.pas 127 KB

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