htypechk.pas 162 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. This unit exports some help routines for the type checking
  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 htypechk;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. cclasses,cmsgs,tokens,
  22. node,globtype,compinnr,
  23. symconst,symtype,symdef,symsym,symbase,
  24. pgentype;
  25. type
  26. Ttok2nodeRec=record
  27. tok : ttoken;
  28. nod : tnodetype;
  29. inr : tinlinenumber;
  30. op_overloading_supported : boolean;
  31. minargs : longint;
  32. maxargs : longint;
  33. end;
  34. Ttok2opRec=record
  35. tok : ttoken;
  36. managementoperator : tmanagementoperator;
  37. end;
  38. pcandidate = ^tcandidate;
  39. tcandidate = record
  40. next : pcandidate;
  41. data : tprocdef;
  42. wrongparaidx,
  43. firstparaidx : integer;
  44. exact_count,
  45. equal_count,
  46. cl1_count,
  47. cl2_count,
  48. cl3_count,
  49. cl4_count,
  50. cl5_count,
  51. cl6_count,
  52. coper_count : integer; { should be signed }
  53. ordinal_distance : double;
  54. invalid : boolean;
  55. {$ifndef DISABLE_FAST_OVERLOAD_PATCH}
  56. saved_validity : boolean;
  57. {$endif}
  58. wrongparanr : byte;
  59. end;
  60. tcallcandidatesflag =
  61. (
  62. cc_ignorevisibility,cc_allowdefaultparas,cc_objcidcall,cc_explicitunit,cc_searchhelpers,cc_anoninherited
  63. );
  64. tcallcandidatesflags = set of tcallcandidatesflag;
  65. tcallcandidates = object
  66. private
  67. FProcsym : tprocsym;
  68. FProcsymtable : tsymtable;
  69. FOperator : ttoken;
  70. FCandidateProcs : pcandidate;
  71. FIgnoredCandidateProcs : tfplist;
  72. FProcCnt : integer;
  73. FParaNode : tnode;
  74. FParaLength : smallint;
  75. FAllowVariant : boolean;
  76. FParaAnonSyms : tfplist;
  77. procedure collect_overloads_in_struct(structdef:tabstractrecorddef;ProcdefOverloadList:TFPObjectList;flags:tcallcandidatesflags;spezcontext:tspecializationcontext);
  78. procedure collect_overloads_in_units(ProcdefOverloadList:TFPObjectList; flags:tcallcandidatesflags;spezcontext:tspecializationcontext);
  79. procedure create_candidate_list(flags:tcallcandidatesflags;spezcontext:tspecializationcontext);
  80. procedure calc_distance(st_root:tsymtable;flags:tcallcandidatesflags);
  81. function proc_add(st:tsymtable;pd:tprocdef):pcandidate;
  82. public
  83. constructor init(sym:tprocsym;st:TSymtable;ppn:tnode;flags:tcallcandidatesflags;spezcontext:tspecializationcontext);
  84. constructor init_operator(op:ttoken;ppn:tnode);
  85. destructor done;
  86. procedure list(all:boolean);
  87. {$ifdef EXTDEBUG}
  88. procedure dump_info(lvl:longint);
  89. {$endif EXTDEBUG}
  90. procedure get_information;
  91. function choose_best(var bestpd:tabstractprocdef; singlevariant: boolean):integer;
  92. procedure find_wrong_para;
  93. property Count:integer read FProcCnt;
  94. { list of symbols for anonymous types required for implicit specializations;
  95. these should be handed over to the picked procdef, otherwise they'll be
  96. freed by tcallcandidates }
  97. property para_anon_syms:tfplist read FParaAnonSyms;
  98. end;
  99. type
  100. tregableinfoflag = (
  101. // can be put in a register if it's the address of a var/out/const parameter
  102. ra_addr_regable,
  103. { orthogonal to above flag: the address of the node is taken and may
  104. possibly escape the block in which this node is declared (e.g. a
  105. local variable is passed as var parameter to another procedure)
  106. }
  107. ra_addr_taken,
  108. { variable is accessed in a different scope }
  109. ra_different_scope);
  110. tregableinfoflags = set of tregableinfoflag;
  111. const
  112. tok2nodes=27;
  113. tok2node:array[1..tok2nodes] of ttok2noderec=(
  114. (tok:_PLUS ;nod:addn;inr:in_none;op_overloading_supported:true;minargs:1;maxargs:2), { binary overloading supported }
  115. (tok:_MINUS ;nod:subn;inr:in_none;op_overloading_supported:true;minargs:1;maxargs:2), { binary and unary overloading supported }
  116. (tok:_STAR ;nod:muln;inr:in_none;op_overloading_supported:true;minargs:2;maxargs:2), { binary overloading supported }
  117. (tok:_SLASH ;nod:slashn;inr:in_none;op_overloading_supported:true;minargs:2;maxargs:2), { binary overloading supported }
  118. (tok:_EQ ;nod:equaln;inr:in_none;op_overloading_supported:true;minargs:2;maxargs:2), { binary overloading supported }
  119. (tok:_GT ;nod:gtn;inr:in_none;op_overloading_supported:true;minargs:2;maxargs:2), { binary overloading supported }
  120. (tok:_LT ;nod:ltn;inr:in_none;op_overloading_supported:true;minargs:2;maxargs:2), { binary overloading supported }
  121. (tok:_GTE ;nod:gten;inr:in_none;op_overloading_supported:true;minargs:2;maxargs:2), { binary overloading supported }
  122. (tok:_LTE ;nod:lten;inr:in_none;op_overloading_supported:true;minargs:2;maxargs:2), { binary overloading supported }
  123. (tok:_SYMDIF ;nod:symdifn;inr:in_none;op_overloading_supported:true;minargs:2;maxargs:2), { binary overloading supported }
  124. (tok:_STARSTAR ;nod:starstarn;inr:in_none;op_overloading_supported:true;minargs:2;maxargs:2), { binary overloading supported }
  125. (tok:_OP_AS ;nod:asn;inr:in_none;op_overloading_supported:false;minargs:0;maxargs:0), { binary overloading NOT supported }
  126. (tok:_OP_IN ;nod:inn;inr:in_none;op_overloading_supported:true;minargs:2;maxargs:2), { binary overloading supported }
  127. (tok:_OP_IS ;nod:isn;inr:in_none;op_overloading_supported:false;minargs:0;maxargs:0), { binary overloading NOT supported }
  128. (tok:_OP_OR ;nod:orn;inr:in_none;op_overloading_supported:true;minargs:2;maxargs:2), { binary overloading supported }
  129. (tok:_OP_AND ;nod:andn;inr:in_none;op_overloading_supported:true;minargs:2;maxargs:2), { binary overloading supported }
  130. (tok:_OP_DIV ;nod:divn;inr:in_none;op_overloading_supported:true;minargs:2;maxargs:2), { binary overloading supported }
  131. (tok:_OP_NOT ;nod:notn;inr:in_none;op_overloading_supported:true;minargs:1;maxargs:1), { unary overloading supported }
  132. (tok:_OP_MOD ;nod:modn;inr:in_none;op_overloading_supported:true;minargs:2;maxargs:2), { binary overloading supported }
  133. (tok:_OP_SHL ;nod:shln;inr:in_none;op_overloading_supported:true;minargs:2;maxargs:2), { binary overloading supported }
  134. (tok:_OP_SHR ;nod:shrn;inr:in_none;op_overloading_supported:true;minargs:2;maxargs:2), { binary overloading supported }
  135. (tok:_OP_XOR ;nod:xorn;inr:in_none;op_overloading_supported:true;minargs:2;maxargs:2), { binary overloading supported }
  136. (tok:_ASSIGNMENT ;nod:assignn;inr:in_none;op_overloading_supported:true;minargs:1;maxargs:1), { unary overloading supported }
  137. (tok:_OP_EXPLICIT;nod:assignn;inr:in_none;op_overloading_supported:true;minargs:1;maxargs:1), { unary overloading supported }
  138. (tok:_NE ;nod:unequaln;inr:in_none;op_overloading_supported:true;minargs:2;maxargs:2), { binary overloading supported }
  139. (tok:_OP_INC ;nod:inlinen;inr:in_inc_x;op_overloading_supported:true;minargs:1;maxargs:1), { unary overloading supported }
  140. (tok:_OP_DEC ;nod:inlinen;inr:in_dec_x;op_overloading_supported:true;minargs:1;maxargs:1) { unary overloading supported }
  141. );
  142. tok2ops=4;
  143. tok2op: array[1..tok2ops] of ttok2oprec=(
  144. (tok:_OP_INITIALIZE; managementoperator: mop_initialize),
  145. (tok:_OP_FINALIZE ; managementoperator: mop_finalize),
  146. (tok:_OP_ADDREF ; managementoperator: mop_addref),
  147. (tok:_OP_COPY ; managementoperator: mop_copy)
  148. );
  149. function node2opstr(nt:tnodetype):string;
  150. function token2managementoperator(optoken:ttoken):tmanagementoperator;
  151. { check operator args and result type }
  152. type
  153. toverload_check_flag = (
  154. ocf_check_non_overloadable, { also check operators that are (currently) considered as
  155. not overloadable (e.g. the "+" operator for dynamic arrays
  156. if modeswitch arrayoperators is active) }
  157. ocf_check_only { only check whether the operator is overloaded, but don't
  158. modify the passed in node (return true if the operator is
  159. overloaded, false otherwise) }
  160. );
  161. toverload_check_flags = set of toverload_check_flag;
  162. function isbinaryoperatoroverloadable(treetyp:tnodetype;ld:tdef;lt:tnodetype;rd:tdef;rt:tnodetype) : boolean;
  163. function isoperatoracceptable(pf : tprocdef; optoken : ttoken) : boolean;
  164. function isunaryoverloaded(var t : tnode;ocf:toverload_check_flags) : boolean;
  165. function isbinaryoverloaded(var t : tnode;ocf:toverload_check_flags) : boolean;
  166. { Register Allocation }
  167. procedure make_not_regable(p : tnode; how: tregableinfoflags);
  168. { procvar handling }
  169. function is_proc2procvar_load(p:tnode;out realprocdef:tprocdef):boolean;
  170. { returns whether a node represents a load of the function result node via
  171. the function name (so it could also be a recursive call to the function
  172. in case there or no parameters, or the function could be passed as
  173. procvar }
  174. function is_ambiguous_funcret_load(p: tnode; out owningprocdef: tprocdef): boolean;
  175. procedure test_local_to_procvar(from_def:tprocvardef;to_def:tdef);
  176. { sets varsym varstate field correctly }
  177. type
  178. tvarstateflag = (vsf_must_be_valid,vsf_use_hints,vsf_use_hint_for_string_result);
  179. tvarstateflags = set of tvarstateflag;
  180. procedure set_varstate(p:tnode;newstate:tvarstate;varstateflags:tvarstateflags);
  181. { sets the callunique flag, if the node is a vecn, }
  182. { takes care of type casts etc. }
  183. procedure set_unique(p : tnode);
  184. function valid_for_formal_var(p : tnode; report_errors: boolean) : boolean;
  185. function valid_for_formal_constref(p : tnode; report_errors: boolean) : boolean;
  186. function valid_for_formal_const(p : tnode; report_errors: boolean) : boolean;
  187. function valid_for_var(p:tnode; report_errors: boolean):boolean;
  188. function valid_for_assignment(p:tnode; report_errors: boolean):boolean;
  189. function valid_for_loopvar(p:tnode; report_errors: boolean):boolean;
  190. function valid_for_addr(p : tnode; report_errors: boolean) : boolean;
  191. function allowenumop(nt:tnodetype):boolean;
  192. procedure check_ranges(const location: tfileposinfo; source: tnode; destdef: tdef);
  193. { returns whether the def may be used in the Default() intrinsic; static
  194. arrays, records and objects are checked recursively }
  195. function is_valid_for_default(def:tdef):boolean;
  196. procedure UninitializedVariableMessage(pos : tfileposinfo;warning,local,managed : boolean;name : TMsgStr);
  197. implementation
  198. uses
  199. systems,constexp,globals,
  200. cutils,verbose,
  201. symtable,symutil,
  202. defutil,defcmp,
  203. nbas,ncnv,nld,nmem,ncal,nmat,ninl,nutils,procinfo,
  204. pgenutil
  205. ;
  206. type
  207. TValidAssign=(Valid_Property,Valid_Void,Valid_Const,Valid_Addr,Valid_Packed,Valid_Range);
  208. TValidAssigns=set of TValidAssign;
  209. { keep these two in sync! }
  210. const
  211. non_commutative_op_tokens=[_OP_SHL,_OP_SHR,_OP_DIV,_OP_MOD,_STARSTAR,_SLASH,_MINUS];
  212. non_commutative_op_nodes=[shln,shrn,divn,modn,starstarn,slashn,subn];
  213. function node2opstr(nt:tnodetype):string;
  214. var
  215. i : integer;
  216. begin
  217. result:='<unknown>';
  218. for i:=1 to tok2nodes do
  219. if tok2node[i].nod=nt then
  220. begin
  221. result:=tokeninfo^[tok2node[i].tok].str;
  222. break;
  223. end;
  224. end;
  225. function token2managementoperator(optoken:ttoken):tmanagementoperator;
  226. var
  227. i : integer;
  228. begin
  229. result:=mop_none;
  230. for i:=1 to tok2ops do
  231. if tok2op[i].tok=optoken then
  232. begin
  233. result:=tok2op[i].managementoperator;
  234. break;
  235. end;
  236. end;
  237. function isbinaryoperatoroverloadable(treetyp:tnodetype;ld:tdef;lt:tnodetype;rd:tdef;rt:tnodetype) : boolean;
  238. function internal_check(treetyp:tnodetype;ld:tdef;lt:tnodetype;rd:tdef;rt:tnodetype;var allowed:boolean):boolean;
  239. const
  240. identity_operators=[equaln,unequaln];
  241. order_theoretic_operators=identity_operators+[ltn,lten,gtn,gten];
  242. arithmetic_operators=[addn,subn,muln,divn,modn];
  243. rational_operators=[addn,subn,muln,slashn];
  244. numerical_operators=arithmetic_operators+[slashn];
  245. pointer_arithmetic_operators=[addn,subn];
  246. logical_operators=[andn,orn,xorn];
  247. bit_manipulation_operators=logical_operators+[shln,shrn];
  248. set_set_operators=identity_operators+[addn,subn,muln,symdifn]+
  249. order_theoretic_operators;
  250. element_set_operators=[inn];
  251. string_comparison_operators=order_theoretic_operators;
  252. string_manipulation_operators=[addn];
  253. string_operators =
  254. string_comparison_operators+string_manipulation_operators;
  255. begin
  256. internal_check:=true;
  257. { Reject the cases permitted by the default interpretation (DI). }
  258. case ld.typ of
  259. formaldef,
  260. recorddef,
  261. variantdef :
  262. begin
  263. allowed:=true;
  264. end;
  265. enumdef:
  266. begin
  267. allowed:=not (
  268. (
  269. is_set(rd) and
  270. (treetyp in element_set_operators)
  271. ) or
  272. (
  273. is_enum(rd) and
  274. (treetyp in (order_theoretic_operators+[addn, subn]))
  275. ) or
  276. (
  277. { for enum definitions, see webtbs/tw22860.pp }
  278. is_integer(rd) and
  279. (treetyp in (order_theoretic_operators+bit_manipulation_operators+arithmetic_operators))
  280. )
  281. );
  282. end;
  283. setdef:
  284. begin
  285. allowed:=not (
  286. (
  287. is_set(rd) and
  288. (treetyp in (set_set_operators+identity_operators))
  289. ) or
  290. (
  291. { This clause is a hack but it’s due to a hack somewhere
  292. else---while set + element is not permitted by DI, it
  293. seems to be used when a set is constructed inline }
  294. (rd.typ in [enumdef,orddef]) and
  295. (treetyp=addn)
  296. )
  297. );
  298. end;
  299. orddef, floatdef:
  300. begin
  301. allowed:=not (
  302. (
  303. (rd.typ in [orddef,floatdef]) and
  304. (treetyp in order_theoretic_operators)
  305. ) or
  306. (
  307. (m_mac in current_settings.modeswitches) and
  308. is_stringlike(rd) and
  309. (ld.typ=orddef) and
  310. (treetyp in string_comparison_operators)) or
  311. { c.f. $(source)\tests\tmacpas5.pp }
  312. (
  313. (rd.typ=setdef) and
  314. (ld.typ=orddef) and
  315. (treetyp in element_set_operators)
  316. )
  317. { This clause may be too restrictive---not all types under
  318. orddef have a corresponding set type; despite this the
  319. restriction should be very unlikely to become
  320. a practical obstacle, and can be relaxed by simply
  321. adding an extra check on TOrdDef(rd).ordtype }
  322. );
  323. { Note that Currency can be under either orddef or floatdef;
  324. when it’s under floatdef, is_currency() implies is_float();
  325. when it’s under orddef, is_currency() does NOT imply
  326. is_integer(). }
  327. if allowed then
  328. begin
  329. if is_anychar(ld) then
  330. allowed:=not (
  331. is_stringlike(rd) and
  332. (treetyp in string_operators)
  333. )
  334. else if is_boolean(ld) then
  335. allowed:=not (
  336. is_boolean(rd) and
  337. (treetyp in logical_operators)
  338. )
  339. else if is_integer(ld) or
  340. (
  341. (ld.typ=orddef) and
  342. is_currency(ld)
  343. { Here ld is Currency but behaves like an integer }
  344. ) then
  345. allowed:=not (
  346. (
  347. (
  348. is_integer(rd) or
  349. (
  350. (rd.typ=orddef) and
  351. is_currency(rd)
  352. )
  353. ) and
  354. (treetyp in (bit_manipulation_operators+numerical_operators))
  355. ) or
  356. (
  357. is_fpu(rd) and
  358. (treetyp in rational_operators)
  359. ) or
  360. (
  361. { When an integer type is used as the first operand in
  362. pointer arithmetic, DI doesn’t accept minus as the
  363. operator (Currency can’t be used in pointer
  364. arithmetic even if it’s under orddef) }
  365. is_integer(ld) and
  366. (rd.typ=pointerdef) and
  367. (treetyp in pointer_arithmetic_operators-[subn])
  368. )
  369. )
  370. else { is_fpu(ld) = True }
  371. allowed:=not (
  372. (
  373. is_fpu(rd) or
  374. is_integer(rd) or
  375. is_currency(rd)
  376. ) and
  377. (treetyp in rational_operators)
  378. );
  379. end;
  380. end;
  381. procvardef :
  382. begin
  383. if (rd.typ in [pointerdef,procdef,procvardef]) then
  384. begin
  385. allowed:=false;
  386. exit;
  387. end;
  388. allowed:=true;
  389. end;
  390. pointerdef :
  391. begin
  392. { DI permits pointer arithmetic for pointer + pointer, pointer -
  393. integer, pointer - pointer, but not for pointer + pointer.
  394. The last case is only valid in DI when both sides are
  395. stringlike. }
  396. if is_stringlike(ld) then
  397. if is_stringlike(rd) then
  398. { DI in this case permits string operations and pointer
  399. arithmetic. }
  400. allowed:=not (treetyp in (string_operators+pointer_arithmetic_operators))
  401. else if rd.typ = pointerdef then
  402. { DI in this case permits minus for pointer arithmetic and
  403. order-theoretic operators for pointer comparison. }
  404. allowed:=not (
  405. treetyp in (
  406. pointer_arithmetic_operators-[addn]+
  407. order_theoretic_operators
  408. )
  409. )
  410. else if is_integer(rd) then
  411. { DI in this case permits pointer arithmetic. }
  412. allowed:=not (treetyp in pointer_arithmetic_operators)
  413. else
  414. allowed:=true
  415. else
  416. allowed:=not (
  417. (
  418. is_integer(rd) and
  419. (treetyp in pointer_arithmetic_operators)
  420. ) or
  421. (
  422. (rd.typ=pointerdef) and
  423. (
  424. treetyp in (
  425. pointer_arithmetic_operators-[addn]+
  426. order_theoretic_operators
  427. )
  428. )
  429. ) or
  430. (
  431. (lt=niln) and
  432. ((rd.typ in [procvardef,procdef,classrefdef]) or
  433. (is_dynamic_array(rd))) and
  434. (treetyp in identity_operators)
  435. ) or
  436. (
  437. is_implicit_pointer_object_type(rd) and
  438. (treetyp in identity_operators)
  439. )
  440. );
  441. end;
  442. arraydef :
  443. begin
  444. { not vector/mmx }
  445. if ((cs_mmx in current_settings.localswitches) and
  446. is_mmx_able_array(ld)) or
  447. ((cs_support_vectors in current_settings.globalswitches) and
  448. is_vector(ld)) then
  449. begin
  450. allowed:=false;
  451. exit;
  452. end;
  453. if is_stringlike(ld) and
  454. (
  455. (
  456. (
  457. is_stringlike(rd) or
  458. (rt = niln)
  459. ) and
  460. (treetyp in string_operators)
  461. ) or
  462. (
  463. is_integer(rd) and
  464. (treetyp in pointer_arithmetic_operators)
  465. ) or
  466. (
  467. (
  468. is_pchar(rd) or
  469. is_pwidechar(rd)) and
  470. (treetyp in pointer_arithmetic_operators) and
  471. (tpointerdef(rd).pointeddef=tarraydef(ld).elementdef
  472. )
  473. )
  474. ) then
  475. begin
  476. allowed:=false;
  477. exit;
  478. end;
  479. { dynamic array compare with niln }
  480. if is_dynamic_array(ld) and
  481. (treetyp in identity_operators) then
  482. if is_dynamic_array(rd) or
  483. (rt=niln) then
  484. begin
  485. allowed:=false;
  486. exit;
  487. end;
  488. { <dyn. array> + <dyn. array> is handled by the compiler }
  489. if (m_array_operators in current_settings.modeswitches) and
  490. (treetyp=addn) and
  491. (is_dynamic_array(ld) or is_dynamic_array(rd)) then
  492. begin
  493. allowed:=false;
  494. exit;
  495. end;
  496. allowed:=true;
  497. end;
  498. objectdef :
  499. begin
  500. { <> and = are defined for implicit pointer object types }
  501. allowed:=not (
  502. is_implicit_pointer_object_type(ld) and
  503. (
  504. (
  505. is_implicit_pointer_object_type(rd) or
  506. (rd.typ=pointerdef) or
  507. (rt=niln) or
  508. ((ld=java_jlstring) and
  509. is_stringlike(rd))
  510. )
  511. ) and
  512. (treetyp in identity_operators)
  513. );
  514. end;
  515. stringdef :
  516. begin
  517. allowed:=not (
  518. is_stringlike(rd) and
  519. (treetyp in string_operators)
  520. );
  521. end;
  522. else
  523. internal_check:=false;
  524. end;
  525. end;
  526. begin
  527. { power ** is always possible }
  528. result:=treetyp=starstarn;
  529. if not result then
  530. begin
  531. if not internal_check(treetyp,ld,lt,rd,rt,result) and
  532. not (treetyp in non_commutative_op_nodes) then
  533. internal_check(treetyp,rd,rt,ld,lt,result)
  534. end;
  535. end;
  536. function isunaryoperatoroverloadable(treetyp:tnodetype;inlinenumber:tinlinenumber;ld:tdef) : boolean;
  537. begin
  538. result:=false;
  539. case treetyp of
  540. subn,
  541. addn,
  542. unaryminusn,
  543. unaryplusn,
  544. inlinen:
  545. begin
  546. { only Inc, Dec inline functions are supported for now, so skip check inlinenumber }
  547. if (ld.typ in [orddef,enumdef,floatdef]) then
  548. exit;
  549. {$ifdef SUPPORT_MMX}
  550. if (cs_mmx in current_settings.localswitches) and
  551. is_mmx_able_array(ld) then
  552. exit;
  553. {$endif SUPPORT_MMX}
  554. result:=true;
  555. end;
  556. notn :
  557. begin
  558. if ld.typ = orddef then exit;
  559. {$ifdef SUPPORT_MMX}
  560. if (cs_mmx in current_settings.localswitches) and
  561. is_mmx_able_array(ld) then
  562. exit;
  563. {$endif SUPPORT_MMX}
  564. result:=true;
  565. end;
  566. else
  567. ;
  568. end;
  569. end;
  570. function isoperatoracceptable(pf : tprocdef; optoken : ttoken) : boolean;
  571. var
  572. ld,rd : tdef;
  573. i : longint;
  574. eq : tequaltype;
  575. conv : tconverttype;
  576. cdo : tcompare_defs_options;
  577. pd : tprocdef;
  578. oldcount,
  579. count: longint;
  580. sym : tsym;
  581. parasym : tparavarsym absolute sym;
  582. begin
  583. result:=false;
  584. count := pf.parast.SymList.count;
  585. oldcount:=count;
  586. while count > 0 do
  587. begin
  588. sym:=tsym(pf.parast.SymList[count-1]);
  589. if sym.typ<>paravarsym then
  590. begin
  591. dec(count);
  592. end
  593. else if is_boolean(parasym.vardef) then
  594. begin
  595. if parasym.name='RANGECHECK' then
  596. begin
  597. Include(parasym.varoptions, vo_is_hidden_para);
  598. Include(parasym.varoptions, vo_is_range_check);
  599. Dec(count);
  600. end
  601. else if parasym.name='OVERFLOWCHECK' then
  602. begin
  603. Include(parasym.varoptions, vo_is_hidden_para);
  604. Include(parasym.varoptions, vo_is_overflow_check);
  605. Dec(count);
  606. end
  607. else
  608. break;
  609. end
  610. else
  611. break;
  612. end;
  613. if count<>oldcount then
  614. pf.calcparas;
  615. case count of
  616. 1 : begin
  617. ld:=tparavarsym(pf.parast.SymList[0]).vardef;
  618. { assignment is a special case }
  619. if optoken in [_ASSIGNMENT,_OP_EXPLICIT] then
  620. begin
  621. cdo:=[];
  622. if optoken=_OP_EXPLICIT then
  623. include(cdo,cdo_explicit);
  624. eq:=compare_defs_ext(ld,pf.returndef,nothingn,conv,pd,cdo);
  625. result:=
  626. (eq=te_exact) or
  627. (eq=te_incompatible);
  628. end
  629. else
  630. { enumerator is a special case too }
  631. if optoken=_OP_ENUMERATOR then
  632. begin
  633. result:=
  634. is_class_or_interface_or_object(pf.returndef) or
  635. is_record(pf.returndef);
  636. if result then
  637. begin
  638. if not assigned(tabstractrecorddef(pf.returndef).search_enumerator_move) then
  639. begin
  640. Message1(sym_e_no_enumerator_move, pf.returndef.typename);
  641. result:=false;
  642. end;
  643. if not assigned(tabstractrecorddef(pf.returndef).search_enumerator_current) then
  644. begin
  645. Message1(sym_e_no_enumerator_current,pf.returndef.typename);
  646. result:=false;
  647. end;
  648. end;
  649. end
  650. else
  651. begin
  652. for i:=1 to tok2nodes do
  653. if tok2node[i].tok=optoken then
  654. begin
  655. result:=
  656. tok2node[i].op_overloading_supported and
  657. (tok2node[i].minargs<=1) and
  658. (tok2node[i].maxargs>=1) and
  659. isunaryoperatoroverloadable(tok2node[i].nod,tok2node[i].inr,ld);
  660. break;
  661. end;
  662. { Inc, Dec operators are valid if only result type is the same as argument type }
  663. if result and (optoken in [_OP_INC,_OP_DEC]) then
  664. result:=pf.returndef=ld;
  665. end;
  666. end;
  667. 2 : begin
  668. for i:=1 to tok2nodes do
  669. if tok2node[i].tok=optoken then
  670. begin
  671. ld:=tparavarsym(pf.parast.SymList[0]).vardef;
  672. rd:=tparavarsym(pf.parast.SymList[1]).vardef;
  673. result:=
  674. tok2node[i].op_overloading_supported and
  675. (tok2node[i].minargs<=2) and
  676. (tok2node[i].maxargs>=2) and
  677. isbinaryoperatoroverloadable(tok2node[i].nod,ld,nothingn,rd,nothingn);
  678. break;
  679. end;
  680. end;
  681. end;
  682. end;
  683. function isunaryoverloaded(var t : tnode;ocf:toverload_check_flags) : boolean;
  684. var
  685. ld : tdef;
  686. optoken : ttoken;
  687. operpd : tprocdef;
  688. ppn : tcallparanode;
  689. candidates : tcallcandidates;
  690. cand_cnt : integer;
  691. inlinenumber: tinlinenumber;
  692. begin
  693. result:=false;
  694. operpd:=nil;
  695. { load easier access variables }
  696. ld:=tunarynode(t).left.resultdef;
  697. { if we are dealing with inline function then get the function }
  698. if t.nodetype=inlinen then
  699. inlinenumber:=tinlinenode(t).inlinenumber
  700. else
  701. inlinenumber:=in_none;
  702. if not (ocf_check_non_overloadable in ocf) and not isunaryoperatoroverloadable(t.nodetype,inlinenumber,ld) then
  703. exit;
  704. { operator overload is possible }
  705. result:=not (ocf_check_only in ocf);
  706. optoken:=NOTOKEN;
  707. case t.nodetype of
  708. notn:
  709. optoken:=_OP_NOT;
  710. unaryminusn:
  711. optoken:=_MINUS;
  712. unaryplusn:
  713. optoken:=_PLUS;
  714. inlinen:
  715. case inlinenumber of
  716. in_inc_x:
  717. optoken:=_OP_INC;
  718. in_dec_x:
  719. optoken:=_OP_DEC;
  720. else
  721. ;
  722. end;
  723. else
  724. ;
  725. end;
  726. if (optoken=NOTOKEN) then
  727. begin
  728. if not (ocf_check_only in ocf) then
  729. begin
  730. CGMessage(parser_e_operator_not_overloaded);
  731. t:=cnothingnode.create;
  732. end;
  733. exit;
  734. end;
  735. { generate parameter nodes }
  736. { for inline nodes just copy existent callparanode }
  737. if (t.nodetype=inlinen) and (tinlinenode(t).left.nodetype=callparan) then
  738. ppn:=tcallparanode(tinlinenode(t).left.getcopy)
  739. else
  740. begin
  741. ppn:=ccallparanode.create(tunarynode(t).left.getcopy,nil);
  742. ppn.get_paratype;
  743. end;
  744. candidates.init_operator(optoken,ppn);
  745. { stop when there are no operators found }
  746. if candidates.count=0 then
  747. begin
  748. candidates.done;
  749. ppn.free;
  750. if not (ocf_check_only in ocf) then
  751. begin
  752. CGMessage2(parser_e_operator_not_overloaded_2,ld.typename,arraytokeninfo[optoken].str);
  753. t:=cnothingnode.create;
  754. end;
  755. exit;
  756. end;
  757. { Retrieve information about the candidates }
  758. candidates.get_information;
  759. {$ifdef EXTDEBUG}
  760. { Display info when multiple candidates are found }
  761. candidates.dump_info(V_Debug);
  762. {$endif EXTDEBUG}
  763. cand_cnt:=candidates.choose_best(tabstractprocdef(operpd),false);
  764. { exit when no overloads are found }
  765. if cand_cnt=0 then
  766. begin
  767. candidates.done;
  768. ppn.free;
  769. if not (ocf_check_only in ocf) then
  770. begin
  771. CGMessage2(parser_e_operator_not_overloaded_2,ld.typename,arraytokeninfo[optoken].str);
  772. t:=cnothingnode.create;
  773. end;
  774. exit;
  775. end;
  776. { Multiple candidates left? }
  777. if (cand_cnt>1) and not (ocf_check_only in ocf) then
  778. begin
  779. CGMessage(type_e_cant_choose_overload_function);
  780. {$ifdef EXTDEBUG}
  781. candidates.dump_info(V_Hint);
  782. {$else EXTDEBUG}
  783. candidates.list(false);
  784. {$endif EXTDEBUG}
  785. { we'll just use the first candidate to make the
  786. call }
  787. end;
  788. candidates.done;
  789. if ocf_check_only in ocf then
  790. begin
  791. ppn.free;
  792. result:=true;
  793. exit;
  794. end;
  795. addsymref(operpd.procsym,operpd);
  796. { the nil as symtable signs firstcalln that this is
  797. an overloaded operator }
  798. t:=ccallnode.create(ppn,Tprocsym(operpd.procsym),nil,nil,[],nil);
  799. { we already know the procdef to use, so it can
  800. skip the overload choosing in callnode.pass_typecheck }
  801. tcallnode(t).procdefinition:=operpd;
  802. end;
  803. function isbinaryoverloaded(var t : tnode;ocf:toverload_check_flags) : boolean;
  804. var
  805. rd,ld : tdef;
  806. optoken : ttoken;
  807. operpd : tprocdef;
  808. ht : tnode;
  809. ppn : tcallparanode;
  810. cand_cnt : integer;
  811. function search_operator(optoken:ttoken;generror:boolean): integer;
  812. var
  813. candidates : tcallcandidates;
  814. begin
  815. { generate parameter nodes }
  816. ppn:=ccallparanode.create(tbinarynode(t).right.getcopy,ccallparanode.create(tbinarynode(t).left.getcopy,nil));
  817. ppn.get_paratype;
  818. candidates.init_operator(optoken,ppn);
  819. { for commutative operators we can swap arguments and try again }
  820. if (candidates.count=0) and
  821. not(optoken in non_commutative_op_tokens) then
  822. begin
  823. candidates.done;
  824. reverseparameters(ppn);
  825. { reverse compare operators }
  826. case optoken of
  827. _LT:
  828. optoken:=_GTE;
  829. _GT:
  830. optoken:=_LTE;
  831. _LTE:
  832. optoken:=_GT;
  833. _GTE:
  834. optoken:=_LT;
  835. else
  836. ;
  837. end;
  838. candidates.init_operator(optoken,ppn);
  839. end;
  840. { stop when there are no operators found }
  841. result:=candidates.count;
  842. if (result=0) and generror then
  843. begin
  844. CGMessage(parser_e_operator_not_overloaded);
  845. candidates.done;
  846. ppn.free;
  847. ppn:=nil;
  848. exit;
  849. end;
  850. if (result>0) then
  851. begin
  852. { Retrieve information about the candidates }
  853. candidates.get_information;
  854. {$ifdef EXTDEBUG}
  855. { Display info when multiple candidates are found }
  856. candidates.dump_info(V_Debug);
  857. {$endif EXTDEBUG}
  858. result:=candidates.choose_best(tabstractprocdef(operpd),false);
  859. end;
  860. { exit when no overloads are found }
  861. if (result=0) and generror then
  862. begin
  863. CGMessage3(parser_e_operator_not_overloaded_3,ld.GetTypeName,arraytokeninfo[optoken].str,rd.GetTypeName);
  864. candidates.done;
  865. ppn.free;
  866. ppn:=nil;
  867. exit;
  868. end;
  869. { Multiple candidates left? }
  870. if result>1 then
  871. begin
  872. CGMessage(type_e_cant_choose_overload_function);
  873. {$ifdef EXTDEBUG}
  874. candidates.dump_info(V_Hint);
  875. {$else EXTDEBUG}
  876. candidates.list(false);
  877. {$endif EXTDEBUG}
  878. { we'll just use the first candidate to make the
  879. call }
  880. end;
  881. candidates.done;
  882. end;
  883. begin
  884. isbinaryoverloaded:=false;
  885. operpd:=nil;
  886. ppn:=nil;
  887. { load easier access variables }
  888. ld:=tbinarynode(t).left.resultdef;
  889. rd:=tbinarynode(t).right.resultdef;
  890. if not (ocf_check_non_overloadable in ocf) and
  891. not isbinaryoperatoroverloadable(t.nodetype,ld,tbinarynode(t).left.nodetype,rd,tbinarynode(t).right.nodetype) then
  892. exit;
  893. { operator overload is possible }
  894. { if we only check for the existance of the overload, then we assume that
  895. it is not overloaded }
  896. result:=not (ocf_check_only in ocf);
  897. case t.nodetype of
  898. equaln:
  899. optoken:=_EQ;
  900. unequaln:
  901. optoken:=_NE;
  902. addn:
  903. optoken:=_PLUS;
  904. subn:
  905. optoken:=_MINUS;
  906. muln:
  907. optoken:=_STAR;
  908. starstarn:
  909. optoken:=_STARSTAR;
  910. slashn:
  911. optoken:=_SLASH;
  912. ltn:
  913. optoken:=_LT;
  914. gtn:
  915. optoken:=_GT;
  916. lten:
  917. optoken:=_LTE;
  918. gten:
  919. optoken:=_GTE;
  920. symdifn :
  921. optoken:=_SYMDIF;
  922. modn :
  923. optoken:=_OP_MOD;
  924. orn :
  925. optoken:=_OP_OR;
  926. xorn :
  927. optoken:=_OP_XOR;
  928. andn :
  929. optoken:=_OP_AND;
  930. divn :
  931. optoken:=_OP_DIV;
  932. shln :
  933. optoken:=_OP_SHL;
  934. shrn :
  935. optoken:=_OP_SHR;
  936. inn :
  937. optoken:=_OP_IN;
  938. else
  939. begin
  940. if not (ocf_check_only in ocf) then
  941. begin
  942. CGMessage(parser_e_operator_not_overloaded);
  943. t:=cnothingnode.create;
  944. end;
  945. exit;
  946. end;
  947. end;
  948. cand_cnt:=search_operator(optoken,(optoken<>_NE) and not (ocf_check_only in ocf));
  949. { no operator found for "<>" then search for "=" operator }
  950. if (cand_cnt=0) and (optoken=_NE) and not (ocf_check_only in ocf) then
  951. begin
  952. ppn.free;
  953. ppn:=nil;
  954. operpd:=nil;
  955. optoken:=_EQ;
  956. cand_cnt:=search_operator(optoken,true);
  957. end;
  958. if (cand_cnt=0) then
  959. begin
  960. ppn.free;
  961. if not (ocf_check_only in ocf) then
  962. t:=cnothingnode.create;
  963. exit;
  964. end;
  965. if ocf_check_only in ocf then
  966. begin
  967. ppn.free;
  968. result:=true;
  969. exit;
  970. end;
  971. addsymref(operpd.procsym,operpd);
  972. { the nil as symtable signs firstcalln that this is
  973. an overloaded operator }
  974. ht:=ccallnode.create(ppn,Tprocsym(operpd.procsym),nil,nil,[],nil);
  975. { we already know the procdef to use, so it can
  976. skip the overload choosing in callnode.pass_typecheck }
  977. tcallnode(ht).procdefinition:=operpd;
  978. { if we found "=" operator for "<>" expression then use it
  979. together with "not" }
  980. if (t.nodetype=unequaln) and (optoken=_EQ) then
  981. ht:=cnotnode.create(ht);
  982. t:=ht;
  983. end;
  984. {****************************************************************************
  985. Register Calculation
  986. ****************************************************************************}
  987. { marks an lvalue as "unregable" }
  988. procedure make_not_regable_intern(p : tnode; how: tregableinfoflags; records_only: boolean);
  989. begin
  990. if ra_addr_taken in how then
  991. include(p.flags,nf_address_taken);
  992. repeat
  993. case p.nodetype of
  994. subscriptn:
  995. begin
  996. records_only:=true;
  997. p:=tsubscriptnode(p).left;
  998. end;
  999. vecn:
  1000. begin
  1001. { if there's an implicit dereference, we can stop (just like
  1002. when there is an actual derefn) }
  1003. if ((tvecnode(p).left.resultdef.typ=arraydef) and
  1004. not is_special_array(tvecnode(p).left.resultdef)) or
  1005. ((tvecnode(p).left.resultdef.typ=stringdef) and
  1006. (tstringdef(tvecnode(p).left.resultdef).stringtype in [st_shortstring,st_longstring])) then
  1007. p:=tvecnode(p).left
  1008. else
  1009. break;
  1010. end;
  1011. typeconvn :
  1012. begin
  1013. { implicit dereference -> stop }
  1014. if (ttypeconvnode(p).convtype=tc_pointer_2_array) then
  1015. break;
  1016. if (ttypeconvnode(p).resultdef.typ=recorddef) then
  1017. records_only:=false;
  1018. p:=ttypeconvnode(p).left;
  1019. end;
  1020. loadn :
  1021. begin
  1022. if (tloadnode(p).symtableentry.typ in [staticvarsym,localvarsym,paravarsym]) then
  1023. begin
  1024. if (ra_addr_taken in how) then
  1025. tabstractvarsym(tloadnode(p).symtableentry).addr_taken:=true;
  1026. if (ra_different_scope in how) then
  1027. tabstractvarsym(tloadnode(p).symtableentry).different_scope:=true;
  1028. if (tabstractvarsym(tloadnode(p).symtableentry).varregable <> vr_none) and
  1029. ((not records_only) or
  1030. (tabstractvarsym(tloadnode(p).symtableentry).vardef.typ = recorddef)) then
  1031. if (tloadnode(p).symtableentry.typ = paravarsym) and
  1032. (ra_addr_regable in how) then
  1033. tabstractvarsym(tloadnode(p).symtableentry).varregable:=vr_addr
  1034. else
  1035. tabstractvarsym(tloadnode(p).symtableentry).varregable:=vr_none;
  1036. end;
  1037. break;
  1038. end;
  1039. temprefn :
  1040. begin
  1041. if (ra_addr_taken in how) then
  1042. ttemprefnode(p).includetempflag(ti_addr_taken);
  1043. if (ti_may_be_in_reg in ttemprefnode(p).tempflags) and
  1044. ((not records_only) or
  1045. (ttemprefnode(p).tempinfo^.typedef.typ = recorddef)) then
  1046. ttemprefnode(p).excludetempflag(ti_may_be_in_reg);
  1047. break;
  1048. end;
  1049. else
  1050. break;
  1051. end;
  1052. until false;
  1053. end;
  1054. procedure make_not_regable(p : tnode; how: tregableinfoflags);
  1055. begin
  1056. make_not_regable_intern(p,how,false);
  1057. end;
  1058. {****************************************************************************
  1059. Subroutine Handling
  1060. ****************************************************************************}
  1061. function is_proc2procvar_load(p:tnode;out realprocdef:tprocdef):boolean;
  1062. begin
  1063. result:=false;
  1064. { remove voidpointer typecast for tp procvars }
  1065. if ((m_tp_procvar in current_settings.modeswitches) or
  1066. (m_mac_procvar in current_settings.modeswitches)) and
  1067. (p.nodetype=typeconvn) and
  1068. is_voidpointer(p.resultdef) then
  1069. p:=tunarynode(p).left;
  1070. result:=(p.nodetype=typeconvn) and
  1071. (ttypeconvnode(p).convtype=tc_proc_2_procvar);
  1072. if result then
  1073. realprocdef:=tprocdef(ttypeconvnode(p).left.resultdef);
  1074. end;
  1075. function is_ambiguous_funcret_load(p: tnode; out owningprocdef: tprocdef): boolean;
  1076. begin
  1077. result:=false;
  1078. { the funcret is an absolutevarsym, which gets converted into a type
  1079. conversion node of the loadnode of the actual function result. Its
  1080. resulttype is obviously the same as that of the real function result }
  1081. if (p.nodetype=typeconvn) and
  1082. (p.resultdef=ttypeconvnode(p).left.resultdef) then
  1083. p:=ttypeconvnode(p).left;
  1084. if (p.nodetype=loadn) and
  1085. (tloadnode(p).symtableentry.typ in [absolutevarsym,localvarsym,paravarsym]) and
  1086. ([vo_is_funcret,vo_is_result] * tabstractvarsym(tloadnode(p).symtableentry).varoptions = [vo_is_funcret]) then
  1087. begin
  1088. owningprocdef:=tprocdef(tloadnode(p).symtableentry.owner.defowner);
  1089. result:=true;
  1090. end;
  1091. end;
  1092. { local routines can't be assigned to procvars }
  1093. procedure test_local_to_procvar(from_def:tprocvardef;to_def:tdef);
  1094. begin
  1095. if not(m_nested_procvars in current_settings.modeswitches) and
  1096. (from_def.parast.symtablelevel>normal_function_level) and
  1097. not (po_anonymous in from_def.procoptions) and
  1098. (to_def.typ=procvardef) and
  1099. (tprocvardef(to_def).parast.symtablelevel <= normal_function_level) then
  1100. CGMessage(type_e_cannot_local_proc_to_procvar);
  1101. end;
  1102. procedure UninitializedVariableMessage(pos : tfileposinfo;warning,local,managed : boolean;name : TMsgStr);
  1103. const
  1104. msg : array[false..true,false..true,false..true] of dword = (
  1105. (
  1106. (sym_h_uninitialized_variable,sym_h_uninitialized_managed_variable),
  1107. (sym_h_uninitialized_local_variable,sym_h_uninitialized_managed_local_variable)
  1108. ),
  1109. (
  1110. (sym_w_uninitialized_variable,sym_w_uninitialized_managed_variable),
  1111. (sym_w_uninitialized_local_variable,sym_w_uninitialized_managed_local_variable)
  1112. )
  1113. );
  1114. begin
  1115. CGMessagePos1(pos,msg[warning,local,managed],name);
  1116. end;
  1117. procedure set_varstate(p:tnode;newstate:tvarstate;varstateflags:tvarstateflags);
  1118. const
  1119. vstrans: array[tvarstate,tvarstate] of tvarstate = (
  1120. { vs_none -> ... }
  1121. (vs_none,vs_declared,vs_initialised,vs_read,vs_read_not_warned,vs_referred_not_inited,vs_written,vs_readwritten),
  1122. { vs_declared -> ... }
  1123. (vs_none,vs_declared,vs_initialised,vs_read,vs_read_not_warned,vs_referred_not_inited,vs_written,vs_readwritten),
  1124. { vs_initialised -> ... }
  1125. (vs_none,vs_initialised,vs_initialised,vs_read,vs_read,vs_read,vs_written,vs_readwritten),
  1126. { vs_read -> ... }
  1127. (vs_none,vs_read,vs_read,vs_read,vs_read,vs_read,vs_readwritten,vs_readwritten),
  1128. { vs_read_not_warned -> ... }
  1129. (vs_none,vs_read_not_warned,vs_read,vs_read,vs_read_not_warned,vs_read_not_warned,vs_readwritten,vs_readwritten),
  1130. { vs_referred_not_inited }
  1131. (vs_none,vs_referred_not_inited,vs_read,vs_read,vs_read_not_warned,vs_referred_not_inited,vs_written,vs_readwritten),
  1132. { vs_written -> ... }
  1133. (vs_none,vs_written,vs_written,vs_readwritten,vs_readwritten,vs_written,vs_written,vs_readwritten),
  1134. { vs_readwritten -> ... }
  1135. (vs_none,vs_readwritten,vs_readwritten,vs_readwritten,vs_readwritten,vs_readwritten,vs_readwritten,vs_readwritten));
  1136. var
  1137. hsym : tabstractvarsym;
  1138. begin
  1139. { make sure we can still warn about uninitialised use after high(v), @v etc }
  1140. if (newstate = vs_read) and
  1141. not(vsf_must_be_valid in varstateflags) then
  1142. newstate := vs_referred_not_inited;
  1143. while assigned(p) do
  1144. begin
  1145. case p.nodetype of
  1146. derefn:
  1147. begin
  1148. if (tderefnode(p).left.nodetype=temprefn) and
  1149. assigned(ttemprefnode(tderefnode(p).left).tempinfo^.withnode) then
  1150. p:=ttemprefnode(tderefnode(p).left).tempinfo^.withnode
  1151. else
  1152. break;
  1153. end;
  1154. typeconvn :
  1155. begin
  1156. case ttypeconvnode(p).convtype of
  1157. tc_cchar_2_pchar,
  1158. tc_cstring_2_pchar,
  1159. tc_array_2_pointer :
  1160. exclude(varstateflags,vsf_must_be_valid);
  1161. tc_pchar_2_string,
  1162. tc_pointer_2_array :
  1163. begin
  1164. include(varstateflags,vsf_must_be_valid);
  1165. { when a pointer is used for array access, the
  1166. pointer itself is read and never written }
  1167. newstate := vs_read;
  1168. end;
  1169. else
  1170. ;
  1171. end;
  1172. p:=tunarynode(p).left;
  1173. end;
  1174. subscriptn :
  1175. begin
  1176. if is_implicit_pointer_object_type(tunarynode(p).left.resultdef) then
  1177. newstate := vs_read;
  1178. p:=tunarynode(p).left;
  1179. end;
  1180. vecn:
  1181. begin
  1182. set_varstate(tbinarynode(p).right,vs_read,[vsf_must_be_valid]);
  1183. { dyn. arrays and dyn. strings are read }
  1184. if is_implicit_array_pointer(tunarynode(p).left.resultdef) then
  1185. newstate:=vs_read;
  1186. if (newstate in [vs_read,vs_readwritten]) or
  1187. not(tunarynode(p).left.resultdef.typ in [stringdef,arraydef]) then
  1188. include(varstateflags,vsf_must_be_valid)
  1189. else if (newstate = vs_written) then
  1190. exclude(varstateflags,vsf_must_be_valid);
  1191. p:=tunarynode(p).left;
  1192. end;
  1193. { do not parse calln }
  1194. calln :
  1195. break;
  1196. loadn :
  1197. begin
  1198. { the methodpointer/framepointer is read }
  1199. if assigned(tunarynode(p).left) then
  1200. set_varstate(tunarynode(p).left,vs_read,[vsf_must_be_valid]);
  1201. if (tloadnode(p).symtableentry.typ in [localvarsym,paravarsym,staticvarsym]) then
  1202. begin
  1203. hsym:=tabstractvarsym(tloadnode(p).symtableentry);
  1204. { this check requires proper data flow analysis... }
  1205. (* if (hsym.varspez=vs_final) and
  1206. (hsym.varstate in [vs_written,vs_readwritten]) and
  1207. (newstate in [vs_written,vs_readwritten]) then
  1208. CGMessagePos1(p.fileinfo,sym_e_final_write_once); *)
  1209. if (vsf_must_be_valid in varstateflags) and
  1210. (hsym.varstate in [vs_declared,vs_read_not_warned,vs_referred_not_inited]) then
  1211. begin
  1212. { Give warning/note for uninitialized locals }
  1213. if assigned(hsym.owner) and
  1214. not(vo_is_external in hsym.varoptions) and
  1215. (hsym.owner.symtabletype in [parasymtable,localsymtable,staticsymtable]) and
  1216. ((hsym.owner=current_procinfo.procdef.localst) or
  1217. (hsym.owner=current_procinfo.procdef.parast)) then
  1218. begin
  1219. if vsf_use_hints in varstateflags then
  1220. include(tloadnode(p).loadnodeflags,loadnf_only_uninitialized_hint);
  1221. if not(cs_opt_nodedfa in current_settings.optimizerswitches) then
  1222. begin
  1223. if (vo_is_funcret in hsym.varoptions) then
  1224. begin
  1225. { An uninitialized function Result of a managed type needs special handling.
  1226. When passing it as a var parameter a warning need to be emitted, since a user
  1227. may expect Result to be empty (nil) by default as it happens with local vars
  1228. of a managed type. But this is not true for Result and may lead to serious issues.
  1229. The only exception is SetLength(Result, ?) for a string Result. A user always
  1230. expects undefined contents of the string after calling SetLength(). In such
  1231. case a hint need to be emitted.
  1232. }
  1233. if is_managed_type(hsym.vardef) then
  1234. if not ( is_string(hsym.vardef) and (vsf_use_hint_for_string_result in varstateflags) ) then
  1235. exclude(varstateflags,vsf_use_hints);
  1236. if vsf_use_hints in varstateflags then
  1237. begin
  1238. if is_managed_type(hsym.vardef) then
  1239. CGMessagePos(p.fileinfo,sym_h_managed_function_result_uninitialized)
  1240. else
  1241. CGMessagePos(p.fileinfo,sym_h_function_result_uninitialized);
  1242. end
  1243. else
  1244. begin
  1245. if is_managed_type(hsym.vardef) then
  1246. CGMessagePos(p.fileinfo,sym_w_managed_function_result_uninitialized)
  1247. else
  1248. CGMessagePos(p.fileinfo,sym_w_function_result_uninitialized);
  1249. end;
  1250. end
  1251. else
  1252. begin
  1253. UninitializedVariableMessage(p.fileinfo,
  1254. { on the JVM, an uninitialized var-parameter
  1255. is just as fatal as a nil pointer dereference }
  1256. not((vsf_use_hints in varstateflags) and not(target_info.system in systems_jvm)),
  1257. tloadnode(p).symtable.symtabletype=localsymtable,
  1258. is_managed_type(tloadnode(p).resultdef),
  1259. hsym.realname);
  1260. end;
  1261. end;
  1262. end
  1263. else if (newstate = vs_read) then
  1264. newstate := vs_read_not_warned;
  1265. end;
  1266. hsym.varstate := vstrans[hsym.varstate,newstate];
  1267. end;
  1268. case newstate of
  1269. vs_written:
  1270. include(tloadnode(p).flags,nf_write);
  1271. vs_readwritten:
  1272. if not(nf_write in tloadnode(p).flags) then
  1273. include(tloadnode(p).flags,nf_modify);
  1274. else
  1275. ;
  1276. end;
  1277. break;
  1278. end;
  1279. addrn:
  1280. break;
  1281. callparan :
  1282. internalerror(200310081);
  1283. else
  1284. break;
  1285. end;{case }
  1286. end;
  1287. end;
  1288. procedure set_unique(p : tnode);
  1289. begin
  1290. while assigned(p) do
  1291. begin
  1292. case p.nodetype of
  1293. vecn:
  1294. begin
  1295. include(tvecnode(p).vecnodeflags,vnf_callunique);
  1296. break;
  1297. end;
  1298. typeconvn,
  1299. subscriptn,
  1300. derefn:
  1301. p:=tunarynode(p).left;
  1302. else
  1303. break;
  1304. end;
  1305. end;
  1306. end;
  1307. function valid_for_assign(p:tnode;opts:TValidAssigns; report_errors: boolean):boolean;
  1308. var
  1309. typeconvs: tfpobjectlist;
  1310. hp2,
  1311. hp : tnode;
  1312. gotstring,
  1313. gotsubscript,
  1314. gotrecord,
  1315. gotvec,
  1316. gottypeconv : boolean;
  1317. fromdef,
  1318. todef : tdef;
  1319. errmsg,
  1320. temp : longint;
  1321. function constaccessok(vs: tabstractvarsym): boolean;
  1322. begin
  1323. result:=false;
  1324. { allow p^:= constructions with p is const parameter }
  1325. if (Valid_Const in opts) or
  1326. ((hp.nodetype=loadn) and
  1327. (loadnf_isinternal_ignoreconst in tloadnode(hp).loadnodeflags)) then
  1328. result:=true
  1329. { final (class) fields can only be initialised in the (class) constructors of
  1330. class in which they have been declared (not in descendent constructors) }
  1331. else if vs.varspez=vs_final then
  1332. begin
  1333. if (current_procinfo.procdef.owner=vs.owner) then
  1334. if vs.typ=staticvarsym then
  1335. result:=current_procinfo.procdef.proctypeoption=potype_class_constructor
  1336. else
  1337. result:=current_procinfo.procdef.proctypeoption=potype_constructor;
  1338. if not result and
  1339. report_errors then
  1340. CGMessagePos(hp.fileinfo,type_e_invalid_final_assignment);
  1341. end
  1342. else
  1343. if report_errors then
  1344. CGMessagePos(hp.fileinfo,type_e_no_assign_to_const);
  1345. end;
  1346. procedure mayberesettypeconvs;
  1347. var
  1348. i: longint;
  1349. begin
  1350. if assigned(typeconvs) then
  1351. begin
  1352. if not report_errors and
  1353. not result then
  1354. for i:=0 to typeconvs.Count-1 do
  1355. ttypeconvnode(typeconvs[i]).assignment_side:=false;
  1356. typeconvs.free;
  1357. end;
  1358. end;
  1359. begin
  1360. if valid_const in opts then
  1361. errmsg:=type_e_variable_id_expected
  1362. else if valid_property in opts then
  1363. errmsg:=type_e_argument_cant_be_assigned
  1364. else
  1365. errmsg:=type_e_no_addr_of_constant;
  1366. result:=false;
  1367. gotsubscript:=false;
  1368. gotvec:=false;
  1369. gotrecord:=false;
  1370. gotstring:=false;
  1371. gottypeconv:=false;
  1372. hp:=p;
  1373. if not(valid_void in opts) and
  1374. is_void(hp.resultdef) then
  1375. begin
  1376. if report_errors then
  1377. CGMessagePos(hp.fileinfo,errmsg);
  1378. exit;
  1379. end;
  1380. typeconvs:=nil;
  1381. while assigned(hp) do
  1382. begin
  1383. { property allowed? calln has a property check itself }
  1384. if (nf_isproperty in hp.flags) then
  1385. begin
  1386. { check return type }
  1387. case hp.resultdef.typ of
  1388. recorddef :
  1389. gotrecord:=true;
  1390. stringdef :
  1391. gotstring:=true;
  1392. else
  1393. ;
  1394. end;
  1395. if (valid_property in opts) then
  1396. begin
  1397. { don't allow writing to calls that will create
  1398. temps like calls that return a structure and we
  1399. are assigning to a member }
  1400. if (valid_const in opts) or
  1401. (
  1402. { allowing assignments to typecasted properties
  1403. a) is Delphi-incompatible
  1404. b) causes problems in case the getter is a function
  1405. (because then the result of the getter is
  1406. typecasted to this type, and then we "assign" to
  1407. this typecasted function result) -> always
  1408. disallow, since property accessors should be
  1409. transparantly changeable to functions at all
  1410. times
  1411. }
  1412. not(gottypeconv) and
  1413. not(gotsubscript and gotrecord) and
  1414. not(gotstring and gotvec) and
  1415. not(nf_no_lvalue in hp.flags)
  1416. ) then
  1417. result:=true
  1418. else
  1419. if report_errors then
  1420. CGMessagePos(hp.fileinfo,errmsg);
  1421. end
  1422. else
  1423. begin
  1424. { 1. if it returns a pointer and we've found a deref,
  1425. 2. if it returns a class and a subscription or with is found
  1426. 3. if the address is needed of a field (subscriptn, vecn) }
  1427. if (gotstring and gotvec) or
  1428. (
  1429. (Valid_Addr in opts) and
  1430. (hp.nodetype in [subscriptn,vecn])
  1431. ) then
  1432. result:=true
  1433. else
  1434. if report_errors then
  1435. CGMessagePos(hp.fileinfo,errmsg);
  1436. end;
  1437. mayberesettypeconvs;
  1438. exit;
  1439. end;
  1440. case hp.nodetype of
  1441. temprefn :
  1442. begin
  1443. valid_for_assign := not(ti_readonly in ttemprefnode(hp).tempflags);
  1444. mayberesettypeconvs;
  1445. exit;
  1446. end;
  1447. derefn :
  1448. begin
  1449. { dereference -> always valid }
  1450. valid_for_assign:=true;
  1451. mayberesettypeconvs;
  1452. exit;
  1453. end;
  1454. typeconvn :
  1455. begin
  1456. gottypeconv:=true;
  1457. { typecast sizes must match, exceptions:
  1458. - implicit typecast made by absolute
  1459. - from formaldef
  1460. - from void
  1461. - from/to open array
  1462. - typecast from pointer to array }
  1463. fromdef:=ttypeconvnode(hp).left.resultdef;
  1464. todef:=hp.resultdef;
  1465. { typeconversions on the assignment side must keep
  1466. left.location the same }
  1467. if not((target_info.system in systems_jvm) and
  1468. (gotsubscript or gotvec)) then
  1469. begin
  1470. ttypeconvnode(hp).assignment_side:=true;
  1471. if not assigned(typeconvs) then
  1472. typeconvs:=tfpobjectlist.create(false);
  1473. typeconvs.add(hp);
  1474. end;
  1475. { in managed VMs, you cannot typecast formaldef when assigning
  1476. to it, see http://hallvards.blogspot.com/2007/10/dn4dp24-net-vs-win32-untyped-parameters.html }
  1477. if (target_info.system in systems_managed_vm) and
  1478. (fromdef.typ=formaldef) then
  1479. begin
  1480. if report_errors then
  1481. CGMessagePos(hp.fileinfo,type_e_no_managed_formal_assign_typecast);
  1482. mayberesettypeconvs;
  1483. exit;
  1484. end
  1485. else if not((nf_absolute in ttypeconvnode(hp).flags) or
  1486. ttypeconvnode(hp).target_specific_general_typeconv or
  1487. ((nf_explicit in hp.flags) and
  1488. ttypeconvnode(hp).target_specific_explicit_typeconv) or
  1489. (fromdef.typ=formaldef) or
  1490. is_void(fromdef) or
  1491. is_open_array(fromdef) or
  1492. is_open_array(todef) or
  1493. ((fromdef.typ=pointerdef) and (todef.typ=arraydef)) or
  1494. (def_is_related(fromdef,todef))) then
  1495. begin
  1496. if (fromdef.size<>todef.size) then
  1497. begin
  1498. { in TP it is allowed to typecast to smaller types. But the variable can't
  1499. be in a register }
  1500. if (m_tp7 in current_settings.modeswitches) or
  1501. (todef.size<fromdef.size) then
  1502. make_not_regable(hp,[ra_addr_regable])
  1503. else
  1504. if report_errors then
  1505. CGMessagePos2(hp.fileinfo,type_e_typecast_wrong_size_for_assignment,tostr(fromdef.size),tostr(todef.size));
  1506. end
  1507. {$ifdef llvm}
  1508. { we can never typecast a non-memory value on the assignment
  1509. side in llvm }
  1510. else
  1511. make_not_regable(hp,[ra_addr_regable])
  1512. {$endif llvm}
  1513. end;
  1514. { don't allow assignments to typeconvs that need special code }
  1515. if not(gotsubscript or gotvec) and
  1516. not(ttypeconvnode(hp).assign_allowed) then
  1517. begin
  1518. if report_errors then
  1519. CGMessagePos(hp.fileinfo,errmsg);
  1520. mayberesettypeconvs;
  1521. exit;
  1522. end;
  1523. case hp.resultdef.typ of
  1524. arraydef :
  1525. begin
  1526. { pointer -> array conversion is done then we need to see it
  1527. as a deref, because a ^ is then not required anymore }
  1528. if ttypeconvnode(hp).convtype=tc_pointer_2_array then
  1529. begin
  1530. valid_for_assign:=true;
  1531. mayberesettypeconvs;
  1532. exit
  1533. end;
  1534. end;
  1535. else
  1536. ;
  1537. end;
  1538. hp:=ttypeconvnode(hp).left;
  1539. end;
  1540. vecn :
  1541. begin
  1542. if (tvecnode(hp).right.nodetype=rangen) and
  1543. not(valid_range in opts) then
  1544. begin
  1545. if report_errors then
  1546. CGMessagePos(tvecnode(hp).right.fileinfo,parser_e_illegal_expression);
  1547. mayberesettypeconvs;
  1548. exit;
  1549. end;
  1550. if { only check for first (= outermost) vec node }
  1551. not gotvec and
  1552. not(valid_packed in opts) and
  1553. (tvecnode(hp).left.resultdef.typ = arraydef) and
  1554. (ado_IsBitPacked in tarraydef(tvecnode(hp).left.resultdef).arrayoptions) and
  1555. ((tarraydef(tvecnode(hp).left.resultdef).elepackedbitsize mod 8 <> 0) or
  1556. (is_ordinal(tarraydef(tvecnode(hp).left.resultdef).elementdef) and
  1557. not ispowerof2(tarraydef(tvecnode(hp).left.resultdef).elepackedbitsize div 8,temp))) then
  1558. begin
  1559. if report_errors then
  1560. if (valid_property in opts) then
  1561. CGMessagePos(hp.fileinfo,parser_e_packed_element_no_loop)
  1562. else
  1563. CGMessagePos(hp.fileinfo,parser_e_packed_element_no_var_addr);
  1564. mayberesettypeconvs;
  1565. exit;
  1566. end;
  1567. gotvec:=true;
  1568. { accesses to dyn. arrays override read only access in delphi
  1569. -- now also in FPC, because the elements of a dynamic array
  1570. returned by a function can also be changed, or you can
  1571. assign the dynamic array to a variable and then change
  1572. its elements anyway }
  1573. if is_dynamic_array(tunarynode(hp).left.resultdef) then
  1574. begin
  1575. result:=true;
  1576. mayberesettypeconvs;
  1577. exit;
  1578. end;
  1579. hp:=tunarynode(hp).left;
  1580. end;
  1581. asn :
  1582. begin
  1583. { asn can't be assigned directly, it returns the value in a register instead
  1584. of reference. }
  1585. if not(gotsubscript or gotvec) then
  1586. begin
  1587. if report_errors then
  1588. CGMessagePos(hp.fileinfo,errmsg);
  1589. mayberesettypeconvs;
  1590. exit;
  1591. end;
  1592. hp:=tunarynode(hp).left;
  1593. end;
  1594. subscriptn :
  1595. begin
  1596. { only check first (= outermost) subscriptn }
  1597. if not gotsubscript and
  1598. not(valid_packed in opts) and
  1599. is_packed_record_or_object(tsubscriptnode(hp).left.resultdef) and
  1600. ((tsubscriptnode(hp).vs.fieldoffset mod 8 <> 0) or
  1601. (is_ordinal(tsubscriptnode(hp).resultdef) and
  1602. not ispowerof2(tsubscriptnode(hp).resultdef.packedbitsize div 8,temp))) then
  1603. begin
  1604. if report_errors then
  1605. if (valid_property in opts) then
  1606. CGMessagePos(hp.fileinfo,parser_e_packed_element_no_loop)
  1607. else
  1608. CGMessagePos(hp.fileinfo,parser_e_packed_element_no_var_addr);
  1609. mayberesettypeconvs;
  1610. exit;
  1611. end;
  1612. { check for final fields }
  1613. if (tsubscriptnode(hp).vs.varspez=vs_final) and
  1614. not constaccessok(tsubscriptnode(hp).vs) then
  1615. begin
  1616. mayberesettypeconvs;
  1617. exit;
  1618. end;
  1619. { if we assign something to a field of a record that is not
  1620. regable, then then the record can't be kept in a regvar,
  1621. because we will force the record into memory for this
  1622. subscript operation (to a temp location, so the assignment
  1623. will happen to the temp and be lost) }
  1624. if not gotsubscript and
  1625. not gotvec and
  1626. not tstoreddef(hp.resultdef).is_intregable then
  1627. make_not_regable(hp,[ra_addr_regable]);
  1628. gotsubscript:=true;
  1629. { loop counter? }
  1630. if not(Valid_Const in opts) and
  1631. (vo_is_loop_counter in tsubscriptnode(hp).vs.varoptions) then
  1632. begin
  1633. if report_errors then
  1634. CGMessage1(parser_e_illegal_assignment_to_count_var,tsubscriptnode(hp).vs.realname);
  1635. mayberesettypeconvs;
  1636. exit;
  1637. end;
  1638. { implicit pointer object types result in dereferencing }
  1639. hp:=tsubscriptnode(hp).left;
  1640. if is_implicit_pointer_object_type(hp.resultdef) or
  1641. (hp.resultdef.typ=classrefdef) then
  1642. begin
  1643. valid_for_assign:=true;
  1644. mayberesettypeconvs;
  1645. exit
  1646. end;
  1647. end;
  1648. muln,
  1649. divn,
  1650. andn,
  1651. xorn,
  1652. orn,
  1653. notn,
  1654. subn,
  1655. addn :
  1656. begin
  1657. { Temp strings are stored in memory, for compatibility with
  1658. delphi only }
  1659. if (m_delphi in current_settings.modeswitches) and
  1660. ((valid_addr in opts) or
  1661. (valid_const in opts)) and
  1662. (hp.resultdef.typ=stringdef) then
  1663. result:=true
  1664. else
  1665. if report_errors then
  1666. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  1667. mayberesettypeconvs;
  1668. exit;
  1669. end;
  1670. niln,
  1671. pointerconstn :
  1672. begin
  1673. if report_errors then
  1674. CGMessagePos(hp.fileinfo,type_e_no_assign_to_addr);
  1675. mayberesettypeconvs;
  1676. exit;
  1677. end;
  1678. ordconstn,
  1679. realconstn :
  1680. begin
  1681. { these constants will be passed by value }
  1682. if report_errors then
  1683. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  1684. mayberesettypeconvs;
  1685. exit;
  1686. end;
  1687. arrayconstructorn,
  1688. setconstn,
  1689. stringconstn,
  1690. guidconstn :
  1691. begin
  1692. { these constants will be passed by reference }
  1693. if valid_const in opts then
  1694. result:=true
  1695. else
  1696. if report_errors then
  1697. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  1698. mayberesettypeconvs;
  1699. exit;
  1700. end;
  1701. addrn :
  1702. begin
  1703. if report_errors then
  1704. CGMessagePos(hp.fileinfo,type_e_no_assign_to_addr);
  1705. mayberesettypeconvs;
  1706. exit;
  1707. end;
  1708. blockn,
  1709. calln :
  1710. begin
  1711. if ((hp.nodetype=calln) and not(is_void(hp.resultdef))) or
  1712. (nf_no_lvalue in hp.flags) then
  1713. begin
  1714. { Temp strings are stored in memory, for compatibility with
  1715. delphi only }
  1716. if (m_delphi in current_settings.modeswitches) and
  1717. (valid_addr in opts) and
  1718. (hp.resultdef.typ=stringdef) then
  1719. result:=true
  1720. else
  1721. if ([valid_const,valid_addr] * opts = [valid_const]) then
  1722. result:=true
  1723. else
  1724. if report_errors then
  1725. CGMessagePos(hp.fileinfo,errmsg);
  1726. mayberesettypeconvs;
  1727. exit;
  1728. end
  1729. else if hp.nodetype=blockn then
  1730. begin
  1731. hp2:=tblocknode(hp).statements;
  1732. if assigned(hp2) then
  1733. begin
  1734. if hp2.nodetype<>statementn then
  1735. internalerror(2006110801);
  1736. while assigned(tstatementnode(hp2).next) do
  1737. hp2:=tstatementnode(hp2).next;
  1738. hp:=tstatementnode(hp2).statement;
  1739. end
  1740. else
  1741. begin
  1742. if report_errors then
  1743. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  1744. mayberesettypeconvs;
  1745. exit;
  1746. end;
  1747. end
  1748. else
  1749. begin
  1750. if report_errors then
  1751. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  1752. mayberesettypeconvs;
  1753. exit;
  1754. end;
  1755. end;
  1756. inlinen :
  1757. begin
  1758. if ((valid_const in opts) and
  1759. (tinlinenode(hp).inlinenumber in [in_typeof_x])) or
  1760. (tinlinenode(hp).inlinenumber in [in_unaligned_x,in_aligned_x,in_volatile_x]) then
  1761. result:=true
  1762. else
  1763. if report_errors then
  1764. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  1765. mayberesettypeconvs;
  1766. exit;
  1767. end;
  1768. nothingn :
  1769. begin
  1770. { generics can generate nothing nodes, just allow everything }
  1771. if df_generic in current_procinfo.procdef.defoptions then
  1772. result:=true
  1773. else if report_errors then
  1774. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  1775. mayberesettypeconvs;
  1776. exit;
  1777. end;
  1778. loadn :
  1779. begin
  1780. case tloadnode(hp).symtableentry.typ of
  1781. absolutevarsym,
  1782. staticvarsym,
  1783. localvarsym,
  1784. paravarsym :
  1785. begin
  1786. { loop counter? }
  1787. if not(Valid_Const in opts) and
  1788. (vo_is_loop_counter in tabstractvarsym(tloadnode(hp).symtableentry).varoptions) then
  1789. begin
  1790. if report_errors then
  1791. CGMessage1(parser_e_illegal_assignment_to_count_var,tloadnode(hp).symtableentry.realname);
  1792. mayberesettypeconvs;
  1793. exit;
  1794. end;
  1795. { read-only variable? }
  1796. if (tabstractvarsym(tloadnode(hp).symtableentry).varspez in [vs_const,vs_constref,vs_final]) then
  1797. begin
  1798. result:=constaccessok(tabstractvarsym(tloadnode(hp).symtableentry));
  1799. mayberesettypeconvs;
  1800. exit;
  1801. end;
  1802. result:=true;
  1803. mayberesettypeconvs;
  1804. exit;
  1805. end;
  1806. procsym :
  1807. begin
  1808. if (Valid_Const in opts) then
  1809. result:=true
  1810. else
  1811. if report_errors then
  1812. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  1813. mayberesettypeconvs;
  1814. exit;
  1815. end;
  1816. labelsym :
  1817. begin
  1818. if (Valid_Addr in opts) then
  1819. result:=true
  1820. else
  1821. if report_errors then
  1822. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  1823. mayberesettypeconvs;
  1824. exit;
  1825. end;
  1826. constsym:
  1827. begin
  1828. if (tconstsym(tloadnode(hp).symtableentry).consttyp in [constresourcestring,constwresourcestring]) and
  1829. (valid_addr in opts) then
  1830. result:=true
  1831. else
  1832. if report_errors then
  1833. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  1834. mayberesettypeconvs;
  1835. exit;
  1836. end;
  1837. else
  1838. begin
  1839. if report_errors then
  1840. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  1841. mayberesettypeconvs;
  1842. exit;
  1843. end;
  1844. end;
  1845. end;
  1846. else
  1847. begin
  1848. if report_errors then
  1849. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  1850. mayberesettypeconvs;
  1851. exit;
  1852. end;
  1853. end;
  1854. end;
  1855. mayberesettypeconvs;
  1856. end;
  1857. function valid_for_var(p:tnode; report_errors: boolean):boolean;
  1858. begin
  1859. valid_for_var:=valid_for_assign(p,[valid_range],report_errors);
  1860. end;
  1861. function valid_for_formal_var(p : tnode; report_errors: boolean) : boolean;
  1862. begin
  1863. valid_for_formal_var:=valid_for_assign(p,[valid_void,valid_range],report_errors);
  1864. end;
  1865. function valid_for_formal_constref(p : tnode; report_errors: boolean) : boolean;
  1866. begin
  1867. valid_for_formal_constref:=(p.resultdef.typ=formaldef) or
  1868. valid_for_assign(p,[valid_void,valid_range],report_errors);
  1869. end;
  1870. function valid_for_formal_const(p : tnode; report_errors: boolean) : boolean;
  1871. begin
  1872. valid_for_formal_const:=(p.resultdef.typ=formaldef) or
  1873. valid_for_assign(p,[valid_void,valid_const,valid_property,valid_range],report_errors);
  1874. end;
  1875. function valid_for_assignment(p:tnode; report_errors: boolean):boolean;
  1876. begin
  1877. valid_for_assignment:=valid_for_assign(p,[valid_property,valid_packed],report_errors);
  1878. end;
  1879. function valid_for_loopvar(p:tnode; report_errors: boolean):boolean;
  1880. begin
  1881. valid_for_loopvar:=valid_for_assign(p,[valid_property],report_errors);
  1882. end;
  1883. function valid_for_addr(p : tnode; report_errors: boolean) : boolean;
  1884. begin
  1885. result:=valid_for_assign(p,[valid_const,valid_addr,valid_void],report_errors);
  1886. end;
  1887. procedure var_para_allowed(var eq:tequaltype;def_from,def_to:Tdef; fromnode: tnode);
  1888. begin
  1889. { Note: eq must be already valid, it will only be updated! }
  1890. case def_to.typ of
  1891. formaldef :
  1892. begin
  1893. { all types can be passed to a formaldef,
  1894. but it is not the prefered way }
  1895. if not is_constnode(fromnode) then
  1896. eq:=te_convert_l6
  1897. else
  1898. eq:=te_incompatible;
  1899. end;
  1900. orddef :
  1901. begin
  1902. { allows conversion from word to integer and
  1903. byte to shortint, but only for TP7 compatibility }
  1904. if (m_tp7 in current_settings.modeswitches) and
  1905. (def_from.typ=orddef) and
  1906. (def_from.size=def_to.size) then
  1907. eq:=te_convert_l1;
  1908. end;
  1909. arraydef :
  1910. begin
  1911. if is_open_array(def_to) then
  1912. begin
  1913. if is_dynamic_array(def_from) and
  1914. equal_defs(tarraydef(def_from).elementdef,tarraydef(def_to).elementdef) then
  1915. eq:=te_convert_l2
  1916. else
  1917. if equal_defs(def_from,tarraydef(def_to).elementdef) then
  1918. eq:=te_convert_l3;
  1919. end;
  1920. end;
  1921. pointerdef :
  1922. begin
  1923. { an implicit pointer conversion is allowed }
  1924. if (def_from.typ=pointerdef) then
  1925. eq:=te_convert_l1;
  1926. end;
  1927. stringdef :
  1928. begin
  1929. { all shortstrings are allowed, size is not important }
  1930. if is_shortstring(def_from) and
  1931. is_shortstring(def_to) then
  1932. eq:=te_equal;
  1933. end;
  1934. objectdef :
  1935. begin
  1936. { child objects can be also passed }
  1937. { in non-delphi mode, otherwise }
  1938. { they must match exactly, except }
  1939. { if they are objects }
  1940. if (def_from.typ=objectdef) and
  1941. (
  1942. (tobjectdef(def_from).objecttype=odt_object) and
  1943. (tobjectdef(def_to).objecttype=odt_object)
  1944. ) and
  1945. (def_is_related(tobjectdef(def_from),tobjectdef(def_to))) then
  1946. eq:=te_convert_l1;
  1947. end;
  1948. filedef :
  1949. begin
  1950. { an implicit file conversion is also allowed }
  1951. { from a typed file to an untyped one }
  1952. if (def_from.typ=filedef) and
  1953. (tfiledef(def_from).filetyp = ft_typed) and
  1954. (tfiledef(def_to).filetyp = ft_untyped) then
  1955. eq:=te_convert_l1;
  1956. end;
  1957. else
  1958. ;
  1959. end;
  1960. end;
  1961. procedure para_allowed(var eq:tequaltype;p:tcallparanode;def_to:tdef);
  1962. var
  1963. acn: tarrayconstructornode;
  1964. realprocdef: tprocdef;
  1965. tmpeq: tequaltype;
  1966. begin
  1967. { Note: eq must be already valid, it will only be updated! }
  1968. case def_to.typ of
  1969. stringdef :
  1970. begin
  1971. { to support ansi/long/wide strings in a proper way }
  1972. { string and string[10] are assumed as equal }
  1973. { when searching the correct overloaded procedure }
  1974. if (p.resultdef.typ=stringdef) and
  1975. (tstringdef(def_to).stringtype=tstringdef(p.resultdef).stringtype) and
  1976. (tstringdef(def_to).encoding=tstringdef(p.resultdef).encoding) then
  1977. eq:=te_equal
  1978. end;
  1979. formaldef,
  1980. setdef :
  1981. begin
  1982. { set can also be a not yet converted array constructor }
  1983. if (p.resultdef.typ=arraydef) and
  1984. is_array_constructor(p.resultdef) and
  1985. not is_variant_array(p.resultdef) then
  1986. eq:=te_equal;
  1987. end;
  1988. procvardef :
  1989. begin
  1990. tmpeq:=te_incompatible;
  1991. { in tp/macpas mode proc -> procvar is allowed }
  1992. if ((m_tp_procvar in current_settings.modeswitches) or
  1993. (m_mac_procvar in current_settings.modeswitches)) and
  1994. (p.left.nodetype=calln) then
  1995. tmpeq:=proc_to_procvar_equal(tprocdef(tcallnode(p.left).procdefinition),tprocvardef(def_to),false);
  1996. if (tmpeq=te_incompatible) and
  1997. (m_nested_procvars in current_settings.modeswitches) and
  1998. is_proc2procvar_load(p.left,realprocdef) then
  1999. tmpeq:=proc_to_procvar_equal(realprocdef,tprocvardef(def_to),false);
  2000. if (tmpeq=te_incompatible) and
  2001. (m_mac in current_settings.modeswitches) and
  2002. is_ambiguous_funcret_load(p.left,realprocdef) then
  2003. tmpeq:=proc_to_procvar_equal(realprocdef,tprocvardef(def_to),false);
  2004. if tmpeq<>te_incompatible then
  2005. eq:=tmpeq;
  2006. end;
  2007. objectdef :
  2008. begin
  2009. tmpeq:=te_incompatible;
  2010. { in tp/macpas mode proc -> funcref is allowed }
  2011. if ((m_tp_procvar in current_settings.modeswitches) or
  2012. (m_mac_procvar in current_settings.modeswitches)) and
  2013. (p.left.nodetype=calln) and
  2014. is_invokable(def_to) then
  2015. tmpeq:=proc_to_funcref_conv(tprocdef(tcallnode(p.left).procdefinition),tobjectdef(def_to));
  2016. if tmpeq<>te_incompatible then
  2017. eq:=tmpeq;
  2018. end;
  2019. arraydef :
  2020. begin
  2021. { an arrayconstructor of proccalls may have to be converted to
  2022. an array of procvars }
  2023. if ((m_tp_procvar in current_settings.modeswitches) or
  2024. (m_mac_procvar in current_settings.modeswitches)) and
  2025. (tarraydef(def_to).elementdef.typ=procvardef) and
  2026. is_array_constructor(p.resultdef) and
  2027. not is_variant_array(p.resultdef) then
  2028. begin
  2029. acn:=tarrayconstructornode(p.left);
  2030. if assigned(acn.left) then
  2031. begin
  2032. eq:=te_exact;
  2033. while assigned(acn) and
  2034. (eq<>te_incompatible) do
  2035. begin
  2036. if (acn.left.nodetype=calln) then
  2037. tmpeq:=proc_to_procvar_equal(tprocdef(tcallnode(acn.left).procdefinition),tprocvardef(tarraydef(def_to).elementdef),false)
  2038. else
  2039. tmpeq:=compare_defs(acn.left.resultdef,tarraydef(def_to).elementdef,acn.left.nodetype);
  2040. if tmpeq<eq then
  2041. eq:=tmpeq;
  2042. acn:=tarrayconstructornode(acn.right);
  2043. end;
  2044. end
  2045. end;
  2046. end;
  2047. else
  2048. ;
  2049. end;
  2050. end;
  2051. function allowenumop(nt:tnodetype):boolean;
  2052. begin
  2053. result:=(nt in [equaln,unequaln,ltn,lten,gtn,gten]) or
  2054. ((cs_allow_enum_calc in current_settings.localswitches) and
  2055. (nt in [addn,subn]));
  2056. end;
  2057. {****************************************************************************
  2058. TCallCandidates
  2059. ****************************************************************************}
  2060. constructor tcallcandidates.init(sym:tprocsym;st:TSymtable;ppn:tnode;flags:tcallcandidatesflags;spezcontext:tspecializationcontext);
  2061. begin
  2062. if not assigned(sym) then
  2063. internalerror(200411015);
  2064. FOperator:=NOTOKEN;
  2065. FProcsym:=sym;
  2066. FProcsymtable:=st;
  2067. FParanode:=ppn;
  2068. create_candidate_list(flags,spezcontext);
  2069. end;
  2070. constructor tcallcandidates.init_operator(op:ttoken;ppn:tnode);
  2071. begin
  2072. FOperator:=op;
  2073. FProcsym:=nil;
  2074. FProcsymtable:=nil;
  2075. FParanode:=ppn;
  2076. create_candidate_list([],nil);
  2077. end;
  2078. destructor tcallcandidates.done;
  2079. var
  2080. hpnext,
  2081. hp : pcandidate;
  2082. psym : tprocsym;
  2083. i : longint;
  2084. sym : tsym;
  2085. begin
  2086. FIgnoredCandidateProcs.free;
  2087. { free any symbols for anonymous parameter types that we're used for
  2088. specialization when no specialization was picked }
  2089. TFPList.FreeAndNilObjects(FParaAnonSyms);
  2090. hp:=FCandidateProcs;
  2091. while assigned(hp) do
  2092. begin
  2093. hpnext:=hp^.next;
  2094. { free those procdef specializations that are not owned (thus were discarded) }
  2095. if hp^.data.is_specialization and not hp^.data.is_registered then
  2096. begin
  2097. { also remove the procdef from its symbol's procdeflist }
  2098. psym:=tprocsym(hp^.data.procsym);
  2099. for i:=0 to psym.procdeflist.count-1 do
  2100. begin
  2101. if psym.procdeflist[i]=hp^.data then
  2102. begin
  2103. psym.procdeflist.delete(i);
  2104. break;
  2105. end;
  2106. end;
  2107. hp^.data.free;
  2108. end;
  2109. dispose(hp);
  2110. hp:=hpnext;
  2111. end;
  2112. end;
  2113. procedure tcallcandidates.collect_overloads_in_struct(structdef:tabstractrecorddef;ProcdefOverloadList:TFPObjectList;flags:tcallcandidatesflags;spezcontext:tspecializationcontext);
  2114. var
  2115. changedhierarchy : boolean;
  2116. function processprocsym(srsym:tprocsym; out foundanything: boolean):boolean;
  2117. var
  2118. j : integer;
  2119. pd : tprocdef;
  2120. begin
  2121. { add all definitions }
  2122. result:=false;
  2123. foundanything:=false;
  2124. { try to specialize the procsym }
  2125. if srsym.could_be_implicitly_specialized and
  2126. try_implicit_specialization(srsym,FParaNode,ProcdefOverloadList,FParaAnonSyms,tsym(FProcsym),result) then
  2127. foundanything:=true;
  2128. for j:=0 to srsym.ProcdefList.Count-1 do
  2129. begin
  2130. pd:=tprocdef(srsym.ProcdefList[j]);
  2131. if not finalize_specialization(pd,spezcontext) then
  2132. continue;
  2133. if (po_ignore_for_overload_resolution in pd.procoptions) then
  2134. begin
  2135. TFPList.AddOnDemand(FIgnoredCandidateProcs,pd);
  2136. continue;
  2137. end;
  2138. { in case of anonymous inherited, only match procdefs identical
  2139. to the current one (apart from hidden parameters), rather than
  2140. anything compatible to the parameters -- except in case of
  2141. the presence of a messagestr/int, in which case those have to
  2142. match exactly }
  2143. if cc_anoninherited in flags then
  2144. if po_msgint in current_procinfo.procdef.procoptions then
  2145. begin
  2146. if not(po_msgint in pd.procoptions) or
  2147. (pd.messageinf.i<>current_procinfo.procdef.messageinf.i) then
  2148. continue
  2149. end
  2150. else if po_msgstr in current_procinfo.procdef.procoptions then
  2151. begin
  2152. if not(po_msgstr in pd.procoptions) or
  2153. (pd.messageinf.str^<>current_procinfo.procdef.messageinf.str^) then
  2154. continue
  2155. end
  2156. else if (compare_paras(current_procinfo.procdef.paras,pd.paras,cp_all,[cpo_ignorehidden])<te_equal) then
  2157. continue;
  2158. foundanything:=true;
  2159. { Store first procsym found }
  2160. if not assigned(FProcsym) then
  2161. FProcsym:=tprocsym(srsym);
  2162. if po_overload in pd.procoptions then
  2163. result:=true;
  2164. { if the hierarchy had been changed we need to check for duplicates }
  2165. if not changedhierarchy or (ProcdefOverloadList.IndexOf(pd)<0) then
  2166. ProcdefOverloadList.Add(pd);
  2167. end;
  2168. end;
  2169. function processhelper(hashedid:THashedIDString;helperdef:tobjectdef):boolean;
  2170. var
  2171. srsym : tsym;
  2172. hasoverload,foundanything : boolean;
  2173. begin
  2174. result:=false;
  2175. srsym:=nil;
  2176. hasoverload:=false;
  2177. while assigned(helperdef) do
  2178. begin
  2179. srsym:=tsym(helperdef.symtable.FindWithHash(hashedid));
  2180. if assigned(srsym) and
  2181. { Delphi allows hiding a property by a procedure with the same name }
  2182. (srsym.typ=procsym) and
  2183. (tprocsym(srsym).procdeflist.count>0) then
  2184. begin
  2185. hasoverload:=processprocsym(tprocsym(srsym),foundanything);
  2186. { when there is no explicit overload we stop searching }
  2187. if foundanything and
  2188. not hasoverload then
  2189. break;
  2190. end;
  2191. helperdef:=helperdef.childof;
  2192. end;
  2193. if not hasoverload and assigned(srsym) then
  2194. exit(true);
  2195. end;
  2196. var
  2197. srsym : tsym;
  2198. hashedid : THashedIDString;
  2199. hasoverload,
  2200. foundanything : boolean;
  2201. extendeddef : tabstractrecorddef;
  2202. helperdef : tobjectdef;
  2203. helperlist : TFPObjectList;
  2204. i : integer;
  2205. begin
  2206. if FOperator=NOTOKEN then
  2207. hashedid.id:=FProcsym.name
  2208. else
  2209. hashedid.id:=overloaded_names[FOperator];
  2210. hasoverload:=false;
  2211. extendeddef:=nil;
  2212. changedhierarchy:=false;
  2213. while assigned(structdef) do
  2214. begin
  2215. { first search in helpers for this type }
  2216. if ((structdef.typ=recorddef) or
  2217. (
  2218. (structdef.typ=objectdef) and
  2219. (tobjectdef(structdef).objecttype in objecttypes_with_helpers)
  2220. )
  2221. )
  2222. and (cc_searchhelpers in flags) then
  2223. begin
  2224. if m_multi_helpers in current_settings.modeswitches then
  2225. begin
  2226. helperlist:=get_objectpascal_helpers(structdef);
  2227. if assigned(helperlist) and (helperlist.count>0) then
  2228. begin
  2229. i:=helperlist.count-1;
  2230. repeat
  2231. helperdef:=tobjectdef(helperlist[i]);
  2232. if (helperdef.owner.symtabletype in [staticsymtable,globalsymtable]) or
  2233. is_visible_for_object(helperdef.typesym,helperdef) then
  2234. if processhelper(hashedid,helperdef) then
  2235. exit;
  2236. dec(i);
  2237. until (i<0);
  2238. end;
  2239. end
  2240. else if search_last_objectpascal_helper(structdef,nil,helperdef) and processhelper(hashedid,helperdef) then
  2241. exit;
  2242. end;
  2243. { now search in the type itself }
  2244. srsym:=tsym(structdef.symtable.FindWithHash(hashedid));
  2245. if assigned(srsym) and
  2246. { Delphi allows hiding a property by a procedure with the same name }
  2247. (srsym.typ=procsym) then
  2248. begin
  2249. hasoverload:=processprocsym(tprocsym(srsym),foundanything);
  2250. { when there is no explicit overload we stop searching }
  2251. if foundanything and
  2252. not hasoverload then
  2253. break;
  2254. end;
  2255. if is_objectpascal_helper(structdef) and
  2256. (
  2257. (tobjectdef(structdef).extendeddef.typ=recorddef) or
  2258. (
  2259. (tobjectdef(structdef).extendeddef.typ=objectdef) and
  2260. (tobjectdef(tobjectdef(structdef).extendeddef).objecttype in objecttypes_with_helpers)
  2261. )
  2262. ) then
  2263. begin
  2264. { remember the first extendeddef of the hierarchy }
  2265. if not assigned(extendeddef) then
  2266. extendeddef:=tabstractrecorddef(tobjectdef(structdef).extendeddef);
  2267. { search methods in the extended type as well }
  2268. srsym:=tprocsym(tabstractrecorddef(tobjectdef(structdef).extendeddef).symtable.FindWithHash(hashedid));
  2269. if assigned(srsym) and
  2270. { Delphi allows hiding a property by a procedure with the same name }
  2271. (srsym.typ=procsym) and
  2272. (tprocsym(srsym).procdeflist.count>0) then
  2273. begin
  2274. hasoverload:=processprocsym(tprocsym(srsym),foundanything);
  2275. { when there is no explicit overload we stop searching }
  2276. if foundanything and
  2277. not hasoverload then
  2278. break;
  2279. end;
  2280. end;
  2281. { next parent }
  2282. if (structdef.typ=objectdef) then
  2283. structdef:=tobjectdef(structdef).childof
  2284. else
  2285. structdef:=nil;
  2286. { switch over to the extended def's hierarchy }
  2287. if not assigned(structdef) and assigned(extendeddef) then
  2288. begin
  2289. structdef:=extendeddef;
  2290. extendeddef:=nil;
  2291. changedhierarchy:=true;
  2292. end;
  2293. end;
  2294. end;
  2295. procedure tcallcandidates.collect_overloads_in_units(ProcdefOverloadList:TFPObjectList; flags:tcallcandidatesflags;spezcontext:tspecializationcontext);
  2296. var
  2297. j : integer;
  2298. pd : tprocdef;
  2299. srsymtable : TSymtable;
  2300. srsym : tsym;
  2301. checkstack : psymtablestackitem;
  2302. hashedid : THashedIDString;
  2303. foundanything,
  2304. hasoverload : boolean;
  2305. begin
  2306. { we search all overloaded operator definitions in the symtablestack. The found
  2307. entries are only added to the procs list and not the procsym, because
  2308. the list can change in every situation }
  2309. if FOperator=NOTOKEN then
  2310. begin
  2311. if not (cc_objcidcall in flags) then
  2312. hashedid.id:=FProcsym.name
  2313. else
  2314. hashedid.id:=class_helper_prefix+FProcsym.name;
  2315. end
  2316. else
  2317. hashedid.id:=overloaded_names[FOperator];
  2318. checkstack:=symtablestack.stack;
  2319. if assigned(FProcsymtable) then
  2320. begin
  2321. while assigned(checkstack) and
  2322. (checkstack^.symtable<>FProcsymtable) do
  2323. checkstack:=checkstack^.next;
  2324. end;
  2325. while assigned(checkstack) do
  2326. begin
  2327. srsymtable:=checkstack^.symtable;
  2328. { if the unit in which the routine has to be searched has been
  2329. specified explicitly, stop searching after its symtable(s) have
  2330. been checked (can be both the static and the global symtable
  2331. in case it's the current unit itself) }
  2332. if (cc_explicitunit in flags) and
  2333. (FProcsymtable.symtabletype in [globalsymtable,staticsymtable]) and
  2334. (srsymtable.moduleid<>FProcsymtable.moduleid) then
  2335. break;
  2336. if (srsymtable.symtabletype in [localsymtable,staticsymtable,globalsymtable]) and
  2337. (
  2338. (FOperator=NOTOKEN) or
  2339. (sto_has_operator in srsymtable.tableoptions)
  2340. )
  2341. then
  2342. begin
  2343. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  2344. if assigned(srsym) and
  2345. (srsym.typ=procsym) and
  2346. (
  2347. (tprocsym(srsym).procdeflist.count>0) or
  2348. (sp_generic_dummy in srsym.symoptions)
  2349. ) then
  2350. begin
  2351. { add all definitions }
  2352. hasoverload:=false;
  2353. foundanything:=false;
  2354. if tprocsym(srsym).could_be_implicitly_specialized then
  2355. foundanything:=try_implicit_specialization(srsym,FParaNode,ProcdefOverloadList,FParaAnonSyms,tsym(FProcsym),hasoverload);
  2356. for j:=0 to tprocsym(srsym).ProcdefList.Count-1 do
  2357. begin
  2358. pd:=tprocdef(tprocsym(srsym).ProcdefList[j]);
  2359. if not finalize_specialization(pd,spezcontext) then
  2360. continue;
  2361. if (po_ignore_for_overload_resolution in pd.procoptions) then
  2362. begin
  2363. TFPList.AddOnDemand(FIgnoredCandidateProcs,pd);
  2364. continue;
  2365. end;
  2366. { Store first procsym found }
  2367. if not assigned(FProcsym) then
  2368. FProcsym:=tprocsym(srsym);
  2369. if po_overload in pd.procoptions then
  2370. hasoverload:=true;
  2371. ProcdefOverloadList.Add(pd);
  2372. foundanything:=true;
  2373. end;
  2374. { when there is no explicit overload we stop searching,
  2375. except for Objective-C methods called via id }
  2376. if foundanything and
  2377. not hasoverload and
  2378. not (cc_objcidcall in flags) then
  2379. break;
  2380. end;
  2381. end;
  2382. checkstack:=checkstack^.next
  2383. end;
  2384. end;
  2385. procedure tcallcandidates.create_candidate_list(flags:tcallcandidatesflags;spezcontext:tspecializationcontext);
  2386. var
  2387. j : integer;
  2388. pd : tprocdef;
  2389. hp : pcandidate;
  2390. pt : tcallparanode;
  2391. found,
  2392. added : boolean;
  2393. st : TSymtable;
  2394. contextstructdef : tabstractrecorddef;
  2395. ProcdefOverloadList : TFPObjectList;
  2396. cpoptions : tcompare_paras_options;
  2397. begin
  2398. FCandidateProcs:=nil;
  2399. { Find all available overloads for this procsym }
  2400. ProcdefOverloadList:=TFPObjectList.Create(false);
  2401. if not (cc_objcidcall in flags) and
  2402. (FOperator=NOTOKEN) and
  2403. (FProcsym.owner.symtabletype in [objectsymtable,recordsymtable]) then
  2404. collect_overloads_in_struct(tabstractrecorddef(FProcsym.owner.defowner),ProcdefOverloadList,flags,spezcontext)
  2405. else
  2406. if (FOperator<>NOTOKEN) then
  2407. begin
  2408. { check operands and if they contain records then search in records,
  2409. then search in unit }
  2410. pt:=tcallparanode(FParaNode);
  2411. while assigned(pt) do
  2412. begin
  2413. if (pt.resultdef.typ=recorddef) and
  2414. (sto_has_operator in tabstractrecorddef(pt.resultdef).symtable.tableoptions) then
  2415. collect_overloads_in_struct(tabstractrecorddef(pt.resultdef),ProcdefOverloadList,flags,spezcontext);
  2416. pt:=tcallparanode(pt.right);
  2417. end;
  2418. collect_overloads_in_units(ProcdefOverloadList,flags,spezcontext);
  2419. end
  2420. else
  2421. collect_overloads_in_units(ProcdefOverloadList,flags,spezcontext);
  2422. { determine length of parameter list.
  2423. for operators also enable the variant-operators if
  2424. a variant parameter is passed }
  2425. FParalength:=0;
  2426. FAllowVariant:=(FOperator=NOTOKEN);
  2427. pt:=tcallparanode(FParaNode);
  2428. while assigned(pt) do
  2429. begin
  2430. if (pt.resultdef.typ=variantdef) then
  2431. FAllowVariant:=true;
  2432. inc(FParalength);
  2433. pt:=tcallparanode(pt.right);
  2434. end;
  2435. { when the class passed is defined in this unit we
  2436. need to use the scope of that class. This is a trick
  2437. that can be used to access protected members in other
  2438. units. At least kylix supports it this way (PFV) }
  2439. if assigned(FProcSymtable) and
  2440. (
  2441. (FProcSymtable.symtabletype in [ObjectSymtable,recordsymtable]) or
  2442. ((FProcSymtable.symtabletype=withsymtable) and
  2443. (FProcSymtable.defowner.typ in [objectdef,recorddef]))
  2444. ) and
  2445. (FProcSymtable.defowner.owner.symtabletype in [globalsymtable,staticsymtable,objectsymtable,recordsymtable]) and
  2446. FProcSymtable.defowner.owner.iscurrentunit then
  2447. contextstructdef:=tabstractrecorddef(FProcSymtable.defowner)
  2448. else
  2449. contextstructdef:=current_structdef;
  2450. { symtable is needed later to calculate the distance }
  2451. if assigned(FProcsym) then
  2452. st:=FProcsym.Owner
  2453. else
  2454. st:=nil;
  2455. { Process all found overloads }
  2456. for j:=0 to ProcdefOverloadList.Count-1 do
  2457. begin
  2458. pd:=tprocdef(ProcdefOverloadList[j]);
  2459. added:=false;
  2460. { only when the # of parameter are supported by the procedure and
  2461. it is visible }
  2462. {$ifdef DISABLE_FAST_OVERLOAD_PATCH}
  2463. if (FParalength>=pd.minparacount) and
  2464. {$else}
  2465. if (pd.seenmarker<>pointer(@self)) and (FParalength>=pd.minparacount) and
  2466. {$endif}
  2467. (
  2468. (
  2469. (cc_allowdefaultparas in flags) and
  2470. (
  2471. (FParalength<=pd.maxparacount) or
  2472. (po_varargs in pd.procoptions)
  2473. )
  2474. ) or
  2475. (
  2476. not (cc_allowdefaultparas in flags) and
  2477. (FParalength=pd.maxparacount)
  2478. )
  2479. ) and
  2480. (
  2481. (cc_ignorevisibility in flags) or
  2482. (
  2483. pd.is_specialization and not assigned(pd.owner) and
  2484. (
  2485. not (pd.genericdef.owner.symtabletype in [objectsymtable,recordsymtable]) or
  2486. is_visible_for_object(tprocdef(pd.genericdef),contextstructdef)
  2487. )
  2488. ) or
  2489. (
  2490. assigned(pd.owner) and
  2491. (
  2492. not (pd.owner.symtabletype in [objectsymtable,recordsymtable]) or
  2493. is_visible_for_object(pd,contextstructdef)
  2494. )
  2495. )
  2496. ) then
  2497. begin
  2498. { don't add duplicates, only compare visible parameters for the user }
  2499. cpoptions:=[cpo_ignorehidden];
  2500. if (po_compilerproc in pd.procoptions) then
  2501. cpoptions:=cpoptions+[cpo_compilerproc];
  2502. if (po_rtlproc in pd.procoptions) then
  2503. cpoptions:=cpoptions+[cpo_rtlproc];
  2504. found:=false;
  2505. hp:=FCandidateProcs;
  2506. {$ifdef DISABLE_FAST_OVERLOAD_PATCH}
  2507. while assigned(hp) do
  2508. begin
  2509. if (compare_paras(hp^.data.paras,pd.paras,cp_value_equal_const,cpoptions)>=te_equal) and
  2510. (not(po_objc in pd.procoptions) or
  2511. (pd.messageinf.str^=hp^.data.messageinf.str^)) then
  2512. begin
  2513. found:=true;
  2514. break;
  2515. end;
  2516. hp:=hp^.next;
  2517. end;
  2518. {$endif}
  2519. if not found then
  2520. begin
  2521. proc_add(st,pd);
  2522. added:=true;
  2523. {$ifndef DISABLE_FAST_OVERLOAD_PATCH}
  2524. pd.seenmarker:=pointer(@self);
  2525. {$endif}
  2526. end;
  2527. end;
  2528. { we need to remove all specializations that were not used from their
  2529. procsyms as no code must be generated for them (if they are used
  2530. later on they'll be added like the ones that were used now) }
  2531. if not added and assigned(spezcontext) and not pd.is_registered then
  2532. begin
  2533. if tprocsym(pd.procsym).procdeflist.extract(pd)<>pd then
  2534. internalerror(20150828);
  2535. pd.free;
  2536. end;
  2537. end;
  2538. {$ifndef DISABLE_FAST_OVERLOAD_PATCH}
  2539. {cleanup modified duplicate pd markers}
  2540. hp := FCandidateProcs;
  2541. while assigned(hp) do begin
  2542. hp^.data.seenmarker := nil;
  2543. hp := hp^.next;
  2544. end;
  2545. {$endif}
  2546. calc_distance(st,flags);
  2547. ProcdefOverloadList.Free;
  2548. end;
  2549. procedure tcallcandidates.calc_distance(st_root: tsymtable; flags:tcallcandidatesflags);
  2550. var
  2551. pd:tprocdef;
  2552. candidate:pcandidate;
  2553. st: tsymtable;
  2554. begin
  2555. { Give a small penalty for overloaded methods not defined in the
  2556. current class/unit }
  2557. st:=nil;
  2558. if (cc_objcidcall in flags) or
  2559. not assigned(st_root) or
  2560. not assigned(st_root.defowner) or
  2561. (st_root.defowner.typ<>objectdef) then
  2562. st:=st_root
  2563. else
  2564. repeat
  2565. { In case of a method, st_root is the symtable of the first found
  2566. procsym with the called method's name, but this procsym may not
  2567. contain any of the overloads that match the used parameters (which
  2568. are the procdefs that have been collected as candidates) -> walk
  2569. up the class hierarchy and look for the first class that actually
  2570. defines at least one of the candidate procdefs.
  2571. The reason is that we will penalise methods in other classes/
  2572. symtables, so if we pick a symtable that does not contain any of
  2573. the candidates, this won't help with picking the best/
  2574. most-inner-scoped one (since all of them will be penalised) }
  2575. candidate:=FCandidateProcs;
  2576. { the current class contains one of the candidates? }
  2577. while assigned(candidate) do
  2578. begin
  2579. pd:=candidate^.data;
  2580. if pd.owner=st_root then
  2581. begin
  2582. { yes -> choose this class }
  2583. st:=st_root;
  2584. break;
  2585. end;
  2586. candidate:=candidate^.next;
  2587. end;
  2588. { None found -> go to parent class }
  2589. if not assigned(st) then
  2590. begin
  2591. if not assigned(st_root.defowner) then
  2592. internalerror(201605301);
  2593. { no more parent class -> take current class as root anyway
  2594. (could maybe happen in case of a class helper?) }
  2595. if not assigned(tobjectdef(st_root.defowner).childof) then
  2596. begin
  2597. st:=st_root;
  2598. break;
  2599. end;
  2600. st_root:=tobjectdef(st_root.defowner).childof.symtable;
  2601. end;
  2602. until assigned(st);
  2603. candidate:=FCandidateProcs;
  2604. { when calling Objective-C methods via id.method, then the found
  2605. procsym will be inside an arbitrary ObjectSymtable, and we don't
  2606. want to give the methods of that particular objcclass precedence
  2607. over other methods, so instead check against the symtable in
  2608. which this objcclass is defined }
  2609. if cc_objcidcall in flags then
  2610. st:=st.defowner.owner;
  2611. while assigned(candidate) do
  2612. begin
  2613. pd:=candidate^.data;
  2614. if st<>pd.owner then
  2615. candidate^.ordinal_distance:=candidate^.ordinal_distance+1.0;
  2616. candidate:=candidate^.next;
  2617. end;
  2618. end;
  2619. function tcallcandidates.proc_add(st:tsymtable;pd:tprocdef):pcandidate;
  2620. var
  2621. defaultparacnt : integer;
  2622. begin
  2623. { generate new candidate entry }
  2624. new(result);
  2625. fillchar(result^,sizeof(tcandidate),0);
  2626. result^.data:=pd;
  2627. result^.next:=FCandidateProcs;
  2628. FCandidateProcs:=result;
  2629. inc(FProccnt);
  2630. { Find last parameter, skip all default parameters
  2631. that are not passed. Ignore this skipping for varargs }
  2632. result^.firstparaidx:=pd.paras.count-1;
  2633. if not(po_varargs in pd.procoptions) then
  2634. begin
  2635. { ignore hidden parameters }
  2636. while (result^.firstparaidx>=0) and (vo_is_hidden_para in tparavarsym(pd.paras[result^.firstparaidx]).varoptions) do
  2637. dec(result^.firstparaidx);
  2638. defaultparacnt:=pd.maxparacount-FParalength;
  2639. if defaultparacnt>0 then
  2640. begin
  2641. if defaultparacnt>result^.firstparaidx+1 then
  2642. internalerror(200401141);
  2643. dec(result^.firstparaidx,defaultparacnt);
  2644. end;
  2645. end;
  2646. end;
  2647. procedure tcallcandidates.list(all:boolean);
  2648. var
  2649. hp : pcandidate;
  2650. begin
  2651. hp:=FCandidateProcs;
  2652. while assigned(hp) do
  2653. begin
  2654. if all or
  2655. (not hp^.invalid) then
  2656. MessagePos1(hp^.data.fileinfo,sym_h_param_list,hp^.data.fullprocname(false));
  2657. hp:=hp^.next;
  2658. end;
  2659. end;
  2660. {$ifdef EXTDEBUG}
  2661. procedure tcallcandidates.dump_info(lvl:longint);
  2662. function ParaTreeStr(p:tcallparanode):string;
  2663. begin
  2664. result:='';
  2665. while assigned(p) do
  2666. begin
  2667. if result<>'' then
  2668. result:=','+result;
  2669. result:=p.resultdef.typename+result;
  2670. p:=tcallparanode(p.right);
  2671. end;
  2672. end;
  2673. var
  2674. hp : pcandidate;
  2675. i : integer;
  2676. currpara : tparavarsym;
  2677. begin
  2678. if not CheckVerbosity(lvl) then
  2679. exit;
  2680. Comment(lvl+V_LineInfo,'Overloaded callnode: '+FProcsym.name+'('+ParaTreeStr(tcallparanode(FParaNode))+')');
  2681. hp:=FCandidateProcs;
  2682. while assigned(hp) do
  2683. begin
  2684. Comment(lvl,' '+hp^.data.fullprocname(false));
  2685. if (hp^.invalid) then
  2686. Comment(lvl,' invalid')
  2687. else
  2688. begin
  2689. Comment(lvl,' ex: '+tostr(hp^.exact_count)+
  2690. ' eq: '+tostr(hp^.equal_count)+
  2691. ' l1: '+tostr(hp^.cl1_count)+
  2692. ' l2: '+tostr(hp^.cl2_count)+
  2693. ' l3: '+tostr(hp^.cl3_count)+
  2694. ' l4: '+tostr(hp^.cl4_count)+
  2695. ' l5: '+tostr(hp^.cl5_count)+
  2696. ' l6: '+tostr(hp^.cl6_count)+
  2697. ' oper: '+tostr(hp^.coper_count)+
  2698. ' ord: '+realtostr(hp^.ordinal_distance));
  2699. { Print parameters in left-right order }
  2700. for i:=0 to hp^.data.paras.count-1 do
  2701. begin
  2702. currpara:=tparavarsym(hp^.data.paras[i]);
  2703. if not(vo_is_hidden_para in currpara.varoptions) then
  2704. Comment(lvl,' - '+currpara.vardef.typename+' : '+EqualTypeName[currpara.eqval]);
  2705. end;
  2706. end;
  2707. hp:=hp^.next;
  2708. end;
  2709. end;
  2710. {$endif EXTDEBUG}
  2711. procedure tcallcandidates.get_information;
  2712. var
  2713. hp : pcandidate;
  2714. currpara : tparavarsym;
  2715. paraidx : integer;
  2716. currparanr : byte;
  2717. rfh,rth : double;
  2718. obj_from,
  2719. obj_to : tobjectdef;
  2720. def_from,
  2721. def_to : tdef;
  2722. currpt,
  2723. pt : tcallparanode;
  2724. eq,
  2725. mineq : tequaltype;
  2726. convtype : tconverttype;
  2727. pdtemp,
  2728. pdoper : tprocdef;
  2729. releasecurrpt, check_valid_var : boolean;
  2730. cdoptions : tcompare_defs_options;
  2731. n : tnode;
  2732. {$push}
  2733. {$r-}
  2734. {$q-}
  2735. const
  2736. inf=1.0/0.0;
  2737. {$pop}
  2738. begin
  2739. cdoptions:=[cdo_check_operator];
  2740. if FAllowVariant then
  2741. include(cdoptions,cdo_allow_variant);
  2742. { process all procs }
  2743. hp:=FCandidateProcs;
  2744. while assigned(hp) do
  2745. begin
  2746. { We compare parameters in reverse order (right to left),
  2747. the firstpara is already pointing to the last parameter
  2748. were we need to start comparing }
  2749. currparanr:=FParalength;
  2750. paraidx:=hp^.firstparaidx;
  2751. while (paraidx>=0) and (vo_is_hidden_para in tparavarsym(hp^.data.paras[paraidx]).varoptions) do
  2752. dec(paraidx);
  2753. pt:=tcallparanode(FParaNode);
  2754. while assigned(pt) and (paraidx>=0) do
  2755. begin
  2756. currpara:=tparavarsym(hp^.data.paras[paraidx]);
  2757. { currpt can be changed from loadn to calln when a procvar
  2758. is passed. This is to prevent that the change is permanent }
  2759. currpt:=pt;
  2760. releasecurrpt:=false;
  2761. { Should we check if the callparanode.left is valid for var }
  2762. check_valid_var:=true;
  2763. { retrieve current parameter definitions to compares }
  2764. eq:=te_incompatible;
  2765. def_from:=currpt.resultdef;
  2766. def_to:=currpara.vardef;
  2767. if not(assigned(def_from)) then
  2768. internalerror(200212091);
  2769. if not(
  2770. assigned(def_to) or
  2771. ((po_varargs in hp^.data.procoptions) and
  2772. (currparanr>hp^.data.minparacount))
  2773. ) then
  2774. internalerror(200212092);
  2775. { Convert tp procvars when not expecting a procvar }
  2776. if (currpt.left.resultdef.typ=procvardef) and
  2777. not(def_to.typ in [procvardef,formaldef]) and
  2778. { if it doesn't require any parameters }
  2779. (tprocvardef(currpt.left.resultdef).minparacount=0) and
  2780. { Only convert to call when there is no overload or the return type
  2781. is compatible with the expected type. }
  2782. (
  2783. (count=1) or
  2784. (compare_defs_ext(tprocvardef(currpt.left.resultdef).returndef,def_to,nothingn,convtype,pdoper,[])>te_incompatible)
  2785. ) then
  2786. begin
  2787. releasecurrpt:=true;
  2788. currpt:=tcallparanode(pt.getcopy);
  2789. if maybe_call_procvar(currpt.left,true) then
  2790. begin
  2791. currpt.resultdef:=currpt.left.resultdef;
  2792. def_from:=currpt.left.resultdef;
  2793. end;
  2794. end;
  2795. { If we expect a procvar and the left is loadnode that
  2796. returns a procdef we need to find the correct overloaded
  2797. procdef that matches the expected procvar. The loadnode
  2798. temporary returned the first procdef (PFV) }
  2799. if (
  2800. (def_to.typ=procvardef) or
  2801. is_funcref(def_to)
  2802. ) and
  2803. (currpt.left.nodetype=loadn) and
  2804. (currpt.left.resultdef.typ=procdef) then
  2805. begin
  2806. if def_to.typ=procvardef then
  2807. pdtemp:=tprocsym(Tloadnode(currpt.left).symtableentry).Find_procdef_byprocvardef(Tprocvardef(def_to))
  2808. else
  2809. pdtemp:=tprocsym(tloadnode(currpt.left).symtableentry).find_procdef_byfuncrefdef(tobjectdef(def_to));
  2810. if assigned(pdtemp) then
  2811. begin
  2812. tloadnode(currpt.left).setprocdef(pdtemp,def_to.typ<>procvardef);
  2813. currpt.resultdef:=currpt.left.resultdef;
  2814. def_from:=currpt.left.resultdef;
  2815. end;
  2816. end;
  2817. { same as above, but for the case that we have a proc-2-procvar
  2818. conversion together with a load }
  2819. if (
  2820. (def_to.typ=procvardef) or
  2821. is_funcref(def_to)
  2822. ) and
  2823. (currpt.left.nodetype=typeconvn) and
  2824. (ttypeconvnode(currpt.left).convtype=tc_proc_2_procvar) and
  2825. (ttypeconvnode(currpt.left).totypedef=voidtype) and
  2826. not (nf_explicit in currpt.left.flags) and
  2827. (ttypeconvnode(currpt.left).left.nodetype=loadn) and
  2828. (ttypeconvnode(currpt.left).left.resultdef.typ=procdef) then
  2829. begin
  2830. if def_to.typ=procvardef then
  2831. pdtemp:=tprocsym(tloadnode(ttypeconvnode(currpt.left).left).symtableentry).Find_procdef_byprocvardef(Tprocvardef(def_to))
  2832. else
  2833. pdtemp:=tprocsym(tloadnode(ttypeconvnode(currpt.left).left).symtableentry).find_procdef_byfuncrefdef(tobjectdef(def_to));
  2834. if assigned(pdtemp) then
  2835. begin
  2836. tloadnode(ttypeconvnode(currpt.left).left).setprocdef(pdtemp,def_to.typ<>procvardef);
  2837. ttypeconvnode(currpt.left).totypedef:=cprocvardef.getreusableprocaddr(pdtemp,pc_normal);
  2838. ttypeconvnode(currpt.left).resultdef:=ttypeconvnode(currpt.left).totypedef;
  2839. def_from:=ttypeconvnode(currpt.left).resultdef;
  2840. end;
  2841. end;
  2842. { varargs are always equal, but not exact }
  2843. if (po_varargs in hp^.data.procoptions) and
  2844. (currparanr>hp^.data.minparacount) and
  2845. not is_array_of_const(def_from) and
  2846. not is_array_constructor(def_from) then
  2847. eq:=te_equal
  2848. else
  2849. { same definition -> exact }
  2850. if (def_from=def_to) then
  2851. eq:=te_exact
  2852. else
  2853. { for value and const parameters check if a integer is constant or
  2854. included in other integer -> equal and calc ordinal_distance }
  2855. if not(currpara.varspez in [vs_var,vs_out]) and
  2856. is_integer(def_from) and
  2857. is_integer(def_to) and
  2858. is_in_limit(def_from,def_to) then
  2859. begin
  2860. eq:=te_equal;
  2861. hp^.ordinal_distance:=hp^.ordinal_distance+
  2862. abs(bestreal(torddef(def_from).low)-bestreal(torddef(def_to).low));
  2863. rth:=bestreal(torddef(def_to).high);
  2864. rfh:=bestreal(torddef(def_from).high);
  2865. hp^.ordinal_distance:=hp^.ordinal_distance+abs(rth-rfh);
  2866. { Give wrong sign a small penalty, this is need to get a diffrence
  2867. from word->[longword,longint] }
  2868. if is_signed(def_from)<>is_signed(def_to) then
  2869. {$push}
  2870. {$r-}
  2871. {$q-}
  2872. hp^.ordinal_distance:=nextafter(hp^.ordinal_distance,inf);
  2873. {$pop}
  2874. end
  2875. else
  2876. { for value and const parameters check precision of real, give
  2877. penalty for loosing of precision. var and out parameters must match exactly }
  2878. if not(currpara.varspez in [vs_var,vs_out]) and
  2879. is_real_or_cextended(def_from) and
  2880. is_real_or_cextended(def_to) then
  2881. begin
  2882. eq:=te_equal;
  2883. if is_extended(def_to) then
  2884. rth:=4
  2885. else
  2886. if is_double (def_to) then
  2887. rth:=2
  2888. else
  2889. rth:=1;
  2890. if is_extended(def_from) then
  2891. rfh:=4
  2892. else
  2893. if is_double (def_from) then
  2894. rfh:=2
  2895. else
  2896. rfh:=1;
  2897. { penalty for shrinking of precision }
  2898. if rth<rfh then
  2899. rfh:=(rfh-rth)*16
  2900. else
  2901. rfh:=rth-rfh;
  2902. hp^.ordinal_distance:=hp^.ordinal_distance+rfh;
  2903. end
  2904. else
  2905. { related object parameters also need to determine the distance between the current
  2906. object and the object we are comparing with. var and out parameters must match exactly }
  2907. if not(currpara.varspez in [vs_var,vs_out]) and
  2908. (def_from.typ=objectdef) and
  2909. (def_to.typ=objectdef) and
  2910. (tobjectdef(def_from).objecttype=tobjectdef(def_to).objecttype) and
  2911. def_is_related(tobjectdef(def_from),tobjectdef(def_to)) then
  2912. begin
  2913. eq:=te_convert_l1;
  2914. check_valid_var:=false;
  2915. { resolve anonymous external class definitions }
  2916. obj_from:=find_real_class_definition(tobjectdef(def_from),false);
  2917. obj_to:=find_real_class_definition(tobjectdef(def_to),false);
  2918. while assigned(obj_from) do
  2919. begin
  2920. if obj_from=obj_to then
  2921. break;
  2922. hp^.ordinal_distance:=hp^.ordinal_distance+1;
  2923. obj_from:=obj_from.childof;
  2924. end;
  2925. end
  2926. { compare_defs_ext compares sets and array constructors very poorly because
  2927. it has too little information. So we do explicitly a detailed comparisation,
  2928. see also bug #11288 (FK)
  2929. }
  2930. else if (def_to.typ=setdef) and is_array_constructor(currpt.left.resultdef) then
  2931. begin
  2932. n:=currpt.left.getcopy;
  2933. arrayconstructor_to_set(n);
  2934. eq:=compare_defs_ext(n.resultdef,def_to,n.nodetype,convtype,pdoper,cdoptions);
  2935. check_valid_var:=false;
  2936. n.free;
  2937. end
  2938. else if is_open_array(def_to) and
  2939. is_class_or_interface_or_dispinterface_or_objc_or_java(tarraydef(def_to).elementdef) and
  2940. is_array_constructor(currpt.left.resultdef) and
  2941. assigned(tarrayconstructornode(currpt.left).left) then
  2942. begin
  2943. { ensure that [nil] can be converted to "array of tobject",
  2944. because if we just try to convert "array of pointer" to
  2945. "array of tobject", we get type conversion errors in
  2946. non-Delphi modes }
  2947. n:=currpt.left;
  2948. mineq:=te_exact;
  2949. repeat
  2950. if tarrayconstructornode(n).left.nodetype=arrayconstructorrangen then
  2951. eq:=te_incompatible
  2952. else
  2953. eq:=compare_defs_ext(tarrayconstructornode(n).left.resultdef,tarraydef(def_to).elementdef,tarrayconstructornode(n).left.nodetype,convtype,pdoper,cdoptions);
  2954. if eq<mineq then
  2955. mineq:=eq;
  2956. if eq=te_incompatible then
  2957. break;
  2958. n:=tarrayconstructornode(n).right;
  2959. until not assigned(n);
  2960. eq:=mineq;
  2961. check_valid_var:=false;
  2962. end
  2963. else
  2964. { generic type comparision }
  2965. begin
  2966. if (hp^.data.procoptions*[po_rtlproc,po_compilerproc]=[]) and
  2967. is_ansistring(def_from) and
  2968. is_ansistring(def_to) and
  2969. (tstringdef(def_from).encoding<>tstringdef(def_to).encoding) and
  2970. (currpara.varspez in [vs_var,vs_out]) then
  2971. begin
  2972. eq:=te_convert_l1; // don't allow to pass different ansistring types to each-other
  2973. check_valid_var:=false;
  2974. end
  2975. else
  2976. eq:=compare_defs_ext(def_from,def_to,currpt.left.nodetype,convtype,pdoper,cdoptions);
  2977. { when the types are not equal we need to check
  2978. some special case for parameter passing }
  2979. if (eq<te_equal) then
  2980. begin
  2981. if currpara.varspez in [vs_var,vs_out] then
  2982. begin
  2983. { para requires an equal type so the previous found
  2984. match was not good enough, reset to incompatible }
  2985. eq:=te_incompatible;
  2986. var_para_allowed(eq,currpt.resultdef,currpara.vardef,currpt.left);
  2987. check_valid_var:=false;
  2988. end
  2989. else
  2990. para_allowed(eq,currpt,def_to);
  2991. end;
  2992. end;
  2993. { univ parameters match if the size matches (don't override the
  2994. comparison result if it was ok, since a match based on the
  2995. "univ" character is the lowest possible match) }
  2996. if (eq=te_incompatible) and
  2997. currpara.univpara and
  2998. is_valid_univ_para_type(def_from) and
  2999. (def_from.size=def_to.size) then
  3000. eq:=te_convert_l5;
  3001. { when a procvar was changed to a call an exact match is
  3002. downgraded to equal. This way an overload call with the
  3003. procvar is choosen. See tb0471 (PFV) }
  3004. if (pt<>currpt) and (eq=te_exact) then
  3005. eq:=te_equal;
  3006. { if var or out parameter type but paranode not is_valid_for_var }
  3007. if check_valid_var and (currpara.varspez in [vs_var,vs_out]) and not valid_for_var(currpt.left,false)
  3008. and (def_to.typ<>formaldef) and not is_open_array(def_to) then
  3009. eq:=te_incompatible;
  3010. { increase correct counter }
  3011. case eq of
  3012. te_exact :
  3013. inc(hp^.exact_count);
  3014. te_equal :
  3015. inc(hp^.equal_count);
  3016. te_convert_l1 :
  3017. inc(hp^.cl1_count);
  3018. te_convert_l2 :
  3019. inc(hp^.cl2_count);
  3020. te_convert_l3 :
  3021. inc(hp^.cl3_count);
  3022. te_convert_l4 :
  3023. inc(hp^.cl4_count);
  3024. te_convert_l5 :
  3025. inc(hp^.cl5_count);
  3026. te_convert_l6 :
  3027. inc(hp^.cl6_count);
  3028. te_convert_operator :
  3029. inc(hp^.coper_count);
  3030. te_incompatible :
  3031. hp^.invalid:=true;
  3032. end;
  3033. { stop checking when an incompatible parameter is found }
  3034. if hp^.invalid then
  3035. begin
  3036. { store the current parameter info for
  3037. a nice error message when no procedure is found }
  3038. hp^.wrongparaidx:=paraidx;
  3039. hp^.wrongparanr:=currparanr;
  3040. break;
  3041. end;
  3042. {$ifdef EXTDEBUG}
  3043. { store equal in node tree for dump }
  3044. currpara.eqval:=eq;
  3045. {$endif EXTDEBUG}
  3046. { maybe release temp currpt }
  3047. if releasecurrpt then
  3048. currpt.free;
  3049. { next parameter in the call tree }
  3050. pt:=tcallparanode(pt.right);
  3051. { next parameter for definition, only goto next para
  3052. if we're out of the varargs }
  3053. if not(po_varargs in hp^.data.procoptions) or
  3054. (currparanr<=hp^.data.maxparacount) then
  3055. begin
  3056. { Ignore vs_hidden parameters }
  3057. repeat
  3058. dec(paraidx);
  3059. until (paraidx<0) or not(vo_is_hidden_para in tparavarsym(hp^.data.paras[paraidx]).varoptions);
  3060. end;
  3061. dec(currparanr);
  3062. end;
  3063. if not(hp^.invalid) and
  3064. (assigned(pt) or (paraidx>=0) or (currparanr<>0)) then
  3065. internalerror(200212141);
  3066. { next candidate }
  3067. hp:=hp^.next;
  3068. end;
  3069. end;
  3070. function get_variantequaltype(def: tdef): tvariantequaltype;
  3071. const
  3072. variantorddef_cl: array[tordtype] of tvariantequaltype =
  3073. (tve_incompatible,tve_byte,tve_word,tve_cardinal,tve_chari64,tve_incompatible,
  3074. tve_shortint,tve_smallint,tve_longint,tve_chari64,tve_incompatible,
  3075. tve_boolformal,tve_boolformal,tve_boolformal,tve_boolformal,tve_boolformal,
  3076. tve_boolformal,tve_boolformal,tve_boolformal,tve_boolformal,
  3077. tve_chari64,tve_chari64,tve_dblcurrency,tve_incompatible);
  3078. { TODO: fixme for 128 bit floats }
  3079. variantfloatdef_cl: array[tfloattype] of tvariantequaltype =
  3080. (tve_single,tve_dblcurrency,tve_extended,tve_extended,
  3081. tve_dblcurrency,tve_dblcurrency,tve_extended);
  3082. variantstringdef_cl: array[tstringtype] of tvariantequaltype =
  3083. (tve_sstring,tve_astring,tve_astring,tve_wstring,tve_ustring);
  3084. begin
  3085. case def.typ of
  3086. orddef:
  3087. begin
  3088. result:=variantorddef_cl[torddef(def).ordtype];
  3089. end;
  3090. floatdef:
  3091. begin
  3092. result:=variantfloatdef_cl[tfloatdef(def).floattype];
  3093. end;
  3094. stringdef:
  3095. begin
  3096. result:=variantstringdef_cl[tstringdef(def).stringtype];
  3097. end;
  3098. formaldef:
  3099. begin
  3100. result:=tve_boolformal;
  3101. end;
  3102. else
  3103. begin
  3104. result:=tve_incompatible;
  3105. end;
  3106. end
  3107. end;
  3108. function is_better_candidate(currpd,bestpd:pcandidate):integer;
  3109. var
  3110. res : integer;
  3111. begin
  3112. {
  3113. Return values:
  3114. > 0 when currpd is better than bestpd
  3115. < 0 when bestpd is better than currpd
  3116. = 0 when both are equal
  3117. To choose the best candidate we use the following order:
  3118. - Incompatible flag
  3119. - (Smaller) Number of convert operator parameters.
  3120. - (Smaller) Number of convertlevel 2 parameters.
  3121. - (Smaller) Number of convertlevel 1 parameters.
  3122. - (Bigger) Number of exact parameters.
  3123. - (Smaller) Number of equal parameters.
  3124. - (Smaller) Total of ordinal distance. For example, the distance of a word
  3125. to a byte is 65535-255=65280.
  3126. }
  3127. if bestpd^.invalid then
  3128. begin
  3129. if currpd^.invalid then
  3130. res:=0
  3131. else
  3132. res:=1;
  3133. end
  3134. else
  3135. if currpd^.invalid then
  3136. res:=-1
  3137. else
  3138. begin
  3139. { less operator parameters? }
  3140. res:=(bestpd^.coper_count-currpd^.coper_count);
  3141. if (res=0) then
  3142. begin
  3143. { less cl6 parameters? }
  3144. res:=(bestpd^.cl6_count-currpd^.cl6_count);
  3145. if (res=0) then
  3146. begin
  3147. { less cl5 parameters? }
  3148. res:=(bestpd^.cl5_count-currpd^.cl5_count);
  3149. if (res=0) then
  3150. begin
  3151. { less cl4 parameters? }
  3152. res:=(bestpd^.cl4_count-currpd^.cl4_count);
  3153. if (res=0) then
  3154. begin
  3155. { less cl3 parameters? }
  3156. res:=(bestpd^.cl3_count-currpd^.cl3_count);
  3157. if (res=0) then
  3158. begin
  3159. { less cl2 parameters? }
  3160. res:=(bestpd^.cl2_count-currpd^.cl2_count);
  3161. if (res=0) then
  3162. begin
  3163. { less cl1 parameters? }
  3164. res:=(bestpd^.cl1_count-currpd^.cl1_count);
  3165. if (res=0) then
  3166. begin
  3167. { more exact parameters? }
  3168. res:=(currpd^.exact_count-bestpd^.exact_count);
  3169. if (res=0) then
  3170. begin
  3171. { less equal parameters? }
  3172. res:=(bestpd^.equal_count-currpd^.equal_count);
  3173. if (res=0) then
  3174. begin
  3175. { smaller ordinal distance? }
  3176. if (currpd^.ordinal_distance<bestpd^.ordinal_distance) then
  3177. res:=1
  3178. else
  3179. if (currpd^.ordinal_distance>bestpd^.ordinal_distance) then
  3180. res:=-1
  3181. else
  3182. res:=0;
  3183. { if a specialization is better than a non-specialization then
  3184. the non-generic always wins }
  3185. if m_implicit_function_specialization in current_settings.modeswitches then
  3186. begin
  3187. if (currpd^.data.is_specialization and not bestpd^.data.is_specialization) then
  3188. res:=-1
  3189. else if (not currpd^.data.is_specialization and bestpd^.data.is_specialization) then
  3190. res:=1;
  3191. end;
  3192. end;
  3193. end;
  3194. end;
  3195. end;
  3196. end;
  3197. end;
  3198. end;
  3199. end;
  3200. end;
  3201. end;
  3202. is_better_candidate:=res;
  3203. end;
  3204. { Delphi precedence rules extracted from test programs. Only valid if passing
  3205. a variant parameter to overloaded procedures expecting exactly one parameter.
  3206. single > (char, currency, int64, shortstring, ansistring, widestring, unicodestring, extended, double)
  3207. double/currency > (char, int64, shortstring, ansistring, widestring, unicodestring, extended)
  3208. extended > (char, int64, shortstring, ansistring, widestring, unicodestring)
  3209. longint/cardinal > (int64, shortstring, ansistring, widestring, unicodestring, extended, double, single, char, currency)
  3210. smallint > (longint, int64, shortstring, ansistring, widestring, unicodestring, extended, double single, char, currency);
  3211. word > (longint, cardinal, int64, shortstring, ansistring, widestring, unicodestring, extended, double single, char, currency);
  3212. shortint > (longint, smallint, int64, shortstring, ansistring, widestring, unicodestring, extended, double, single, char, currency)
  3213. byte > (longint, cardinal, word, smallint, int64, shortstring, ansistring, widestring, unicodestring, extended, double, single, char, currency);
  3214. boolean/formal > (char, int64, shortstring, ansistring, widestring, unicodestring)
  3215. widestring > (char, int64, shortstring, ansistring, unicodestring)
  3216. unicodestring > (char, int64, shortstring, ansistring)
  3217. ansistring > (char, int64, shortstring)
  3218. shortstring > (char, int64)
  3219. Relations not mentioned mean that they conflict: no decision possible }
  3220. function is_better_candidate_single_variant(currpd,bestpd:pcandidate):integer;
  3221. function calculate_relation(const currvcl, bestvcl, testvcl:
  3222. tvariantequaltype; const conflictvcls: tvariantequaltypes):integer;
  3223. begin
  3224. { if (bestvcl=conflictvcl) or
  3225. (currvcl=conflictvcl) then
  3226. result:=0
  3227. else if (bestvcl=testvcl) then
  3228. result:=-1
  3229. else result:=1 }
  3230. result:=1-2*ord(bestvcl=testvcl)+
  3231. ord(currvcl in conflictvcls)-ord(bestvcl in conflictvcls);
  3232. end;
  3233. function getfirstrealparaidx(pd: pcandidate): integer;
  3234. begin
  3235. { can be different for currpd and bestpd in case of overloaded }
  3236. { functions, e.g. lowercase():char and lowercase():shortstring }
  3237. { (depending on the calling convention and parameter order) }
  3238. result:=pd^.firstparaidx;
  3239. while (result>=0) and (vo_is_hidden_para in tparavarsym(pd^.data.paras[result]).varoptions) do
  3240. dec(result);
  3241. if (vo_is_hidden_para in tparavarsym(pd^.data.paras[result]).varoptions) then
  3242. internalerror(2006122803);
  3243. end;
  3244. var
  3245. currpara, bestpara: tparavarsym;
  3246. currvcl, bestvcl: tvariantequaltype;
  3247. begin
  3248. {
  3249. Return values:
  3250. > 0 when currpd is better than bestpd
  3251. < 0 when bestpd is better than currpd
  3252. = 0 when both are equal
  3253. }
  3254. currpara:=tparavarsym(currpd^.data.paras[getfirstrealparaidx(currpd)]);
  3255. bestpara:=tparavarsym(bestpd^.data.paras[getfirstrealparaidx(bestpd)]);
  3256. { if one of the parameters is a regular variant, fall back to the }
  3257. { default algorithm }
  3258. if (currpara.vardef.typ = variantdef) or
  3259. (bestpara.vardef.typ = variantdef) then
  3260. begin
  3261. result:=is_better_candidate(currpd,bestpd);
  3262. exit;
  3263. end;
  3264. currvcl:=get_variantequaltype(currpara.vardef);
  3265. bestvcl:=get_variantequaltype(bestpara.vardef);
  3266. { sanity check }
  3267. result:=-5;
  3268. { if both are the same, there is a conflict }
  3269. if (currvcl=bestvcl) then
  3270. result:=0
  3271. { if one of the two cannot be used as variant, the other is better }
  3272. else if (bestvcl=tve_incompatible) then
  3273. result:=1
  3274. else if (currvcl=tve_incompatible) then
  3275. result:=-1
  3276. { boolean and formal are better than chari64str, but conflict with }
  3277. { everything else }
  3278. else if (currvcl=tve_boolformal) or
  3279. (bestvcl=tve_boolformal) then
  3280. if (currvcl=tve_boolformal) then
  3281. result:=ord(bestvcl in [tve_chari64,tve_sstring,tve_astring,tve_wstring,tve_ustring])
  3282. else
  3283. result:=-ord(currvcl in [tve_chari64,tve_sstring,tve_astring,tve_wstring,tve_ustring])
  3284. { byte is better than everything else (we assume both aren't byte, }
  3285. { since there's only one parameter and that one can't be the same) }
  3286. else if (currvcl=tve_byte) or
  3287. (bestvcl=tve_byte) then
  3288. result:=calculate_relation(currvcl,bestvcl,tve_byte,[tve_shortint])
  3289. { shortint conflicts with word and cardinal, but is better than }
  3290. { everything else but byte (which has already been handled) }
  3291. else if (currvcl=tve_shortint) or
  3292. (bestvcl=tve_shortint) then
  3293. result:=calculate_relation(currvcl,bestvcl,tve_shortint,[tve_word, tve_cardinal])
  3294. { word conflicts with smallint, but is better than everything else }
  3295. { but shortint and byte (which has already been handled) }
  3296. else if (currvcl=tve_word) or
  3297. (bestvcl=tve_word) then
  3298. result:=calculate_relation(currvcl,bestvcl,tve_word,[tve_smallint])
  3299. { smallint conflicts with cardinal, but is better than everything }
  3300. { which has not yet been tested }
  3301. else if (currvcl=tve_smallint) or
  3302. (bestvcl=tve_smallint) then
  3303. result:=calculate_relation(currvcl,bestvcl,tve_smallint,[tve_cardinal])
  3304. { cardinal conflicts with each longint and is better than everything }
  3305. { which has not yet been tested }
  3306. else if (currvcl=tve_cardinal) or
  3307. (bestvcl=tve_cardinal) then
  3308. result:=calculate_relation(currvcl,bestvcl,tve_cardinal,[tve_longint])
  3309. { longint is better than everything which has not yet been tested }
  3310. else if (currvcl=tve_longint) or
  3311. (bestvcl=tve_longint) then
  3312. { if bestvcl=tve_longint then
  3313. result:=-1
  3314. else
  3315. result:=1 }
  3316. result:=1-2*ord(bestvcl=tve_longint)
  3317. { single is better than everything left }
  3318. else if (currvcl=tve_single) or
  3319. (bestvcl=tve_single) then
  3320. result:=1-2*ord(bestvcl=tve_single)
  3321. { double/comp/currency are better than everything left, and conflict }
  3322. { with each other (but that's already tested) }
  3323. else if (currvcl=tve_dblcurrency) or
  3324. (bestvcl=tve_dblcurrency) then
  3325. result:=1-2*ord(bestvcl=tve_dblcurrency)
  3326. { extended is better than everything left }
  3327. else if (currvcl=tve_extended) or
  3328. (bestvcl=tve_extended) then
  3329. result:=1-2*ord(bestvcl=tve_extended)
  3330. { widestring is better than everything left }
  3331. else if (currvcl=tve_wstring) or
  3332. (bestvcl=tve_wstring) then
  3333. result:=1-2*ord(bestvcl=tve_wstring)
  3334. { unicodestring is better than everything left }
  3335. else if (currvcl=tve_ustring) or
  3336. (bestvcl=tve_ustring) then
  3337. result:=1-2*ord(bestvcl=tve_ustring)
  3338. { ansistring is better than everything left }
  3339. else if (currvcl=tve_astring) or
  3340. (bestvcl=tve_astring) then
  3341. result:=1-2*ord(bestvcl=tve_astring)
  3342. { shortstring is better than everything left }
  3343. else if (currvcl=tve_sstring) or
  3344. (bestvcl=tve_sstring) then
  3345. result:=1-2*ord(bestvcl=tve_sstring);
  3346. { all possibilities should have been checked now }
  3347. if (result=-5) then
  3348. internalerror(2006122805);
  3349. end;
  3350. {$ifdef DISABLE_FAST_OVERLOAD_PATCH}
  3351. function tcallcandidates.choose_best(var bestpd:tabstractprocdef; singlevariant: boolean):integer;
  3352. var
  3353. pd: tprocdef;
  3354. besthpstart,
  3355. hp : pcandidate;
  3356. cntpd,
  3357. res : integer;
  3358. begin
  3359. {
  3360. Returns the number of candidates left and the
  3361. first candidate is returned in pdbest
  3362. }
  3363. { Setup the first procdef as best, only count it as a result
  3364. when it is valid }
  3365. bestpd:=FCandidateProcs^.data;
  3366. if FCandidateProcs^.invalid then
  3367. cntpd:=0
  3368. else
  3369. cntpd:=1;
  3370. if assigned(FCandidateProcs^.next) then
  3371. begin
  3372. besthpstart:=FCandidateProcs;
  3373. hp:=FCandidateProcs^.next;
  3374. while assigned(hp) do
  3375. begin
  3376. if not singlevariant then
  3377. res:=is_better_candidate(hp,besthpstart)
  3378. else
  3379. res:=is_better_candidate_single_variant(hp,besthpstart);
  3380. if (res>0) then
  3381. begin
  3382. { hp is better, flag all procs to be incompatible }
  3383. while (besthpstart<>hp) do
  3384. begin
  3385. besthpstart^.invalid:=true;
  3386. besthpstart:=besthpstart^.next;
  3387. end;
  3388. { besthpstart is already set to hp }
  3389. bestpd:=besthpstart^.data;
  3390. cntpd:=1;
  3391. end
  3392. else
  3393. if (res<0) then
  3394. begin
  3395. { besthpstart is better, flag current hp to be incompatible }
  3396. hp^.invalid:=true;
  3397. end
  3398. else
  3399. begin
  3400. { res=0, both are valid }
  3401. if not hp^.invalid then
  3402. inc(cntpd);
  3403. end;
  3404. hp:=hp^.next;
  3405. end;
  3406. end;
  3407. { if we've found one, check the procdefs ignored for overload choosing
  3408. to see whether they contain one from a child class with the same
  3409. parameters (so the overload choosing was not influenced by their
  3410. presence, but now that we've decided which overloaded version to call,
  3411. make sure we call the version closest in terms of visibility }
  3412. if (cntpd=1) and assigned(FIgnoredCandidateProcs) then
  3413. begin
  3414. for res:=0 to FIgnoredCandidateProcs.count-1 do
  3415. begin
  3416. pd:=tprocdef(FIgnoredCandidateProcs[res]);
  3417. { stop searching when we start comparing methods of parent of
  3418. the struct in which the current best method was found }
  3419. if assigned(pd.struct) and
  3420. (pd.struct<>tprocdef(bestpd).struct) and
  3421. def_is_related(tprocdef(bestpd).struct,pd.struct) then
  3422. break;
  3423. if (pd.proctypeoption=bestpd.proctypeoption) and
  3424. ((pd.procoptions*[po_classmethod,po_methodpointer])=(bestpd.procoptions*[po_classmethod,po_methodpointer])) and
  3425. (compare_paras(pd.paras,bestpd.paras,cp_all,[cpo_ignorehidden,cpo_ignoreuniv,cpo_openequalisexact])=te_exact) then
  3426. begin
  3427. { first one encountered is closest in terms of visibility }
  3428. bestpd:=pd;
  3429. break;
  3430. end;
  3431. end;
  3432. end;
  3433. result:=cntpd;
  3434. end;
  3435. {$else}
  3436. function compare_by_old_sortout_check(pd,bestpd:pcandidate):integer;
  3437. var cpoptions : tcompare_paras_options;
  3438. begin
  3439. { don't add duplicates, only compare visible parameters for the user }
  3440. cpoptions:=[cpo_ignorehidden];
  3441. if (po_compilerproc in bestpd^.data.procoptions) then
  3442. cpoptions:=cpoptions+[cpo_compilerproc];
  3443. if (po_rtlproc in bestpd^.data.procoptions) then
  3444. cpoptions:=cpoptions+[cpo_rtlproc];
  3445. compare_by_old_sortout_check := 0; // can't decide, bestpd probably wasn't sorted out in unpatched
  3446. if (compare_paras(pd^.data.paras,bestpd^.data.paras,cp_value_equal_const,cpoptions)>=te_equal) and
  3447. (not(po_objc in bestpd^.data.procoptions) or (bestpd^.data.messageinf.str^=pd^.data.messageinf.str^)) then
  3448. compare_by_old_sortout_check := 1; // bestpd was sorted out before patch
  3449. { for implicit specializations non-generics should take precedence so
  3450. when comparing a specialization to a non-specialization mark as undecided
  3451. and it will be re-evaluated in is_better_candidate }
  3452. if (m_implicit_function_specialization in current_settings.modeswitches)
  3453. and (pd^.data.is_specialization <> bestpd^.data.is_specialization) then
  3454. compare_by_old_sortout_check:=0;
  3455. end;
  3456. function decide_restart(pd,bestpd:pcandidate) : boolean;
  3457. begin
  3458. decide_restart := false;
  3459. if assigned(bestpd) then
  3460. begin
  3461. { don't restart if bestpd is marked invalid already }
  3462. if not bestpd^.invalid then
  3463. decide_restart := compare_by_old_sortout_check(pd,bestpd)<>0;
  3464. end;
  3465. end;
  3466. procedure save_validity(c : pcandidate);
  3467. begin
  3468. while assigned(c) do
  3469. begin
  3470. c^.saved_validity := c^.invalid;
  3471. c := c^.next;
  3472. end;
  3473. end;
  3474. procedure restore_validity(c : pcandidate);
  3475. begin
  3476. while assigned(c) do begin
  3477. c^.invalid := c^.saved_validity;
  3478. c := c^.next;
  3479. end;
  3480. end;
  3481. function tcallcandidates.choose_best(var bestpd:tabstractprocdef; singlevariant: boolean):integer;
  3482. var
  3483. pd: tprocdef;
  3484. besthpstart,
  3485. hp,hp2 : pcandidate;
  3486. cntpd,
  3487. res : integer;
  3488. restart : boolean;
  3489. begin
  3490. res:=0;
  3491. {
  3492. Returns the number of candidates left and the
  3493. first candidate is returned in pdbest
  3494. }
  3495. if not(assigned(FCandidateProcs)) then
  3496. begin
  3497. choose_best := 0;
  3498. exit;
  3499. end;
  3500. bestpd:=FCandidateProcs^.data;
  3501. if FCandidateProcs^.invalid then
  3502. cntpd:=0
  3503. else
  3504. cntpd:=1;
  3505. if assigned(FCandidateProcs^.next) then
  3506. begin
  3507. save_validity(FCandidateProcs);
  3508. restart := false;
  3509. { keep restarting, until there wasn't a sorted-out besthpstart }
  3510. repeat
  3511. besthpstart:=FCandidateProcs;
  3512. bestpd:=FCandidateProcs^.data;
  3513. if restart then
  3514. begin
  3515. restore_validity(FCandidateProcs);
  3516. restart := false;
  3517. end;
  3518. { Setup the first procdef as best, only count it as a result
  3519. when it is valid }
  3520. if besthpstart^.invalid then
  3521. cntpd:=0
  3522. else
  3523. cntpd:=1;
  3524. hp:=FCandidateProcs^.next;
  3525. while assigned(hp) and not(restart) do
  3526. begin
  3527. restart := decide_restart(hp,besthpstart);
  3528. if not restart then
  3529. begin
  3530. if besthpstart^.invalid then res := 1
  3531. else if hp^.invalid then res := -1
  3532. else if not singlevariant then
  3533. res:=is_better_candidate(hp,besthpstart)
  3534. else
  3535. res:=is_better_candidate_single_variant(hp,besthpstart);
  3536. end;
  3537. if restart then
  3538. begin
  3539. { mark the sorted out invalid globally }
  3540. besthpstart^.saved_validity := true;
  3541. end
  3542. else if (res>0) then
  3543. begin
  3544. { hp is better, flag all procs to be incompatible }
  3545. while (besthpstart<>hp) do
  3546. begin
  3547. besthpstart^.invalid:=true;
  3548. besthpstart:=besthpstart^.next;
  3549. end;
  3550. { besthpstart is already set to hp }
  3551. bestpd:=besthpstart^.data;
  3552. if besthpstart^.invalid then
  3553. cntpd:=0
  3554. else
  3555. cntpd:=1;
  3556. end
  3557. else if (res<0) then
  3558. begin
  3559. { besthpstart is better, flag current hp to be incompatible }
  3560. hp^.invalid:=true;
  3561. end
  3562. else
  3563. begin
  3564. { res=0, both are valid }
  3565. if not hp^.invalid then
  3566. inc(cntpd);
  3567. end;
  3568. hp:=hp^.next;
  3569. end;
  3570. until not(restart);
  3571. end;
  3572. { check the alternate choices if they would have been sorted out before patch... }
  3573. { note we have procadded the candidates, so order is reversed procadd order here.
  3574. this was also used above: each sorted-out always has an "outsorter" counterpart
  3575. deeper down the next chain
  3576. }
  3577. { for the intial implementation, let's first do some more consistency checking}
  3578. res := 0;
  3579. hp := FCandidateProcs;
  3580. while assigned(hp) do
  3581. begin
  3582. if not(hp^.invalid) then
  3583. inc(res);
  3584. hp := hp^.next;
  3585. end;
  3586. if (res<>cntpd) then
  3587. internalerror(202002161);
  3588. { check all valid choices for sortout }
  3589. cntpd := 0;
  3590. hp := FCandidateProcs;
  3591. while assigned(hp) do
  3592. begin
  3593. if not(hp^.invalid) then
  3594. begin
  3595. hp2 := hp^.next;
  3596. while assigned(hp2) do begin
  3597. if compare_by_old_sortout_check(hp2,hp)<>0 then
  3598. begin
  3599. hp^.invalid := true;
  3600. hp2 := nil;
  3601. end
  3602. else
  3603. hp2:=hp2^.next;
  3604. end;
  3605. if not(hp^.invalid) then
  3606. begin
  3607. inc(cntpd);
  3608. { check for the impossible event bestpd had become invalid}
  3609. if (cntpd=1) and (hp^.data<>bestpd) then
  3610. internalerror(202002162);
  3611. end;
  3612. end;
  3613. hp := hp^.next;
  3614. end;
  3615. { if we've found one, check the procdefs ignored for overload choosing
  3616. to see whether they contain one from a child class with the same
  3617. parameters (so the overload choosing was not influenced by their
  3618. presence, but now that we've decided which overloaded version to call,
  3619. make sure we call the version closest in terms of visibility }
  3620. if (cntpd=1) and assigned(FIgnoredCandidateProcs) then
  3621. begin
  3622. for res:=0 to FIgnoredCandidateProcs.count-1 do
  3623. begin
  3624. pd:=tprocdef(FIgnoredCandidateProcs[res]);
  3625. { stop searching when we start comparing methods of parent of
  3626. the struct in which the current best method was found }
  3627. if assigned(pd.struct) and
  3628. (pd.struct<>tprocdef(bestpd).struct) and
  3629. def_is_related(tprocdef(bestpd).struct,pd.struct) then
  3630. break;
  3631. if (pd.proctypeoption=bestpd.proctypeoption) and
  3632. ((pd.procoptions*[po_classmethod,po_methodpointer])=(bestpd.procoptions*[po_classmethod,po_methodpointer])) and
  3633. (compare_paras(pd.paras,bestpd.paras,cp_all,[cpo_ignorehidden,cpo_ignoreuniv,cpo_openequalisexact])=te_exact) then
  3634. begin
  3635. { first one encountered is closest in terms of visibility }
  3636. bestpd:=pd;
  3637. break;
  3638. end;
  3639. end;
  3640. end;
  3641. result:=cntpd;
  3642. end;
  3643. {$endif}
  3644. procedure tcallcandidates.find_wrong_para;
  3645. var
  3646. currparanr : smallint;
  3647. hp : pcandidate;
  3648. pt : tcallparanode;
  3649. wrongpara : tparavarsym;
  3650. begin
  3651. { Only process the first overloaded procdef }
  3652. hp:=FCandidateProcs;
  3653. { Find callparanode corresponding to the argument }
  3654. pt:=tcallparanode(FParanode);
  3655. currparanr:=FParalength;
  3656. while assigned(pt) and
  3657. (currparanr>hp^.wrongparanr) do
  3658. begin
  3659. pt:=tcallparanode(pt.right);
  3660. dec(currparanr);
  3661. end;
  3662. if (currparanr<>hp^.wrongparanr) or
  3663. not assigned(pt) then
  3664. internalerror(200212094);
  3665. { Show error message, when it was a var or out parameter
  3666. guess that it is a missing typeconv }
  3667. wrongpara:=tparavarsym(hp^.data.paras[hp^.wrongparaidx]);
  3668. if wrongpara.varspez in [vs_var,vs_out] then
  3669. begin
  3670. { Maybe passing the correct type but passing a const to var parameter }
  3671. if (compare_defs(pt.resultdef,wrongpara.vardef,pt.nodetype)<>te_incompatible) and
  3672. not valid_for_var(pt.left,true) then
  3673. CGMessagePos(pt.left.fileinfo,type_e_variable_id_expected)
  3674. else
  3675. CGMessagePos3(pt.left.fileinfo,parser_e_call_by_ref_without_typeconv,tostr(hp^.wrongparanr),
  3676. FullTypeName(pt.left.resultdef,wrongpara.vardef),
  3677. FullTypeName(wrongpara.vardef,pt.left.resultdef))
  3678. end
  3679. else
  3680. CGMessagePos3(pt.left.fileinfo,type_e_wrong_parameter_type,tostr(hp^.wrongparanr),
  3681. FullTypeName(pt.left.resultdef,wrongpara.vardef),
  3682. FullTypeName(wrongpara.vardef,pt.left.resultdef));
  3683. end;
  3684. procedure check_ranges(const location: tfileposinfo; source: tnode; destdef: tdef);
  3685. begin
  3686. if not(cs_check_ordinal_size in current_settings.localswitches) then
  3687. exit;
  3688. { check if the assignment may cause a range check error }
  3689. { if its not explicit, and only if the values are }
  3690. { ordinals, enumdef and floatdef }
  3691. if assigned(destdef) and
  3692. (destdef.typ in [enumdef,orddef,floatdef]) and
  3693. not is_boolean(destdef) and
  3694. assigned(source.resultdef) and
  3695. (source.resultdef.typ in [enumdef,orddef,floatdef]) and
  3696. not is_boolean(source.resultdef) and
  3697. not is_constrealnode(source) and
  3698. { constants are handled via regular range checking }
  3699. (source.nodetype<>ordconstn) then
  3700. begin
  3701. if ((destdef.size < source.resultdef.size) and
  3702. { s80real and sc80real have a different size but the same precision }
  3703. not((destdef.typ=floatdef) and
  3704. (source.resultdef.typ=floatdef) and
  3705. (tfloatdef(source.resultdef).floattype in [s80real,sc80real]) and
  3706. (tfloatdef(destdef).floattype in [s80real,sc80real]))) or
  3707. ((destdef.typ<>floatdef) and
  3708. (source.resultdef.typ<>floatdef) and
  3709. not is_in_limit(source.resultdef,destdef)) then
  3710. begin
  3711. if (cs_check_range in current_settings.localswitches) then
  3712. MessagePos(location,type_w_smaller_possible_range_check)
  3713. else
  3714. MessagePos(location,type_h_smaller_possible_range_check);
  3715. end;
  3716. end;
  3717. end;
  3718. function is_valid_for_default(def:tdef):boolean;
  3719. function is_valid_record_or_object(def:tabstractrecorddef):boolean;
  3720. var
  3721. sym : tsym;
  3722. i : longint;
  3723. begin
  3724. for i:=0 to def.symtable.symlist.count-1 do
  3725. begin
  3726. sym:=tsym(def.symtable.symlist[i]);
  3727. if not is_normal_fieldvarsym(sym) then
  3728. continue;
  3729. if not is_valid_for_default(tfieldvarsym(sym).vardef) then
  3730. begin
  3731. result:=false;
  3732. exit;
  3733. end;
  3734. end;
  3735. result:=true;
  3736. end;
  3737. begin
  3738. case def.typ of
  3739. recorddef:
  3740. result:=is_valid_record_or_object(tabstractrecorddef(def));
  3741. objectdef:
  3742. if is_implicit_pointer_object_type(def) then
  3743. result:=true
  3744. else
  3745. if is_object(def) then
  3746. result:=is_valid_record_or_object(tabstractrecorddef(def))
  3747. else
  3748. result:=false;
  3749. arraydef:
  3750. if not (ado_isdynamicarray in tarraydef(def).arrayoptions) then
  3751. result:=is_valid_for_default(tarraydef(def).elementdef)
  3752. else
  3753. result:=true;
  3754. formaldef,
  3755. abstractdef,
  3756. filedef:
  3757. result:=false;
  3758. else
  3759. result:=true;
  3760. end;
  3761. end;
  3762. end.