htypechk.pas 162 KB

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