htypechk.pas 161 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010
  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) then
  1094. CGMessage(type_e_cannot_local_proc_to_procvar);
  1095. end;
  1096. procedure UninitializedVariableMessage(pos : tfileposinfo;warning,local,managed : boolean;name : TMsgStr);
  1097. const
  1098. msg : array[false..true,false..true,false..true] of dword = (
  1099. (
  1100. (sym_h_uninitialized_variable,sym_h_uninitialized_managed_variable),
  1101. (sym_h_uninitialized_local_variable,sym_h_uninitialized_managed_local_variable)
  1102. ),
  1103. (
  1104. (sym_w_uninitialized_variable,sym_w_uninitialized_managed_variable),
  1105. (sym_w_uninitialized_local_variable,sym_w_uninitialized_managed_local_variable)
  1106. )
  1107. );
  1108. begin
  1109. CGMessagePos1(pos,msg[warning,local,managed],name);
  1110. end;
  1111. procedure set_varstate(p:tnode;newstate:tvarstate;varstateflags:tvarstateflags);
  1112. const
  1113. vstrans: array[tvarstate,tvarstate] of tvarstate = (
  1114. { vs_none -> ... }
  1115. (vs_none,vs_declared,vs_initialised,vs_read,vs_read_not_warned,vs_referred_not_inited,vs_written,vs_readwritten),
  1116. { vs_declared -> ... }
  1117. (vs_none,vs_declared,vs_initialised,vs_read,vs_read_not_warned,vs_referred_not_inited,vs_written,vs_readwritten),
  1118. { vs_initialised -> ... }
  1119. (vs_none,vs_initialised,vs_initialised,vs_read,vs_read,vs_read,vs_written,vs_readwritten),
  1120. { vs_read -> ... }
  1121. (vs_none,vs_read,vs_read,vs_read,vs_read,vs_read,vs_readwritten,vs_readwritten),
  1122. { vs_read_not_warned -> ... }
  1123. (vs_none,vs_read_not_warned,vs_read,vs_read,vs_read_not_warned,vs_read_not_warned,vs_readwritten,vs_readwritten),
  1124. { vs_referred_not_inited }
  1125. (vs_none,vs_referred_not_inited,vs_read,vs_read,vs_read_not_warned,vs_referred_not_inited,vs_written,vs_readwritten),
  1126. { vs_written -> ... }
  1127. (vs_none,vs_written,vs_written,vs_readwritten,vs_readwritten,vs_written,vs_written,vs_readwritten),
  1128. { vs_readwritten -> ... }
  1129. (vs_none,vs_readwritten,vs_readwritten,vs_readwritten,vs_readwritten,vs_readwritten,vs_readwritten,vs_readwritten));
  1130. var
  1131. hsym : tabstractvarsym;
  1132. begin
  1133. { make sure we can still warn about uninitialised use after high(v), @v etc }
  1134. if (newstate = vs_read) and
  1135. not(vsf_must_be_valid in varstateflags) then
  1136. newstate := vs_referred_not_inited;
  1137. while assigned(p) do
  1138. begin
  1139. case p.nodetype of
  1140. derefn:
  1141. begin
  1142. if (tderefnode(p).left.nodetype=temprefn) and
  1143. assigned(ttemprefnode(tderefnode(p).left).tempinfo^.withnode) then
  1144. p:=ttemprefnode(tderefnode(p).left).tempinfo^.withnode
  1145. else
  1146. break;
  1147. end;
  1148. typeconvn :
  1149. begin
  1150. case ttypeconvnode(p).convtype of
  1151. tc_cchar_2_pchar,
  1152. tc_cstring_2_pchar,
  1153. tc_array_2_pointer :
  1154. exclude(varstateflags,vsf_must_be_valid);
  1155. tc_pchar_2_string,
  1156. tc_pointer_2_array :
  1157. begin
  1158. include(varstateflags,vsf_must_be_valid);
  1159. { when a pointer is used for array access, the
  1160. pointer itself is read and never written }
  1161. newstate := vs_read;
  1162. end;
  1163. else
  1164. ;
  1165. end;
  1166. p:=tunarynode(p).left;
  1167. end;
  1168. subscriptn :
  1169. begin
  1170. if is_implicit_pointer_object_type(tunarynode(p).left.resultdef) then
  1171. newstate := vs_read;
  1172. p:=tunarynode(p).left;
  1173. end;
  1174. vecn:
  1175. begin
  1176. set_varstate(tbinarynode(p).right,vs_read,[vsf_must_be_valid]);
  1177. { dyn. arrays and dyn. strings are read }
  1178. if is_implicit_array_pointer(tunarynode(p).left.resultdef) then
  1179. newstate:=vs_read;
  1180. if (newstate in [vs_read,vs_readwritten]) or
  1181. not(tunarynode(p).left.resultdef.typ in [stringdef,arraydef]) then
  1182. include(varstateflags,vsf_must_be_valid)
  1183. else if (newstate = vs_written) then
  1184. exclude(varstateflags,vsf_must_be_valid);
  1185. p:=tunarynode(p).left;
  1186. end;
  1187. { do not parse calln }
  1188. calln :
  1189. break;
  1190. loadn :
  1191. begin
  1192. { the methodpointer/framepointer is read }
  1193. if assigned(tunarynode(p).left) then
  1194. set_varstate(tunarynode(p).left,vs_read,[vsf_must_be_valid]);
  1195. if (tloadnode(p).symtableentry.typ in [localvarsym,paravarsym,staticvarsym]) then
  1196. begin
  1197. hsym:=tabstractvarsym(tloadnode(p).symtableentry);
  1198. { this check requires proper data flow analysis... }
  1199. (* if (hsym.varspez=vs_final) and
  1200. (hsym.varstate in [vs_written,vs_readwritten]) and
  1201. (newstate in [vs_written,vs_readwritten]) then
  1202. CGMessagePos1(p.fileinfo,sym_e_final_write_once); *)
  1203. if (vsf_must_be_valid in varstateflags) and
  1204. (hsym.varstate in [vs_declared,vs_read_not_warned,vs_referred_not_inited]) then
  1205. begin
  1206. { Give warning/note for uninitialized locals }
  1207. if assigned(hsym.owner) and
  1208. not(vo_is_external in hsym.varoptions) and
  1209. (hsym.owner.symtabletype in [parasymtable,localsymtable,staticsymtable]) and
  1210. ((hsym.owner=current_procinfo.procdef.localst) or
  1211. (hsym.owner=current_procinfo.procdef.parast)) then
  1212. begin
  1213. if vsf_use_hints in varstateflags then
  1214. include(tloadnode(p).loadnodeflags,loadnf_only_uninitialized_hint);
  1215. if not(cs_opt_nodedfa in current_settings.optimizerswitches) then
  1216. begin
  1217. if (vo_is_funcret in hsym.varoptions) then
  1218. begin
  1219. { An uninitialized function Result of a managed type needs special handling.
  1220. When passing it as a var parameter a warning need to be emitted, since a user
  1221. may expect Result to be empty (nil) by default as it happens with local vars
  1222. of a managed type. But this is not true for Result and may lead to serious issues.
  1223. The only exception is SetLength(Result, ?) for a string Result. A user always
  1224. expects undefined contents of the string after calling SetLength(). In such
  1225. case a hint need to be emitted.
  1226. }
  1227. if is_managed_type(hsym.vardef) then
  1228. if not ( is_string(hsym.vardef) and (vsf_use_hint_for_string_result in varstateflags) ) then
  1229. exclude(varstateflags,vsf_use_hints);
  1230. if vsf_use_hints in varstateflags then
  1231. begin
  1232. if is_managed_type(hsym.vardef) then
  1233. CGMessagePos(p.fileinfo,sym_h_managed_function_result_uninitialized)
  1234. else
  1235. CGMessagePos(p.fileinfo,sym_h_function_result_uninitialized);
  1236. end
  1237. else
  1238. begin
  1239. if is_managed_type(hsym.vardef) then
  1240. CGMessagePos(p.fileinfo,sym_w_managed_function_result_uninitialized)
  1241. else
  1242. CGMessagePos(p.fileinfo,sym_w_function_result_uninitialized);
  1243. end;
  1244. end
  1245. else
  1246. begin
  1247. UninitializedVariableMessage(p.fileinfo,
  1248. { on the JVM, an uninitialized var-parameter
  1249. is just as fatal as a nil pointer dereference }
  1250. not((vsf_use_hints in varstateflags) and not(target_info.system in systems_jvm)),
  1251. tloadnode(p).symtable.symtabletype=localsymtable,
  1252. is_managed_type(tloadnode(p).resultdef),
  1253. hsym.realname);
  1254. end;
  1255. end;
  1256. end
  1257. else if (newstate = vs_read) then
  1258. newstate := vs_read_not_warned;
  1259. end;
  1260. hsym.varstate := vstrans[hsym.varstate,newstate];
  1261. end;
  1262. case newstate of
  1263. vs_written:
  1264. include(tloadnode(p).flags,nf_write);
  1265. vs_readwritten:
  1266. if not(nf_write in tloadnode(p).flags) then
  1267. include(tloadnode(p).flags,nf_modify);
  1268. else
  1269. ;
  1270. end;
  1271. break;
  1272. end;
  1273. addrn:
  1274. break;
  1275. callparan :
  1276. internalerror(200310081);
  1277. else
  1278. break;
  1279. end;{case }
  1280. end;
  1281. end;
  1282. procedure set_unique(p : tnode);
  1283. begin
  1284. while assigned(p) do
  1285. begin
  1286. case p.nodetype of
  1287. vecn:
  1288. begin
  1289. include(p.flags,nf_callunique);
  1290. break;
  1291. end;
  1292. typeconvn,
  1293. subscriptn,
  1294. derefn:
  1295. p:=tunarynode(p).left;
  1296. else
  1297. break;
  1298. end;
  1299. end;
  1300. end;
  1301. function valid_for_assign(p:tnode;opts:TValidAssigns; report_errors: boolean):boolean;
  1302. var
  1303. typeconvs: tfpobjectlist;
  1304. hp2,
  1305. hp : tnode;
  1306. gotstring,
  1307. gotsubscript,
  1308. gotrecord,
  1309. gotvec,
  1310. gottypeconv : boolean;
  1311. fromdef,
  1312. todef : tdef;
  1313. errmsg,
  1314. temp : longint;
  1315. function constaccessok(vs: tabstractvarsym): boolean;
  1316. begin
  1317. result:=false;
  1318. { allow p^:= constructions with p is const parameter }
  1319. if (Valid_Const in opts) or
  1320. ((hp.nodetype=loadn) and
  1321. (loadnf_isinternal_ignoreconst in tloadnode(hp).loadnodeflags)) then
  1322. result:=true
  1323. { final (class) fields can only be initialised in the (class) constructors of
  1324. class in which they have been declared (not in descendent constructors) }
  1325. else if vs.varspez=vs_final then
  1326. begin
  1327. if (current_procinfo.procdef.owner=vs.owner) then
  1328. if vs.typ=staticvarsym then
  1329. result:=current_procinfo.procdef.proctypeoption=potype_class_constructor
  1330. else
  1331. result:=current_procinfo.procdef.proctypeoption=potype_constructor;
  1332. if not result and
  1333. report_errors then
  1334. CGMessagePos(hp.fileinfo,type_e_invalid_final_assignment);
  1335. end
  1336. else
  1337. if report_errors then
  1338. CGMessagePos(hp.fileinfo,type_e_no_assign_to_const);
  1339. end;
  1340. procedure mayberesettypeconvs;
  1341. var
  1342. i: longint;
  1343. begin
  1344. if assigned(typeconvs) then
  1345. begin
  1346. if not report_errors and
  1347. not result then
  1348. for i:=0 to typeconvs.Count-1 do
  1349. ttypeconvnode(typeconvs[i]).assignment_side:=false;
  1350. typeconvs.free;
  1351. end;
  1352. end;
  1353. begin
  1354. if valid_const in opts then
  1355. errmsg:=type_e_variable_id_expected
  1356. else if valid_property in opts then
  1357. errmsg:=type_e_argument_cant_be_assigned
  1358. else
  1359. errmsg:=type_e_no_addr_of_constant;
  1360. result:=false;
  1361. gotsubscript:=false;
  1362. gotvec:=false;
  1363. gotrecord:=false;
  1364. gotstring:=false;
  1365. gottypeconv:=false;
  1366. hp:=p;
  1367. if not(valid_void in opts) and
  1368. is_void(hp.resultdef) then
  1369. begin
  1370. if report_errors then
  1371. CGMessagePos(hp.fileinfo,errmsg);
  1372. exit;
  1373. end;
  1374. typeconvs:=nil;
  1375. while assigned(hp) do
  1376. begin
  1377. { property allowed? calln has a property check itself }
  1378. if (nf_isproperty in hp.flags) then
  1379. begin
  1380. { check return type }
  1381. case hp.resultdef.typ of
  1382. recorddef :
  1383. gotrecord:=true;
  1384. stringdef :
  1385. gotstring:=true;
  1386. else
  1387. ;
  1388. end;
  1389. if (valid_property in opts) then
  1390. begin
  1391. { don't allow writing to calls that will create
  1392. temps like calls that return a structure and we
  1393. are assigning to a member }
  1394. if (valid_const in opts) or
  1395. (
  1396. { allowing assignments to typecasted properties
  1397. a) is Delphi-incompatible
  1398. b) causes problems in case the getter is a function
  1399. (because then the result of the getter is
  1400. typecasted to this type, and then we "assign" to
  1401. this typecasted function result) -> always
  1402. disallow, since property accessors should be
  1403. transparantly changeable to functions at all
  1404. times
  1405. }
  1406. not(gottypeconv) and
  1407. not(gotsubscript and gotrecord) and
  1408. not(gotstring and gotvec) and
  1409. not(nf_no_lvalue in hp.flags)
  1410. ) then
  1411. result:=true
  1412. else
  1413. if report_errors then
  1414. CGMessagePos(hp.fileinfo,errmsg);
  1415. end
  1416. else
  1417. begin
  1418. { 1. if it returns a pointer and we've found a deref,
  1419. 2. if it returns a class and a subscription or with is found
  1420. 3. if the address is needed of a field (subscriptn, vecn) }
  1421. if (gotstring and gotvec) or
  1422. (
  1423. (Valid_Addr in opts) and
  1424. (hp.nodetype in [subscriptn,vecn])
  1425. ) then
  1426. result:=true
  1427. else
  1428. if report_errors then
  1429. CGMessagePos(hp.fileinfo,errmsg);
  1430. end;
  1431. mayberesettypeconvs;
  1432. exit;
  1433. end;
  1434. case hp.nodetype of
  1435. temprefn :
  1436. begin
  1437. valid_for_assign := not(ti_readonly in ttemprefnode(hp).tempflags);
  1438. mayberesettypeconvs;
  1439. exit;
  1440. end;
  1441. derefn :
  1442. begin
  1443. { dereference -> always valid }
  1444. valid_for_assign:=true;
  1445. mayberesettypeconvs;
  1446. exit;
  1447. end;
  1448. typeconvn :
  1449. begin
  1450. gottypeconv:=true;
  1451. { typecast sizes must match, exceptions:
  1452. - implicit typecast made by absolute
  1453. - from formaldef
  1454. - from void
  1455. - from/to open array
  1456. - typecast from pointer to array }
  1457. fromdef:=ttypeconvnode(hp).left.resultdef;
  1458. todef:=hp.resultdef;
  1459. { typeconversions on the assignment side must keep
  1460. left.location the same }
  1461. if not((target_info.system in systems_jvm) and
  1462. (gotsubscript or gotvec)) then
  1463. begin
  1464. ttypeconvnode(hp).assignment_side:=true;
  1465. if not assigned(typeconvs) then
  1466. typeconvs:=tfpobjectlist.create(false);
  1467. typeconvs.add(hp);
  1468. end;
  1469. { in managed VMs, you cannot typecast formaldef when assigning
  1470. to it, see http://hallvards.blogspot.com/2007/10/dn4dp24-net-vs-win32-untyped-parameters.html }
  1471. if (target_info.system in systems_managed_vm) and
  1472. (fromdef.typ=formaldef) then
  1473. begin
  1474. if report_errors then
  1475. CGMessagePos(hp.fileinfo,type_e_no_managed_formal_assign_typecast);
  1476. mayberesettypeconvs;
  1477. exit;
  1478. end
  1479. else if not((nf_absolute in ttypeconvnode(hp).flags) or
  1480. ttypeconvnode(hp).target_specific_general_typeconv or
  1481. ((nf_explicit in hp.flags) and
  1482. ttypeconvnode(hp).target_specific_explicit_typeconv) or
  1483. (fromdef.typ=formaldef) or
  1484. is_void(fromdef) or
  1485. is_open_array(fromdef) or
  1486. is_open_array(todef) or
  1487. ((fromdef.typ=pointerdef) and (todef.typ=arraydef)) or
  1488. (def_is_related(fromdef,todef))) then
  1489. begin
  1490. if (fromdef.size<>todef.size) then
  1491. begin
  1492. { in TP it is allowed to typecast to smaller types. But the variable can't
  1493. be in a register }
  1494. if (m_tp7 in current_settings.modeswitches) or
  1495. (todef.size<fromdef.size) then
  1496. make_not_regable(hp,[ra_addr_regable])
  1497. else
  1498. if report_errors then
  1499. CGMessagePos2(hp.fileinfo,type_e_typecast_wrong_size_for_assignment,tostr(fromdef.size),tostr(todef.size));
  1500. end
  1501. {$ifdef llvm}
  1502. { we can never typecast a non-memory value on the assignment
  1503. side in llvm }
  1504. else
  1505. make_not_regable(hp,[ra_addr_regable])
  1506. {$endif llvm}
  1507. end;
  1508. { don't allow assignments to typeconvs that need special code }
  1509. if not(gotsubscript or gotvec) and
  1510. not(ttypeconvnode(hp).assign_allowed) then
  1511. begin
  1512. if report_errors then
  1513. CGMessagePos(hp.fileinfo,errmsg);
  1514. mayberesettypeconvs;
  1515. exit;
  1516. end;
  1517. case hp.resultdef.typ of
  1518. arraydef :
  1519. begin
  1520. { pointer -> array conversion is done then we need to see it
  1521. as a deref, because a ^ is then not required anymore }
  1522. if ttypeconvnode(hp).convtype=tc_pointer_2_array then
  1523. begin
  1524. valid_for_assign:=true;
  1525. mayberesettypeconvs;
  1526. exit
  1527. end;
  1528. end;
  1529. else
  1530. ;
  1531. end;
  1532. hp:=ttypeconvnode(hp).left;
  1533. end;
  1534. vecn :
  1535. begin
  1536. if (tvecnode(hp).right.nodetype=rangen) and
  1537. not(valid_range in opts) then
  1538. begin
  1539. if report_errors then
  1540. CGMessagePos(tvecnode(hp).right.fileinfo,parser_e_illegal_expression);
  1541. mayberesettypeconvs;
  1542. exit;
  1543. end;
  1544. if { only check for first (= outermost) vec node }
  1545. not gotvec and
  1546. not(valid_packed in opts) and
  1547. (tvecnode(hp).left.resultdef.typ = arraydef) and
  1548. (ado_IsBitPacked in tarraydef(tvecnode(hp).left.resultdef).arrayoptions) and
  1549. ((tarraydef(tvecnode(hp).left.resultdef).elepackedbitsize mod 8 <> 0) or
  1550. (is_ordinal(tarraydef(tvecnode(hp).left.resultdef).elementdef) and
  1551. not ispowerof2(tarraydef(tvecnode(hp).left.resultdef).elepackedbitsize div 8,temp))) then
  1552. begin
  1553. if report_errors then
  1554. if (valid_property in opts) then
  1555. CGMessagePos(hp.fileinfo,parser_e_packed_element_no_loop)
  1556. else
  1557. CGMessagePos(hp.fileinfo,parser_e_packed_element_no_var_addr);
  1558. mayberesettypeconvs;
  1559. exit;
  1560. end;
  1561. gotvec:=true;
  1562. { accesses to dyn. arrays override read only access in delphi
  1563. -- now also in FPC, because the elements of a dynamic array
  1564. returned by a function can also be changed, or you can
  1565. assign the dynamic array to a variable and then change
  1566. its elements anyway }
  1567. if is_dynamic_array(tunarynode(hp).left.resultdef) then
  1568. begin
  1569. result:=true;
  1570. mayberesettypeconvs;
  1571. exit;
  1572. end;
  1573. hp:=tunarynode(hp).left;
  1574. end;
  1575. asn :
  1576. begin
  1577. { asn can't be assigned directly, it returns the value in a register instead
  1578. of reference. }
  1579. if not(gotsubscript or gotvec) then
  1580. begin
  1581. if report_errors then
  1582. CGMessagePos(hp.fileinfo,errmsg);
  1583. mayberesettypeconvs;
  1584. exit;
  1585. end;
  1586. hp:=tunarynode(hp).left;
  1587. end;
  1588. subscriptn :
  1589. begin
  1590. { only check first (= outermost) subscriptn }
  1591. if not gotsubscript and
  1592. not(valid_packed in opts) and
  1593. is_packed_record_or_object(tsubscriptnode(hp).left.resultdef) and
  1594. ((tsubscriptnode(hp).vs.fieldoffset mod 8 <> 0) or
  1595. (is_ordinal(tsubscriptnode(hp).resultdef) and
  1596. not ispowerof2(tsubscriptnode(hp).resultdef.packedbitsize div 8,temp))) then
  1597. begin
  1598. if report_errors then
  1599. if (valid_property in opts) then
  1600. CGMessagePos(hp.fileinfo,parser_e_packed_element_no_loop)
  1601. else
  1602. CGMessagePos(hp.fileinfo,parser_e_packed_element_no_var_addr);
  1603. mayberesettypeconvs;
  1604. exit;
  1605. end;
  1606. { check for final fields }
  1607. if (tsubscriptnode(hp).vs.varspez=vs_final) and
  1608. not constaccessok(tsubscriptnode(hp).vs) then
  1609. begin
  1610. mayberesettypeconvs;
  1611. exit;
  1612. end;
  1613. { if we assign something to a field of a record that is not
  1614. regable, then then the record can't be kept in a regvar,
  1615. because we will force the record into memory for this
  1616. subscript operation (to a temp location, so the assignment
  1617. will happen to the temp and be lost) }
  1618. if not gotsubscript and
  1619. not gotvec and
  1620. not tstoreddef(hp.resultdef).is_intregable then
  1621. make_not_regable(hp,[ra_addr_regable]);
  1622. gotsubscript:=true;
  1623. { loop counter? }
  1624. if not(Valid_Const in opts) and
  1625. (vo_is_loop_counter in tsubscriptnode(hp).vs.varoptions) then
  1626. begin
  1627. if report_errors then
  1628. CGMessage1(parser_e_illegal_assignment_to_count_var,tsubscriptnode(hp).vs.realname);
  1629. mayberesettypeconvs;
  1630. exit;
  1631. end;
  1632. { implicit pointer object types result in dereferencing }
  1633. hp:=tsubscriptnode(hp).left;
  1634. if is_implicit_pointer_object_type(hp.resultdef) or
  1635. (hp.resultdef.typ=classrefdef) then
  1636. begin
  1637. valid_for_assign:=true;
  1638. mayberesettypeconvs;
  1639. exit
  1640. end;
  1641. end;
  1642. muln,
  1643. divn,
  1644. andn,
  1645. xorn,
  1646. orn,
  1647. notn,
  1648. subn,
  1649. addn :
  1650. begin
  1651. { Temp strings are stored in memory, for compatibility with
  1652. delphi only }
  1653. if (m_delphi in current_settings.modeswitches) and
  1654. ((valid_addr in opts) or
  1655. (valid_const in opts)) and
  1656. (hp.resultdef.typ=stringdef) then
  1657. result:=true
  1658. else
  1659. if report_errors then
  1660. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  1661. mayberesettypeconvs;
  1662. exit;
  1663. end;
  1664. niln,
  1665. pointerconstn :
  1666. begin
  1667. if report_errors then
  1668. CGMessagePos(hp.fileinfo,type_e_no_assign_to_addr);
  1669. mayberesettypeconvs;
  1670. exit;
  1671. end;
  1672. ordconstn,
  1673. realconstn :
  1674. begin
  1675. { these constants will be passed by value }
  1676. if report_errors then
  1677. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  1678. mayberesettypeconvs;
  1679. exit;
  1680. end;
  1681. arrayconstructorn,
  1682. setconstn,
  1683. stringconstn,
  1684. guidconstn :
  1685. begin
  1686. { these constants will be passed by reference }
  1687. if valid_const in opts then
  1688. result:=true
  1689. else
  1690. if report_errors then
  1691. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  1692. mayberesettypeconvs;
  1693. exit;
  1694. end;
  1695. addrn :
  1696. begin
  1697. if report_errors then
  1698. CGMessagePos(hp.fileinfo,type_e_no_assign_to_addr);
  1699. mayberesettypeconvs;
  1700. exit;
  1701. end;
  1702. blockn,
  1703. calln :
  1704. begin
  1705. if (hp.nodetype=calln) or
  1706. (nf_no_lvalue in hp.flags) then
  1707. begin
  1708. { Temp strings are stored in memory, for compatibility with
  1709. delphi only }
  1710. if (m_delphi in current_settings.modeswitches) and
  1711. (valid_addr in opts) and
  1712. (hp.resultdef.typ=stringdef) then
  1713. result:=true
  1714. else
  1715. if ([valid_const,valid_addr] * opts = [valid_const]) then
  1716. result:=true
  1717. else
  1718. if report_errors then
  1719. CGMessagePos(hp.fileinfo,errmsg);
  1720. mayberesettypeconvs;
  1721. exit;
  1722. end
  1723. else
  1724. begin
  1725. hp2:=tblocknode(hp).statements;
  1726. if assigned(hp2) then
  1727. begin
  1728. if hp2.nodetype<>statementn then
  1729. internalerror(2006110801);
  1730. while assigned(tstatementnode(hp2).next) do
  1731. hp2:=tstatementnode(hp2).next;
  1732. hp:=tstatementnode(hp2).statement;
  1733. end
  1734. else
  1735. begin
  1736. if report_errors then
  1737. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  1738. mayberesettypeconvs;
  1739. exit;
  1740. end;
  1741. end;
  1742. end;
  1743. inlinen :
  1744. begin
  1745. if ((valid_const in opts) and
  1746. (tinlinenode(hp).inlinenumber in [in_typeof_x])) or
  1747. (tinlinenode(hp).inlinenumber in [in_unaligned_x,in_aligned_x]) then
  1748. result:=true
  1749. else
  1750. if report_errors then
  1751. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  1752. mayberesettypeconvs;
  1753. exit;
  1754. end;
  1755. nothingn :
  1756. begin
  1757. { generics can generate nothing nodes, just allow everything }
  1758. if df_generic in current_procinfo.procdef.defoptions then
  1759. result:=true
  1760. else if report_errors then
  1761. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  1762. mayberesettypeconvs;
  1763. exit;
  1764. end;
  1765. loadn :
  1766. begin
  1767. case tloadnode(hp).symtableentry.typ of
  1768. absolutevarsym,
  1769. staticvarsym,
  1770. localvarsym,
  1771. paravarsym :
  1772. begin
  1773. { loop counter? }
  1774. if not(Valid_Const in opts) and
  1775. (vo_is_loop_counter in tabstractvarsym(tloadnode(hp).symtableentry).varoptions) then
  1776. begin
  1777. if report_errors then
  1778. CGMessage1(parser_e_illegal_assignment_to_count_var,tloadnode(hp).symtableentry.realname);
  1779. mayberesettypeconvs;
  1780. exit;
  1781. end;
  1782. { read-only variable? }
  1783. if (tabstractvarsym(tloadnode(hp).symtableentry).varspez in [vs_const,vs_constref,vs_final]) then
  1784. begin
  1785. result:=constaccessok(tabstractvarsym(tloadnode(hp).symtableentry));
  1786. mayberesettypeconvs;
  1787. exit;
  1788. end;
  1789. result:=true;
  1790. mayberesettypeconvs;
  1791. exit;
  1792. end;
  1793. procsym :
  1794. begin
  1795. if (Valid_Const in opts) then
  1796. result:=true
  1797. else
  1798. if report_errors then
  1799. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  1800. mayberesettypeconvs;
  1801. exit;
  1802. end;
  1803. labelsym :
  1804. begin
  1805. if (Valid_Addr in opts) then
  1806. result:=true
  1807. else
  1808. if report_errors then
  1809. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  1810. mayberesettypeconvs;
  1811. exit;
  1812. end;
  1813. constsym:
  1814. begin
  1815. if (tconstsym(tloadnode(hp).symtableentry).consttyp=constresourcestring) and
  1816. (valid_addr in opts) then
  1817. result:=true
  1818. else
  1819. if report_errors then
  1820. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  1821. mayberesettypeconvs;
  1822. exit;
  1823. end;
  1824. else
  1825. begin
  1826. if report_errors then
  1827. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  1828. mayberesettypeconvs;
  1829. exit;
  1830. end;
  1831. end;
  1832. end;
  1833. else
  1834. begin
  1835. if report_errors then
  1836. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  1837. mayberesettypeconvs;
  1838. exit;
  1839. end;
  1840. end;
  1841. end;
  1842. mayberesettypeconvs;
  1843. end;
  1844. function valid_for_var(p:tnode; report_errors: boolean):boolean;
  1845. begin
  1846. valid_for_var:=valid_for_assign(p,[valid_range],report_errors);
  1847. end;
  1848. function valid_for_formal_var(p : tnode; report_errors: boolean) : boolean;
  1849. begin
  1850. valid_for_formal_var:=valid_for_assign(p,[valid_void,valid_range],report_errors);
  1851. end;
  1852. function valid_for_formal_constref(p : tnode; report_errors: boolean) : boolean;
  1853. begin
  1854. valid_for_formal_constref:=(p.resultdef.typ=formaldef) or
  1855. valid_for_assign(p,[valid_void,valid_range],report_errors);
  1856. end;
  1857. function valid_for_formal_const(p : tnode; report_errors: boolean) : boolean;
  1858. begin
  1859. valid_for_formal_const:=(p.resultdef.typ=formaldef) or
  1860. valid_for_assign(p,[valid_void,valid_const,valid_property,valid_range],report_errors);
  1861. end;
  1862. function valid_for_assignment(p:tnode; report_errors: boolean):boolean;
  1863. begin
  1864. valid_for_assignment:=valid_for_assign(p,[valid_property,valid_packed],report_errors);
  1865. end;
  1866. function valid_for_loopvar(p:tnode; report_errors: boolean):boolean;
  1867. begin
  1868. valid_for_loopvar:=valid_for_assign(p,[valid_property],report_errors);
  1869. end;
  1870. function valid_for_addr(p : tnode; report_errors: boolean) : boolean;
  1871. begin
  1872. result:=valid_for_assign(p,[valid_const,valid_addr,valid_void],report_errors);
  1873. end;
  1874. procedure var_para_allowed(var eq:tequaltype;def_from,def_to:Tdef; fromnode: tnode);
  1875. begin
  1876. { Note: eq must be already valid, it will only be updated! }
  1877. case def_to.typ of
  1878. formaldef :
  1879. begin
  1880. { all types can be passed to a formaldef,
  1881. but it is not the prefered way }
  1882. if not is_constnode(fromnode) then
  1883. eq:=te_convert_l6
  1884. else
  1885. eq:=te_incompatible;
  1886. end;
  1887. orddef :
  1888. begin
  1889. { allows conversion from word to integer and
  1890. byte to shortint, but only for TP7 compatibility }
  1891. if (m_tp7 in current_settings.modeswitches) and
  1892. (def_from.typ=orddef) and
  1893. (def_from.size=def_to.size) then
  1894. eq:=te_convert_l1;
  1895. end;
  1896. arraydef :
  1897. begin
  1898. if is_open_array(def_to) then
  1899. begin
  1900. if is_dynamic_array(def_from) and
  1901. equal_defs(tarraydef(def_from).elementdef,tarraydef(def_to).elementdef) then
  1902. eq:=te_convert_l2
  1903. else
  1904. if equal_defs(def_from,tarraydef(def_to).elementdef) then
  1905. eq:=te_convert_l3;
  1906. end;
  1907. end;
  1908. pointerdef :
  1909. begin
  1910. { an implicit pointer conversion is allowed }
  1911. if (def_from.typ=pointerdef) then
  1912. eq:=te_convert_l1;
  1913. end;
  1914. stringdef :
  1915. begin
  1916. { all shortstrings are allowed, size is not important }
  1917. if is_shortstring(def_from) and
  1918. is_shortstring(def_to) then
  1919. eq:=te_equal;
  1920. end;
  1921. objectdef :
  1922. begin
  1923. { child objects can be also passed }
  1924. { in non-delphi mode, otherwise }
  1925. { they must match exactly, except }
  1926. { if they are objects }
  1927. if (def_from.typ=objectdef) and
  1928. (
  1929. (tobjectdef(def_from).objecttype=odt_object) and
  1930. (tobjectdef(def_to).objecttype=odt_object)
  1931. ) and
  1932. (def_is_related(tobjectdef(def_from),tobjectdef(def_to))) then
  1933. eq:=te_convert_l1;
  1934. end;
  1935. filedef :
  1936. begin
  1937. { an implicit file conversion is also allowed }
  1938. { from a typed file to an untyped one }
  1939. if (def_from.typ=filedef) and
  1940. (tfiledef(def_from).filetyp = ft_typed) and
  1941. (tfiledef(def_to).filetyp = ft_untyped) then
  1942. eq:=te_convert_l1;
  1943. end;
  1944. else
  1945. ;
  1946. end;
  1947. end;
  1948. procedure para_allowed(var eq:tequaltype;p:tcallparanode;def_to:tdef);
  1949. var
  1950. acn: tarrayconstructornode;
  1951. realprocdef: tprocdef;
  1952. tmpeq: tequaltype;
  1953. begin
  1954. { Note: eq must be already valid, it will only be updated! }
  1955. case def_to.typ of
  1956. stringdef :
  1957. begin
  1958. { to support ansi/long/wide strings in a proper way }
  1959. { string and string[10] are assumed as equal }
  1960. { when searching the correct overloaded procedure }
  1961. if (p.resultdef.typ=stringdef) and
  1962. (tstringdef(def_to).stringtype=tstringdef(p.resultdef).stringtype) and
  1963. (tstringdef(def_to).encoding=tstringdef(p.resultdef).encoding) then
  1964. eq:=te_equal
  1965. end;
  1966. formaldef,
  1967. setdef :
  1968. begin
  1969. { set can also be a not yet converted array constructor }
  1970. if (p.resultdef.typ=arraydef) and
  1971. is_array_constructor(p.resultdef) and
  1972. not is_variant_array(p.resultdef) then
  1973. eq:=te_equal;
  1974. end;
  1975. procvardef :
  1976. begin
  1977. tmpeq:=te_incompatible;
  1978. { in tp/macpas mode proc -> procvar is allowed }
  1979. if ((m_tp_procvar in current_settings.modeswitches) or
  1980. (m_mac_procvar in current_settings.modeswitches)) and
  1981. (p.left.nodetype=calln) then
  1982. tmpeq:=proc_to_procvar_equal(tprocdef(tcallnode(p.left).procdefinition),tprocvardef(def_to),false);
  1983. if (tmpeq=te_incompatible) and
  1984. (m_nested_procvars in current_settings.modeswitches) and
  1985. is_proc2procvar_load(p.left,realprocdef) then
  1986. tmpeq:=proc_to_procvar_equal(realprocdef,tprocvardef(def_to),false);
  1987. if (tmpeq=te_incompatible) and
  1988. (m_mac in current_settings.modeswitches) and
  1989. is_ambiguous_funcret_load(p.left,realprocdef) then
  1990. tmpeq:=proc_to_procvar_equal(realprocdef,tprocvardef(def_to),false);
  1991. if tmpeq<>te_incompatible then
  1992. eq:=tmpeq;
  1993. end;
  1994. objectdef :
  1995. begin
  1996. tmpeq:=te_incompatible;
  1997. { in tp/macpas mode proc -> funcref is allowed }
  1998. if ((m_tp_procvar in current_settings.modeswitches) or
  1999. (m_mac_procvar in current_settings.modeswitches)) and
  2000. (p.left.nodetype=calln) and
  2001. is_invokable(def_to) then
  2002. tmpeq:=proc_to_funcref_conv(tprocdef(tcallnode(p.left).procdefinition),tobjectdef(def_to));
  2003. if tmpeq<>te_incompatible then
  2004. eq:=tmpeq;
  2005. end;
  2006. arraydef :
  2007. begin
  2008. { an arrayconstructor of proccalls may have to be converted to
  2009. an array of procvars }
  2010. if ((m_tp_procvar in current_settings.modeswitches) or
  2011. (m_mac_procvar in current_settings.modeswitches)) and
  2012. (tarraydef(def_to).elementdef.typ=procvardef) and
  2013. is_array_constructor(p.resultdef) and
  2014. not is_variant_array(p.resultdef) then
  2015. begin
  2016. acn:=tarrayconstructornode(p.left);
  2017. if assigned(acn.left) then
  2018. begin
  2019. eq:=te_exact;
  2020. while assigned(acn) and
  2021. (eq<>te_incompatible) do
  2022. begin
  2023. if (acn.left.nodetype=calln) then
  2024. tmpeq:=proc_to_procvar_equal(tprocdef(tcallnode(acn.left).procdefinition),tprocvardef(tarraydef(def_to).elementdef),false)
  2025. else
  2026. tmpeq:=compare_defs(acn.left.resultdef,tarraydef(def_to).elementdef,acn.left.nodetype);
  2027. if tmpeq<eq then
  2028. eq:=tmpeq;
  2029. acn:=tarrayconstructornode(acn.right);
  2030. end;
  2031. end
  2032. end;
  2033. end;
  2034. else
  2035. ;
  2036. end;
  2037. end;
  2038. function allowenumop(nt:tnodetype):boolean;
  2039. begin
  2040. result:=(nt in [equaln,unequaln,ltn,lten,gtn,gten]) or
  2041. ((cs_allow_enum_calc in current_settings.localswitches) and
  2042. (nt in [addn,subn]));
  2043. end;
  2044. {****************************************************************************
  2045. TCallCandidates
  2046. ****************************************************************************}
  2047. constructor tcallcandidates.create(sym:tprocsym;st:TSymtable;ppn:tnode;ignorevisibility,allowdefaultparas,objcidcall,explicitunit,searchhelpers,anoninherited:boolean;spezcontext:tspecializationcontext);
  2048. begin
  2049. if not assigned(sym) then
  2050. internalerror(200411015);
  2051. FOperator:=NOTOKEN;
  2052. FProcsym:=sym;
  2053. FProcsymtable:=st;
  2054. FParanode:=ppn;
  2055. FIgnoredCandidateProcs:=tfpobjectlist.create(false);
  2056. create_candidate_list(ignorevisibility,allowdefaultparas,objcidcall,explicitunit,searchhelpers,anoninherited,spezcontext);
  2057. end;
  2058. constructor tcallcandidates.create_operator(op:ttoken;ppn:tnode);
  2059. begin
  2060. FOperator:=op;
  2061. FProcsym:=nil;
  2062. FProcsymtable:=nil;
  2063. FParanode:=ppn;
  2064. FIgnoredCandidateProcs:=tfpobjectlist.create(false);
  2065. create_candidate_list(false,false,false,false,false,false,nil);
  2066. end;
  2067. destructor tcallcandidates.destroy;
  2068. var
  2069. hpnext,
  2070. hp : pcandidate;
  2071. psym : tprocsym;
  2072. i : longint;
  2073. sym : tsym;
  2074. begin
  2075. FIgnoredCandidateProcs.free;
  2076. { free any symbols for anonymous parameter types that we're used for
  2077. specialization when no specialization was picked }
  2078. if assigned(FParaAnonSyms) then
  2079. begin
  2080. for i := 0 to FParaAnonSyms.count-1 do
  2081. tsym(FParaAnonSyms[i]).free;
  2082. FParaAnonSyms.free;
  2083. end;
  2084. hp:=FCandidateProcs;
  2085. while assigned(hp) do
  2086. begin
  2087. hpnext:=hp^.next;
  2088. { free those procdef specializations that are not owned (thus were discarded) }
  2089. if hp^.data.is_specialization and not hp^.data.is_registered then
  2090. begin
  2091. { also remove the procdef from its symbol's procdeflist }
  2092. psym:=tprocsym(hp^.data.procsym);
  2093. for i:=0 to psym.procdeflist.count-1 do
  2094. begin
  2095. if psym.procdeflist[i]=hp^.data then
  2096. begin
  2097. psym.procdeflist.delete(i);
  2098. break;
  2099. end;
  2100. end;
  2101. hp^.data.free;
  2102. end;
  2103. dispose(hp);
  2104. hp:=hpnext;
  2105. end;
  2106. end;
  2107. procedure tcallcandidates.collect_overloads_in_struct(structdef:tabstractrecorddef;ProcdefOverloadList:TFPObjectList;searchhelpers,anoninherited:boolean;spezcontext:tspecializationcontext);
  2108. var
  2109. changedhierarchy : boolean;
  2110. function processprocsym(srsym:tprocsym; out foundanything: boolean):boolean;
  2111. var
  2112. j : integer;
  2113. pd : tprocdef;
  2114. begin
  2115. { add all definitions }
  2116. result:=false;
  2117. foundanything:=false;
  2118. { try to specialize the procsym }
  2119. if srsym.could_be_implicitly_specialized and
  2120. try_implicit_specialization(srsym,FParaNode,ProcdefOverloadList,FParaAnonSyms,tsym(FProcsym),result) then
  2121. foundanything:=true;
  2122. for j:=0 to srsym.ProcdefList.Count-1 do
  2123. begin
  2124. pd:=tprocdef(srsym.ProcdefList[j]);
  2125. if not finalize_specialization(pd,spezcontext) then
  2126. continue;
  2127. if (po_ignore_for_overload_resolution in pd.procoptions) then
  2128. begin
  2129. FIgnoredCandidateProcs.add(pd);
  2130. continue;
  2131. end;
  2132. { in case of anonymous inherited, only match procdefs identical
  2133. to the current one (apart from hidden parameters), rather than
  2134. anything compatible to the parameters -- except in case of
  2135. the presence of a messagestr/int, in which case those have to
  2136. match exactly }
  2137. if anoninherited then
  2138. if po_msgint in current_procinfo.procdef.procoptions then
  2139. begin
  2140. if not(po_msgint in pd.procoptions) or
  2141. (pd.messageinf.i<>current_procinfo.procdef.messageinf.i) then
  2142. continue
  2143. end
  2144. else if po_msgstr in current_procinfo.procdef.procoptions then
  2145. begin
  2146. if not(po_msgstr in pd.procoptions) or
  2147. (pd.messageinf.str^<>current_procinfo.procdef.messageinf.str^) then
  2148. continue
  2149. end
  2150. else if (compare_paras(current_procinfo.procdef.paras,pd.paras,cp_all,[cpo_ignorehidden])<te_equal) then
  2151. continue;
  2152. foundanything:=true;
  2153. { Store first procsym found }
  2154. if not assigned(FProcsym) then
  2155. FProcsym:=tprocsym(srsym);
  2156. if po_overload in pd.procoptions then
  2157. result:=true;
  2158. { if the hierarchy had been changed we need to check for duplicates }
  2159. if not changedhierarchy or (ProcdefOverloadList.IndexOf(pd)<0) then
  2160. ProcdefOverloadList.Add(pd);
  2161. end;
  2162. end;
  2163. function processhelper(hashedid:THashedIDString;helperdef:tobjectdef):boolean;
  2164. var
  2165. srsym : tsym;
  2166. hasoverload,foundanything : boolean;
  2167. begin
  2168. result:=false;
  2169. srsym:=nil;
  2170. hasoverload:=false;
  2171. while assigned(helperdef) do
  2172. begin
  2173. srsym:=tsym(helperdef.symtable.FindWithHash(hashedid));
  2174. if assigned(srsym) and
  2175. { Delphi allows hiding a property by a procedure with the same name }
  2176. (srsym.typ=procsym) and
  2177. (tprocsym(srsym).procdeflist.count>0) then
  2178. begin
  2179. hasoverload:=processprocsym(tprocsym(srsym),foundanything);
  2180. { when there is no explicit overload we stop searching }
  2181. if foundanything and
  2182. not hasoverload then
  2183. break;
  2184. end;
  2185. helperdef:=helperdef.childof;
  2186. end;
  2187. if not hasoverload and assigned(srsym) then
  2188. exit(true);
  2189. end;
  2190. var
  2191. srsym : tsym;
  2192. hashedid : THashedIDString;
  2193. hasoverload,
  2194. foundanything : boolean;
  2195. extendeddef : tabstractrecorddef;
  2196. helperdef : tobjectdef;
  2197. helperlist : TFPObjectList;
  2198. i : integer;
  2199. begin
  2200. if FOperator=NOTOKEN then
  2201. hashedid.id:=FProcsym.name
  2202. else
  2203. hashedid.id:=overloaded_names[FOperator];
  2204. hasoverload:=false;
  2205. extendeddef:=nil;
  2206. changedhierarchy:=false;
  2207. while assigned(structdef) do
  2208. begin
  2209. { first search in helpers for this type }
  2210. if ((structdef.typ=recorddef) or
  2211. (
  2212. (structdef.typ=objectdef) and
  2213. (tobjectdef(structdef).objecttype in objecttypes_with_helpers)
  2214. )
  2215. )
  2216. and searchhelpers then
  2217. begin
  2218. if m_multi_helpers in current_settings.modeswitches then
  2219. begin
  2220. helperlist:=get_objectpascal_helpers(structdef);
  2221. if assigned(helperlist) and (helperlist.count>0) then
  2222. begin
  2223. i:=helperlist.count-1;
  2224. repeat
  2225. helperdef:=tobjectdef(helperlist[i]);
  2226. if (helperdef.owner.symtabletype in [staticsymtable,globalsymtable]) or
  2227. is_visible_for_object(helperdef.typesym,helperdef) then
  2228. if processhelper(hashedid,helperdef) then
  2229. exit;
  2230. dec(i);
  2231. until (i<0);
  2232. end;
  2233. end
  2234. else if search_last_objectpascal_helper(structdef,nil,helperdef) and processhelper(hashedid,helperdef) then
  2235. exit;
  2236. end;
  2237. { now search in the type itself }
  2238. srsym:=tsym(structdef.symtable.FindWithHash(hashedid));
  2239. if assigned(srsym) and
  2240. { Delphi allows hiding a property by a procedure with the same name }
  2241. (srsym.typ=procsym) then
  2242. begin
  2243. hasoverload:=processprocsym(tprocsym(srsym),foundanything);
  2244. { when there is no explicit overload we stop searching }
  2245. if foundanything and
  2246. not hasoverload then
  2247. break;
  2248. end;
  2249. if is_objectpascal_helper(structdef) and
  2250. (
  2251. (tobjectdef(structdef).extendeddef.typ=recorddef) or
  2252. (
  2253. (tobjectdef(structdef).extendeddef.typ=objectdef) and
  2254. (tobjectdef(tobjectdef(structdef).extendeddef).objecttype in objecttypes_with_helpers)
  2255. )
  2256. ) then
  2257. begin
  2258. { remember the first extendeddef of the hierarchy }
  2259. if not assigned(extendeddef) then
  2260. extendeddef:=tabstractrecorddef(tobjectdef(structdef).extendeddef);
  2261. { search methods in the extended type as well }
  2262. srsym:=tprocsym(tabstractrecorddef(tobjectdef(structdef).extendeddef).symtable.FindWithHash(hashedid));
  2263. if assigned(srsym) and
  2264. { Delphi allows hiding a property by a procedure with the same name }
  2265. (srsym.typ=procsym) and
  2266. (tprocsym(srsym).procdeflist.count>0) then
  2267. begin
  2268. hasoverload:=processprocsym(tprocsym(srsym),foundanything);
  2269. { when there is no explicit overload we stop searching }
  2270. if foundanything and
  2271. not hasoverload then
  2272. break;
  2273. end;
  2274. end;
  2275. { next parent }
  2276. if (structdef.typ=objectdef) then
  2277. structdef:=tobjectdef(structdef).childof
  2278. else
  2279. structdef:=nil;
  2280. { switch over to the extended def's hierarchy }
  2281. if not assigned(structdef) and assigned(extendeddef) then
  2282. begin
  2283. structdef:=extendeddef;
  2284. extendeddef:=nil;
  2285. changedhierarchy:=true;
  2286. end;
  2287. end;
  2288. end;
  2289. procedure tcallcandidates.collect_overloads_in_units(ProcdefOverloadList:TFPObjectList; objcidcall,explicitunit: boolean;spezcontext:tspecializationcontext);
  2290. var
  2291. j : integer;
  2292. pd : tprocdef;
  2293. srsymtable : TSymtable;
  2294. srsym : tsym;
  2295. checkstack : psymtablestackitem;
  2296. hashedid : THashedIDString;
  2297. foundanything,
  2298. hasoverload : boolean;
  2299. begin
  2300. { we search all overloaded operator definitions in the symtablestack. The found
  2301. entries are only added to the procs list and not the procsym, because
  2302. the list can change in every situation }
  2303. if FOperator=NOTOKEN then
  2304. begin
  2305. if not objcidcall then
  2306. hashedid.id:=FProcsym.name
  2307. else
  2308. hashedid.id:=class_helper_prefix+FProcsym.name;
  2309. end
  2310. else
  2311. hashedid.id:=overloaded_names[FOperator];
  2312. checkstack:=symtablestack.stack;
  2313. if assigned(FProcsymtable) then
  2314. begin
  2315. while assigned(checkstack) and
  2316. (checkstack^.symtable<>FProcsymtable) do
  2317. checkstack:=checkstack^.next;
  2318. end;
  2319. while assigned(checkstack) do
  2320. begin
  2321. srsymtable:=checkstack^.symtable;
  2322. { if the unit in which the routine has to be searched has been
  2323. specified explicitly, stop searching after its symtable(s) have
  2324. been checked (can be both the static and the global symtable
  2325. in case it's the current unit itself) }
  2326. if explicitunit and
  2327. (FProcsymtable.symtabletype in [globalsymtable,staticsymtable]) and
  2328. (srsymtable.moduleid<>FProcsymtable.moduleid) then
  2329. break;
  2330. if (srsymtable.symtabletype in [localsymtable,staticsymtable,globalsymtable]) and
  2331. (
  2332. (FOperator=NOTOKEN) or
  2333. (sto_has_operator in srsymtable.tableoptions)
  2334. )
  2335. then
  2336. begin
  2337. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  2338. if assigned(srsym) and
  2339. (srsym.typ=procsym) and
  2340. (
  2341. (tprocsym(srsym).procdeflist.count>0) or
  2342. (sp_generic_dummy in srsym.symoptions)
  2343. ) then
  2344. begin
  2345. { add all definitions }
  2346. hasoverload:=false;
  2347. foundanything:=false;
  2348. if tprocsym(srsym).could_be_implicitly_specialized then
  2349. foundanything:=try_implicit_specialization(srsym,FParaNode,ProcdefOverloadList,FParaAnonSyms,tsym(FProcsym),hasoverload);
  2350. for j:=0 to tprocsym(srsym).ProcdefList.Count-1 do
  2351. begin
  2352. pd:=tprocdef(tprocsym(srsym).ProcdefList[j]);
  2353. if not finalize_specialization(pd,spezcontext) then
  2354. continue;
  2355. if (po_ignore_for_overload_resolution in pd.procoptions) then
  2356. begin
  2357. FIgnoredCandidateProcs.add(pd);
  2358. continue;
  2359. end;
  2360. { Store first procsym found }
  2361. if not assigned(FProcsym) then
  2362. FProcsym:=tprocsym(srsym);
  2363. if po_overload in pd.procoptions then
  2364. hasoverload:=true;
  2365. ProcdefOverloadList.Add(pd);
  2366. foundanything:=true;
  2367. end;
  2368. { when there is no explicit overload we stop searching,
  2369. except for Objective-C methods called via id }
  2370. if foundanything and
  2371. not hasoverload and
  2372. not objcidcall then
  2373. break;
  2374. end;
  2375. end;
  2376. checkstack:=checkstack^.next
  2377. end;
  2378. end;
  2379. procedure tcallcandidates.create_candidate_list(ignorevisibility,allowdefaultparas,objcidcall,explicitunit,searchhelpers,anoninherited:boolean;spezcontext:tspecializationcontext);
  2380. var
  2381. j : integer;
  2382. pd : tprocdef;
  2383. hp : pcandidate;
  2384. pt : tcallparanode;
  2385. found,
  2386. added : boolean;
  2387. st : TSymtable;
  2388. contextstructdef : tabstractrecorddef;
  2389. ProcdefOverloadList : TFPObjectList;
  2390. cpoptions : tcompare_paras_options;
  2391. begin
  2392. FCandidateProcs:=nil;
  2393. { Find all available overloads for this procsym }
  2394. ProcdefOverloadList:=TFPObjectList.Create(false);
  2395. if not objcidcall and
  2396. (FOperator=NOTOKEN) and
  2397. (FProcsym.owner.symtabletype in [objectsymtable,recordsymtable]) then
  2398. collect_overloads_in_struct(tabstractrecorddef(FProcsym.owner.defowner),ProcdefOverloadList,searchhelpers,anoninherited,spezcontext)
  2399. else
  2400. if (FOperator<>NOTOKEN) then
  2401. begin
  2402. { check operands and if they contain records then search in records,
  2403. then search in unit }
  2404. pt:=tcallparanode(FParaNode);
  2405. while assigned(pt) do
  2406. begin
  2407. if (pt.resultdef.typ=recorddef) and
  2408. (sto_has_operator in tabstractrecorddef(pt.resultdef).symtable.tableoptions) then
  2409. collect_overloads_in_struct(tabstractrecorddef(pt.resultdef),ProcdefOverloadList,searchhelpers,anoninherited,spezcontext);
  2410. pt:=tcallparanode(pt.right);
  2411. end;
  2412. collect_overloads_in_units(ProcdefOverloadList,objcidcall,explicitunit,spezcontext);
  2413. end
  2414. else
  2415. collect_overloads_in_units(ProcdefOverloadList,objcidcall,explicitunit,spezcontext);
  2416. { determine length of parameter list.
  2417. for operators also enable the variant-operators if
  2418. a variant parameter is passed }
  2419. FParalength:=0;
  2420. FAllowVariant:=(FOperator=NOTOKEN);
  2421. pt:=tcallparanode(FParaNode);
  2422. while assigned(pt) do
  2423. begin
  2424. if (pt.resultdef.typ=variantdef) then
  2425. FAllowVariant:=true;
  2426. inc(FParalength);
  2427. pt:=tcallparanode(pt.right);
  2428. end;
  2429. { when the class passed is defined in this unit we
  2430. need to use the scope of that class. This is a trick
  2431. that can be used to access protected members in other
  2432. units. At least kylix supports it this way (PFV) }
  2433. if assigned(FProcSymtable) and
  2434. (
  2435. (FProcSymtable.symtabletype in [ObjectSymtable,recordsymtable]) or
  2436. ((FProcSymtable.symtabletype=withsymtable) and
  2437. (FProcSymtable.defowner.typ in [objectdef,recorddef]))
  2438. ) and
  2439. (FProcSymtable.defowner.owner.symtabletype in [globalsymtable,staticsymtable,objectsymtable,recordsymtable]) and
  2440. FProcSymtable.defowner.owner.iscurrentunit then
  2441. contextstructdef:=tabstractrecorddef(FProcSymtable.defowner)
  2442. else
  2443. contextstructdef:=current_structdef;
  2444. { symtable is needed later to calculate the distance }
  2445. if assigned(FProcsym) then
  2446. st:=FProcsym.Owner
  2447. else
  2448. st:=nil;
  2449. { Process all found overloads }
  2450. for j:=0 to ProcdefOverloadList.Count-1 do
  2451. begin
  2452. pd:=tprocdef(ProcdefOverloadList[j]);
  2453. added:=false;
  2454. { only when the # of parameter are supported by the procedure and
  2455. it is visible }
  2456. {$ifdef DISABLE_FAST_OVERLOAD_PATCH}
  2457. if (FParalength>=pd.minparacount) and
  2458. {$else}
  2459. if (pd.seenmarker<>pointer(self)) and (FParalength>=pd.minparacount) and
  2460. {$endif}
  2461. (
  2462. (
  2463. allowdefaultparas and
  2464. (
  2465. (FParalength<=pd.maxparacount) or
  2466. (po_varargs in pd.procoptions)
  2467. )
  2468. ) or
  2469. (
  2470. not allowdefaultparas and
  2471. (FParalength=pd.maxparacount)
  2472. )
  2473. ) and
  2474. (
  2475. ignorevisibility or
  2476. (
  2477. pd.is_specialization and not assigned(pd.owner) and
  2478. (
  2479. not (pd.genericdef.owner.symtabletype in [objectsymtable,recordsymtable]) or
  2480. is_visible_for_object(tprocdef(pd.genericdef),contextstructdef)
  2481. )
  2482. ) or
  2483. (
  2484. assigned(pd.owner) and
  2485. (
  2486. not (pd.owner.symtabletype in [objectsymtable,recordsymtable]) or
  2487. is_visible_for_object(pd,contextstructdef)
  2488. )
  2489. )
  2490. ) then
  2491. begin
  2492. { don't add duplicates, only compare visible parameters for the user }
  2493. cpoptions:=[cpo_ignorehidden];
  2494. if (po_compilerproc in pd.procoptions) then
  2495. cpoptions:=cpoptions+[cpo_compilerproc];
  2496. if (po_rtlproc in pd.procoptions) then
  2497. cpoptions:=cpoptions+[cpo_rtlproc];
  2498. found:=false;
  2499. hp:=FCandidateProcs;
  2500. {$ifdef DISABLE_FAST_OVERLOAD_PATCH}
  2501. while assigned(hp) do
  2502. begin
  2503. if (compare_paras(hp^.data.paras,pd.paras,cp_value_equal_const,cpoptions)>=te_equal) and
  2504. (not(po_objc in pd.procoptions) or
  2505. (pd.messageinf.str^=hp^.data.messageinf.str^)) then
  2506. begin
  2507. found:=true;
  2508. break;
  2509. end;
  2510. hp:=hp^.next;
  2511. end;
  2512. {$endif}
  2513. if not found then
  2514. begin
  2515. proc_add(st,pd,objcidcall);
  2516. added:=true;
  2517. {$ifndef DISABLE_FAST_OVERLOAD_PATCH}
  2518. pd.seenmarker:=self;
  2519. {$endif}
  2520. end;
  2521. end;
  2522. { we need to remove all specializations that were not used from their
  2523. procsyms as no code must be generated for them (if they are used
  2524. later on they'll be added like the ones that were used now) }
  2525. if not added and assigned(spezcontext) and not pd.is_registered then
  2526. begin
  2527. if tprocsym(pd.procsym).procdeflist.extract(pd)<>pd then
  2528. internalerror(20150828);
  2529. pd.free;
  2530. end;
  2531. end;
  2532. {$ifndef DISABLE_FAST_OVERLOAD_PATCH}
  2533. {cleanup modified duplicate pd markers}
  2534. hp := FCandidateProcs;
  2535. while assigned(hp) do begin
  2536. hp^.data.seenmarker := nil;
  2537. hp := hp^.next;
  2538. end;
  2539. {$endif}
  2540. calc_distance(st,objcidcall);
  2541. ProcdefOverloadList.Free;
  2542. end;
  2543. procedure tcallcandidates.calc_distance(st_root: tsymtable; objcidcall: boolean);
  2544. var
  2545. pd:tprocdef;
  2546. candidate:pcandidate;
  2547. st: tsymtable;
  2548. begin
  2549. { Give a small penalty for overloaded methods not defined in the
  2550. current class/unit }
  2551. st:=nil;
  2552. if objcidcall or
  2553. not assigned(st_root) or
  2554. not assigned(st_root.defowner) or
  2555. (st_root.defowner.typ<>objectdef) then
  2556. st:=st_root
  2557. else
  2558. repeat
  2559. { In case of a method, st_root is the symtable of the first found
  2560. procsym with the called method's name, but this procsym may not
  2561. contain any of the overloads that match the used parameters (which
  2562. are the procdefs that have been collected as candidates) -> walk
  2563. up the class hierarchy and look for the first class that actually
  2564. defines at least one of the candidate procdefs.
  2565. The reason is that we will penalise methods in other classes/
  2566. symtables, so if we pick a symtable that does not contain any of
  2567. the candidates, this won't help with picking the best/
  2568. most-inner-scoped one (since all of them will be penalised) }
  2569. candidate:=FCandidateProcs;
  2570. { the current class contains one of the candidates? }
  2571. while assigned(candidate) do
  2572. begin
  2573. pd:=candidate^.data;
  2574. if pd.owner=st_root then
  2575. begin
  2576. { yes -> choose this class }
  2577. st:=st_root;
  2578. break;
  2579. end;
  2580. candidate:=candidate^.next;
  2581. end;
  2582. { None found -> go to parent class }
  2583. if not assigned(st) then
  2584. begin
  2585. if not assigned(st_root.defowner) then
  2586. internalerror(201605301);
  2587. { no more parent class -> take current class as root anyway
  2588. (could maybe happen in case of a class helper?) }
  2589. if not assigned(tobjectdef(st_root.defowner).childof) then
  2590. begin
  2591. st:=st_root;
  2592. break;
  2593. end;
  2594. st_root:=tobjectdef(st_root.defowner).childof.symtable;
  2595. end;
  2596. until assigned(st);
  2597. candidate:=FCandidateProcs;
  2598. { when calling Objective-C methods via id.method, then the found
  2599. procsym will be inside an arbitrary ObjectSymtable, and we don't
  2600. want to give the methods of that particular objcclass precedence
  2601. over other methods, so instead check against the symtable in
  2602. which this objcclass is defined }
  2603. if objcidcall then
  2604. st:=st.defowner.owner;
  2605. while assigned(candidate) do
  2606. begin
  2607. pd:=candidate^.data;
  2608. if st<>pd.owner then
  2609. candidate^.ordinal_distance:=candidate^.ordinal_distance+1.0;
  2610. candidate:=candidate^.next;
  2611. end;
  2612. end;
  2613. function tcallcandidates.proc_add(st:tsymtable;pd:tprocdef;objcidcall: boolean):pcandidate;
  2614. var
  2615. defaultparacnt : integer;
  2616. begin
  2617. { generate new candidate entry }
  2618. new(result);
  2619. fillchar(result^,sizeof(tcandidate),0);
  2620. result^.data:=pd;
  2621. result^.next:=FCandidateProcs;
  2622. FCandidateProcs:=result;
  2623. inc(FProccnt);
  2624. { Find last parameter, skip all default parameters
  2625. that are not passed. Ignore this skipping for varargs }
  2626. result^.firstparaidx:=pd.paras.count-1;
  2627. if not(po_varargs in pd.procoptions) then
  2628. begin
  2629. { ignore hidden parameters }
  2630. while (result^.firstparaidx>=0) and (vo_is_hidden_para in tparavarsym(pd.paras[result^.firstparaidx]).varoptions) do
  2631. dec(result^.firstparaidx);
  2632. defaultparacnt:=pd.maxparacount-FParalength;
  2633. if defaultparacnt>0 then
  2634. begin
  2635. if defaultparacnt>result^.firstparaidx+1 then
  2636. internalerror(200401141);
  2637. dec(result^.firstparaidx,defaultparacnt);
  2638. end;
  2639. end;
  2640. end;
  2641. procedure tcallcandidates.list(all:boolean);
  2642. var
  2643. hp : pcandidate;
  2644. begin
  2645. hp:=FCandidateProcs;
  2646. while assigned(hp) do
  2647. begin
  2648. if all or
  2649. (not hp^.invalid) then
  2650. MessagePos1(hp^.data.fileinfo,sym_h_param_list,hp^.data.fullprocname(false));
  2651. hp:=hp^.next;
  2652. end;
  2653. end;
  2654. {$ifdef EXTDEBUG}
  2655. procedure tcallcandidates.dump_info(lvl:longint);
  2656. function ParaTreeStr(p:tcallparanode):string;
  2657. begin
  2658. result:='';
  2659. while assigned(p) do
  2660. begin
  2661. if result<>'' then
  2662. result:=','+result;
  2663. result:=p.resultdef.typename+result;
  2664. p:=tcallparanode(p.right);
  2665. end;
  2666. end;
  2667. var
  2668. hp : pcandidate;
  2669. i : integer;
  2670. currpara : tparavarsym;
  2671. begin
  2672. if not CheckVerbosity(lvl) then
  2673. exit;
  2674. Comment(lvl+V_LineInfo,'Overloaded callnode: '+FProcsym.name+'('+ParaTreeStr(tcallparanode(FParaNode))+')');
  2675. hp:=FCandidateProcs;
  2676. while assigned(hp) do
  2677. begin
  2678. Comment(lvl,' '+hp^.data.fullprocname(false));
  2679. if (hp^.invalid) then
  2680. Comment(lvl,' invalid')
  2681. else
  2682. begin
  2683. Comment(lvl,' ex: '+tostr(hp^.exact_count)+
  2684. ' eq: '+tostr(hp^.equal_count)+
  2685. ' l1: '+tostr(hp^.cl1_count)+
  2686. ' l2: '+tostr(hp^.cl2_count)+
  2687. ' l3: '+tostr(hp^.cl3_count)+
  2688. ' l4: '+tostr(hp^.cl4_count)+
  2689. ' l5: '+tostr(hp^.cl5_count)+
  2690. ' l6: '+tostr(hp^.cl6_count)+
  2691. ' oper: '+tostr(hp^.coper_count)+
  2692. ' ord: '+realtostr(hp^.ordinal_distance));
  2693. { Print parameters in left-right order }
  2694. for i:=0 to hp^.data.paras.count-1 do
  2695. begin
  2696. currpara:=tparavarsym(hp^.data.paras[i]);
  2697. if not(vo_is_hidden_para in currpara.varoptions) then
  2698. Comment(lvl,' - '+currpara.vardef.typename+' : '+EqualTypeName[currpara.eqval]);
  2699. end;
  2700. end;
  2701. hp:=hp^.next;
  2702. end;
  2703. end;
  2704. {$endif EXTDEBUG}
  2705. procedure tcallcandidates.get_information;
  2706. var
  2707. hp : pcandidate;
  2708. currpara : tparavarsym;
  2709. paraidx : integer;
  2710. currparanr : byte;
  2711. rfh,rth : double;
  2712. obj_from,
  2713. obj_to : tobjectdef;
  2714. def_from,
  2715. def_to : tdef;
  2716. currpt,
  2717. pt : tcallparanode;
  2718. eq,
  2719. mineq : tequaltype;
  2720. convtype : tconverttype;
  2721. pdtemp,
  2722. pdoper : tprocdef;
  2723. releasecurrpt : boolean;
  2724. cdoptions : tcompare_defs_options;
  2725. n : tnode;
  2726. {$push}
  2727. {$r-}
  2728. {$q-}
  2729. const
  2730. inf=1.0/0.0;
  2731. {$pop}
  2732. begin
  2733. cdoptions:=[cdo_check_operator];
  2734. if FAllowVariant then
  2735. include(cdoptions,cdo_allow_variant);
  2736. { process all procs }
  2737. hp:=FCandidateProcs;
  2738. while assigned(hp) do
  2739. begin
  2740. { We compare parameters in reverse order (right to left),
  2741. the firstpara is already pointing to the last parameter
  2742. were we need to start comparing }
  2743. currparanr:=FParalength;
  2744. paraidx:=hp^.firstparaidx;
  2745. while (paraidx>=0) and (vo_is_hidden_para in tparavarsym(hp^.data.paras[paraidx]).varoptions) do
  2746. dec(paraidx);
  2747. pt:=tcallparanode(FParaNode);
  2748. while assigned(pt) and (paraidx>=0) do
  2749. begin
  2750. currpara:=tparavarsym(hp^.data.paras[paraidx]);
  2751. { currpt can be changed from loadn to calln when a procvar
  2752. is passed. This is to prevent that the change is permanent }
  2753. currpt:=pt;
  2754. releasecurrpt:=false;
  2755. { retrieve current parameter definitions to compares }
  2756. eq:=te_incompatible;
  2757. def_from:=currpt.resultdef;
  2758. def_to:=currpara.vardef;
  2759. if not(assigned(def_from)) then
  2760. internalerror(200212091);
  2761. if not(
  2762. assigned(def_to) or
  2763. ((po_varargs in hp^.data.procoptions) and
  2764. (currparanr>hp^.data.minparacount))
  2765. ) then
  2766. internalerror(200212092);
  2767. { Convert tp procvars when not expecting a procvar }
  2768. if (currpt.left.resultdef.typ=procvardef) and
  2769. not(def_to.typ in [procvardef,formaldef]) and
  2770. { if it doesn't require any parameters }
  2771. (tprocvardef(currpt.left.resultdef).minparacount=0) and
  2772. { Only convert to call when there is no overload or the return type
  2773. is compatible with the expected type. }
  2774. (
  2775. (count=1) or
  2776. (compare_defs_ext(tprocvardef(currpt.left.resultdef).returndef,def_to,nothingn,convtype,pdoper,[])>te_incompatible)
  2777. ) then
  2778. begin
  2779. releasecurrpt:=true;
  2780. currpt:=tcallparanode(pt.getcopy);
  2781. if maybe_call_procvar(currpt.left,true) then
  2782. begin
  2783. currpt.resultdef:=currpt.left.resultdef;
  2784. def_from:=currpt.left.resultdef;
  2785. end;
  2786. end;
  2787. { If we expect a procvar and the left is loadnode that
  2788. returns a procdef we need to find the correct overloaded
  2789. procdef that matches the expected procvar. The loadnode
  2790. temporary returned the first procdef (PFV) }
  2791. if (
  2792. (def_to.typ=procvardef) or
  2793. is_funcref(def_to)
  2794. ) and
  2795. (currpt.left.nodetype=loadn) and
  2796. (currpt.left.resultdef.typ=procdef) then
  2797. begin
  2798. if def_to.typ=procvardef then
  2799. pdtemp:=tprocsym(Tloadnode(currpt.left).symtableentry).Find_procdef_byprocvardef(Tprocvardef(def_to))
  2800. else
  2801. pdtemp:=tprocsym(tloadnode(currpt.left).symtableentry).find_procdef_byfuncrefdef(tobjectdef(def_to));
  2802. if assigned(pdtemp) then
  2803. begin
  2804. tloadnode(currpt.left).setprocdef(pdtemp);
  2805. currpt.resultdef:=currpt.left.resultdef;
  2806. def_from:=currpt.left.resultdef;
  2807. end;
  2808. end;
  2809. { same as above, but for the case that we have a proc-2-procvar
  2810. conversion together with a load }
  2811. if (
  2812. (def_to.typ=procvardef) or
  2813. is_funcref(def_to)
  2814. ) and
  2815. (currpt.left.nodetype=typeconvn) and
  2816. (ttypeconvnode(currpt.left).convtype=tc_proc_2_procvar) and
  2817. (ttypeconvnode(currpt.left).totypedef=voidtype) and
  2818. not (nf_explicit in currpt.left.flags) and
  2819. (ttypeconvnode(currpt.left).left.nodetype=loadn) and
  2820. (ttypeconvnode(currpt.left).left.resultdef.typ=procdef) then
  2821. begin
  2822. if def_to.typ=procvardef then
  2823. pdtemp:=tprocsym(tloadnode(ttypeconvnode(currpt.left).left).symtableentry).Find_procdef_byprocvardef(Tprocvardef(def_to))
  2824. else
  2825. pdtemp:=tprocsym(tloadnode(ttypeconvnode(currpt.left).left).symtableentry).find_procdef_byfuncrefdef(tobjectdef(def_to));
  2826. if assigned(pdtemp) then
  2827. begin
  2828. tloadnode(ttypeconvnode(currpt.left).left).setprocdef(pdtemp);
  2829. ttypeconvnode(currpt.left).totypedef:=cprocvardef.getreusableprocaddr(pdtemp,pc_normal);
  2830. ttypeconvnode(currpt.left).resultdef:=ttypeconvnode(currpt.left).totypedef;
  2831. def_from:=ttypeconvnode(currpt.left).resultdef;
  2832. end;
  2833. end;
  2834. { varargs are always equal, but not exact }
  2835. if (po_varargs in hp^.data.procoptions) and
  2836. (currparanr>hp^.data.minparacount) and
  2837. not is_array_of_const(def_from) and
  2838. not is_array_constructor(def_from) then
  2839. eq:=te_equal
  2840. else
  2841. { same definition -> exact }
  2842. if (def_from=def_to) then
  2843. eq:=te_exact
  2844. else
  2845. { for value and const parameters check if a integer is constant or
  2846. included in other integer -> equal and calc ordinal_distance }
  2847. if not(currpara.varspez in [vs_var,vs_out]) and
  2848. is_integer(def_from) and
  2849. is_integer(def_to) and
  2850. is_in_limit(def_from,def_to) then
  2851. begin
  2852. eq:=te_equal;
  2853. hp^.ordinal_distance:=hp^.ordinal_distance+
  2854. abs(bestreal(torddef(def_from).low)-bestreal(torddef(def_to).low));
  2855. rth:=bestreal(torddef(def_to).high);
  2856. rfh:=bestreal(torddef(def_from).high);
  2857. hp^.ordinal_distance:=hp^.ordinal_distance+abs(rth-rfh);
  2858. { Give wrong sign a small penalty, this is need to get a diffrence
  2859. from word->[longword,longint] }
  2860. if is_signed(def_from)<>is_signed(def_to) then
  2861. {$push}
  2862. {$r-}
  2863. {$q-}
  2864. hp^.ordinal_distance:=nextafter(hp^.ordinal_distance,inf);
  2865. {$pop}
  2866. end
  2867. else
  2868. { for value and const parameters check precision of real, give
  2869. penalty for loosing of precision. var and out parameters must match exactly }
  2870. if not(currpara.varspez in [vs_var,vs_out]) and
  2871. is_real_or_cextended(def_from) and
  2872. is_real_or_cextended(def_to) then
  2873. begin
  2874. eq:=te_equal;
  2875. if is_extended(def_to) then
  2876. rth:=4
  2877. else
  2878. if is_double (def_to) then
  2879. rth:=2
  2880. else
  2881. rth:=1;
  2882. if is_extended(def_from) then
  2883. rfh:=4
  2884. else
  2885. if is_double (def_from) then
  2886. rfh:=2
  2887. else
  2888. rfh:=1;
  2889. { penalty for shrinking of precision }
  2890. if rth<rfh then
  2891. rfh:=(rfh-rth)*16
  2892. else
  2893. rfh:=rth-rfh;
  2894. hp^.ordinal_distance:=hp^.ordinal_distance+rfh;
  2895. end
  2896. else
  2897. { related object parameters also need to determine the distance between the current
  2898. object and the object we are comparing with. var and out parameters must match exactly }
  2899. if not(currpara.varspez in [vs_var,vs_out]) and
  2900. (def_from.typ=objectdef) and
  2901. (def_to.typ=objectdef) and
  2902. (tobjectdef(def_from).objecttype=tobjectdef(def_to).objecttype) and
  2903. def_is_related(tobjectdef(def_from),tobjectdef(def_to)) then
  2904. begin
  2905. eq:=te_convert_l1;
  2906. { resolve anonymous external class definitions }
  2907. obj_from:=find_real_class_definition(tobjectdef(def_from),false);
  2908. obj_to:=find_real_class_definition(tobjectdef(def_to),false);
  2909. while assigned(obj_from) do
  2910. begin
  2911. if obj_from=obj_to then
  2912. break;
  2913. hp^.ordinal_distance:=hp^.ordinal_distance+1;
  2914. obj_from:=obj_from.childof;
  2915. end;
  2916. end
  2917. { compare_defs_ext compares sets and array constructors very poorly because
  2918. it has too little information. So we do explicitly a detailed comparisation,
  2919. see also bug #11288 (FK)
  2920. }
  2921. else if (def_to.typ=setdef) and is_array_constructor(currpt.left.resultdef) then
  2922. begin
  2923. n:=currpt.left.getcopy;
  2924. arrayconstructor_to_set(n);
  2925. eq:=compare_defs_ext(n.resultdef,def_to,n.nodetype,convtype,pdoper,cdoptions);
  2926. n.free;
  2927. end
  2928. else if is_open_array(def_to) and
  2929. is_class_or_interface_or_dispinterface_or_objc_or_java(tarraydef(def_to).elementdef) and
  2930. is_array_constructor(currpt.left.resultdef) and
  2931. assigned(tarrayconstructornode(currpt.left).left) then
  2932. begin
  2933. { ensure that [nil] can be converted to "array of tobject",
  2934. because if we just try to convert "array of pointer" to
  2935. "array of tobject", we get type conversion errors in
  2936. non-Delphi modes }
  2937. n:=currpt.left;
  2938. mineq:=te_exact;
  2939. repeat
  2940. if tarrayconstructornode(n).left.nodetype=arrayconstructorrangen then
  2941. eq:=te_incompatible
  2942. else
  2943. eq:=compare_defs_ext(tarrayconstructornode(n).left.resultdef,tarraydef(def_to).elementdef,tarrayconstructornode(n).left.nodetype,convtype,pdoper,cdoptions);
  2944. if eq<mineq then
  2945. mineq:=eq;
  2946. if eq=te_incompatible then
  2947. break;
  2948. n:=tarrayconstructornode(n).right;
  2949. until not assigned(n);
  2950. eq:=mineq;
  2951. end
  2952. else
  2953. { generic type comparision }
  2954. begin
  2955. if (hp^.data.procoptions*[po_rtlproc,po_compilerproc]=[]) and
  2956. is_ansistring(def_from) and
  2957. is_ansistring(def_to) and
  2958. (tstringdef(def_from).encoding<>tstringdef(def_to).encoding) and
  2959. (currpara.varspez in [vs_var,vs_out]) then
  2960. eq:=te_convert_l1 // don't allow to pass different ansistring types to each-other
  2961. else
  2962. eq:=compare_defs_ext(def_from,def_to,currpt.left.nodetype,convtype,pdoper,cdoptions);
  2963. { when the types are not equal we need to check
  2964. some special case for parameter passing }
  2965. if (eq<te_equal) then
  2966. begin
  2967. if currpara.varspez in [vs_var,vs_out] then
  2968. begin
  2969. { para requires an equal type so the previous found
  2970. match was not good enough, reset to incompatible }
  2971. eq:=te_incompatible;
  2972. { var_para_allowed will return te_equal and te_convert_l1 to
  2973. make a difference for best matching }
  2974. var_para_allowed(eq,currpt.resultdef,currpara.vardef,currpt.left)
  2975. end
  2976. else
  2977. para_allowed(eq,currpt,def_to);
  2978. end;
  2979. end;
  2980. { univ parameters match if the size matches (don't override the
  2981. comparison result if it was ok, since a match based on the
  2982. "univ" character is the lowest possible match) }
  2983. if (eq=te_incompatible) and
  2984. currpara.univpara and
  2985. is_valid_univ_para_type(def_from) and
  2986. (def_from.size=def_to.size) then
  2987. eq:=te_convert_l5;
  2988. { when a procvar was changed to a call an exact match is
  2989. downgraded to equal. This way an overload call with the
  2990. procvar is choosen. See tb0471 (PFV) }
  2991. if (pt<>currpt) and (eq=te_exact) then
  2992. eq:=te_equal;
  2993. { increase correct counter }
  2994. case eq of
  2995. te_exact :
  2996. inc(hp^.exact_count);
  2997. te_equal :
  2998. inc(hp^.equal_count);
  2999. te_convert_l1 :
  3000. inc(hp^.cl1_count);
  3001. te_convert_l2 :
  3002. inc(hp^.cl2_count);
  3003. te_convert_l3 :
  3004. inc(hp^.cl3_count);
  3005. te_convert_l4 :
  3006. inc(hp^.cl4_count);
  3007. te_convert_l5 :
  3008. inc(hp^.cl5_count);
  3009. te_convert_l6 :
  3010. inc(hp^.cl6_count);
  3011. te_convert_operator :
  3012. inc(hp^.coper_count);
  3013. te_incompatible :
  3014. hp^.invalid:=true;
  3015. end;
  3016. { stop checking when an incompatible parameter is found }
  3017. if hp^.invalid then
  3018. begin
  3019. { store the current parameter info for
  3020. a nice error message when no procedure is found }
  3021. hp^.wrongparaidx:=paraidx;
  3022. hp^.wrongparanr:=currparanr;
  3023. break;
  3024. end;
  3025. {$ifdef EXTDEBUG}
  3026. { store equal in node tree for dump }
  3027. currpara.eqval:=eq;
  3028. {$endif EXTDEBUG}
  3029. { maybe release temp currpt }
  3030. if releasecurrpt then
  3031. currpt.free;
  3032. { next parameter in the call tree }
  3033. pt:=tcallparanode(pt.right);
  3034. { next parameter for definition, only goto next para
  3035. if we're out of the varargs }
  3036. if not(po_varargs in hp^.data.procoptions) or
  3037. (currparanr<=hp^.data.maxparacount) then
  3038. begin
  3039. { Ignore vs_hidden parameters }
  3040. repeat
  3041. dec(paraidx);
  3042. until (paraidx<0) or not(vo_is_hidden_para in tparavarsym(hp^.data.paras[paraidx]).varoptions);
  3043. end;
  3044. dec(currparanr);
  3045. end;
  3046. if not(hp^.invalid) and
  3047. (assigned(pt) or (paraidx>=0) or (currparanr<>0)) then
  3048. internalerror(200212141);
  3049. { next candidate }
  3050. hp:=hp^.next;
  3051. end;
  3052. end;
  3053. function get_variantequaltype(def: tdef): tvariantequaltype;
  3054. const
  3055. variantorddef_cl: array[tordtype] of tvariantequaltype =
  3056. (tve_incompatible,tve_byte,tve_word,tve_cardinal,tve_chari64,tve_incompatible,
  3057. tve_shortint,tve_smallint,tve_longint,tve_chari64,tve_incompatible,
  3058. tve_boolformal,tve_boolformal,tve_boolformal,tve_boolformal,tve_boolformal,
  3059. tve_boolformal,tve_boolformal,tve_boolformal,tve_boolformal,
  3060. tve_chari64,tve_chari64,tve_dblcurrency,tve_incompatible);
  3061. { TODO: fixme for 128 bit floats }
  3062. variantfloatdef_cl: array[tfloattype] of tvariantequaltype =
  3063. (tve_single,tve_dblcurrency,tve_extended,tve_extended,
  3064. tve_dblcurrency,tve_dblcurrency,tve_extended);
  3065. variantstringdef_cl: array[tstringtype] of tvariantequaltype =
  3066. (tve_sstring,tve_astring,tve_astring,tve_wstring,tve_ustring);
  3067. begin
  3068. case def.typ of
  3069. orddef:
  3070. begin
  3071. result:=variantorddef_cl[torddef(def).ordtype];
  3072. end;
  3073. floatdef:
  3074. begin
  3075. result:=variantfloatdef_cl[tfloatdef(def).floattype];
  3076. end;
  3077. stringdef:
  3078. begin
  3079. result:=variantstringdef_cl[tstringdef(def).stringtype];
  3080. end;
  3081. formaldef:
  3082. begin
  3083. result:=tve_boolformal;
  3084. end;
  3085. else
  3086. begin
  3087. result:=tve_incompatible;
  3088. end;
  3089. end
  3090. end;
  3091. function is_better_candidate(currpd,bestpd:pcandidate):integer;
  3092. var
  3093. res : integer;
  3094. begin
  3095. {
  3096. Return values:
  3097. > 0 when currpd is better than bestpd
  3098. < 0 when bestpd is better than currpd
  3099. = 0 when both are equal
  3100. To choose the best candidate we use the following order:
  3101. - Incompatible flag
  3102. - (Smaller) Number of convert operator parameters.
  3103. - (Smaller) Number of convertlevel 2 parameters.
  3104. - (Smaller) Number of convertlevel 1 parameters.
  3105. - (Bigger) Number of exact parameters.
  3106. - (Smaller) Number of equal parameters.
  3107. - (Smaller) Total of ordinal distance. For example, the distance of a word
  3108. to a byte is 65535-255=65280.
  3109. }
  3110. if bestpd^.invalid then
  3111. begin
  3112. if currpd^.invalid then
  3113. res:=0
  3114. else
  3115. res:=1;
  3116. end
  3117. else
  3118. if currpd^.invalid then
  3119. res:=-1
  3120. else
  3121. begin
  3122. { less operator parameters? }
  3123. res:=(bestpd^.coper_count-currpd^.coper_count);
  3124. if (res=0) then
  3125. begin
  3126. { less cl6 parameters? }
  3127. res:=(bestpd^.cl6_count-currpd^.cl6_count);
  3128. if (res=0) then
  3129. begin
  3130. { less cl5 parameters? }
  3131. res:=(bestpd^.cl5_count-currpd^.cl5_count);
  3132. if (res=0) then
  3133. begin
  3134. { less cl4 parameters? }
  3135. res:=(bestpd^.cl4_count-currpd^.cl4_count);
  3136. if (res=0) then
  3137. begin
  3138. { less cl3 parameters? }
  3139. res:=(bestpd^.cl3_count-currpd^.cl3_count);
  3140. if (res=0) then
  3141. begin
  3142. { less cl2 parameters? }
  3143. res:=(bestpd^.cl2_count-currpd^.cl2_count);
  3144. if (res=0) then
  3145. begin
  3146. { less cl1 parameters? }
  3147. res:=(bestpd^.cl1_count-currpd^.cl1_count);
  3148. if (res=0) then
  3149. begin
  3150. { more exact parameters? }
  3151. res:=(currpd^.exact_count-bestpd^.exact_count);
  3152. if (res=0) then
  3153. begin
  3154. { less equal parameters? }
  3155. res:=(bestpd^.equal_count-currpd^.equal_count);
  3156. if (res=0) then
  3157. begin
  3158. { smaller ordinal distance? }
  3159. if (currpd^.ordinal_distance<bestpd^.ordinal_distance) then
  3160. res:=1
  3161. else
  3162. if (currpd^.ordinal_distance>bestpd^.ordinal_distance) then
  3163. res:=-1
  3164. else
  3165. res:=0;
  3166. { if a specialization is better than a non-specialization then
  3167. the non-generic always wins }
  3168. if m_implicit_function_specialization in current_settings.modeswitches then
  3169. begin
  3170. if (currpd^.data.is_specialization and not bestpd^.data.is_specialization) then
  3171. res:=-1
  3172. else if (not currpd^.data.is_specialization and bestpd^.data.is_specialization) then
  3173. res:=1;
  3174. end;
  3175. end;
  3176. end;
  3177. end;
  3178. end;
  3179. end;
  3180. end;
  3181. end;
  3182. end;
  3183. end;
  3184. end;
  3185. is_better_candidate:=res;
  3186. end;
  3187. { Delphi precedence rules extracted from test programs. Only valid if passing
  3188. a variant parameter to overloaded procedures expecting exactly one parameter.
  3189. single > (char, currency, int64, shortstring, ansistring, widestring, unicodestring, extended, double)
  3190. double/currency > (char, int64, shortstring, ansistring, widestring, unicodestring, extended)
  3191. extended > (char, int64, shortstring, ansistring, widestring, unicodestring)
  3192. longint/cardinal > (int64, shortstring, ansistring, widestring, unicodestring, extended, double, single, char, currency)
  3193. smallint > (longint, int64, shortstring, ansistring, widestring, unicodestring, extended, double single, char, currency);
  3194. word > (longint, cardinal, int64, shortstring, ansistring, widestring, unicodestring, extended, double single, char, currency);
  3195. shortint > (longint, smallint, int64, shortstring, ansistring, widestring, unicodestring, extended, double, single, char, currency)
  3196. byte > (longint, cardinal, word, smallint, int64, shortstring, ansistring, widestring, unicodestring, extended, double, single, char, currency);
  3197. boolean/formal > (char, int64, shortstring, ansistring, widestring, unicodestring)
  3198. widestring > (char, int64, shortstring, ansistring, unicodestring)
  3199. unicodestring > (char, int64, shortstring, ansistring)
  3200. ansistring > (char, int64, shortstring)
  3201. shortstring > (char, int64)
  3202. Relations not mentioned mean that they conflict: no decision possible }
  3203. function is_better_candidate_single_variant(currpd,bestpd:pcandidate):integer;
  3204. function calculate_relation(const currvcl, bestvcl, testvcl:
  3205. tvariantequaltype; const conflictvcls: tvariantequaltypes):integer;
  3206. begin
  3207. { if (bestvcl=conflictvcl) or
  3208. (currvcl=conflictvcl) then
  3209. result:=0
  3210. else if (bestvcl=testvcl) then
  3211. result:=-1
  3212. else result:=1 }
  3213. result:=1-2*ord(bestvcl=testvcl)+
  3214. ord(currvcl in conflictvcls)-ord(bestvcl in conflictvcls);
  3215. end;
  3216. function getfirstrealparaidx(pd: pcandidate): integer;
  3217. begin
  3218. { can be different for currpd and bestpd in case of overloaded }
  3219. { functions, e.g. lowercase():char and lowercase():shortstring }
  3220. { (depending on the calling convention and parameter order) }
  3221. result:=pd^.firstparaidx;
  3222. while (result>=0) and (vo_is_hidden_para in tparavarsym(pd^.data.paras[result]).varoptions) do
  3223. dec(result);
  3224. if (vo_is_hidden_para in tparavarsym(pd^.data.paras[result]).varoptions) then
  3225. internalerror(2006122803);
  3226. end;
  3227. var
  3228. currpara, bestpara: tparavarsym;
  3229. currvcl, bestvcl: tvariantequaltype;
  3230. begin
  3231. {
  3232. Return values:
  3233. > 0 when currpd is better than bestpd
  3234. < 0 when bestpd is better than currpd
  3235. = 0 when both are equal
  3236. }
  3237. currpara:=tparavarsym(currpd^.data.paras[getfirstrealparaidx(currpd)]);
  3238. bestpara:=tparavarsym(bestpd^.data.paras[getfirstrealparaidx(bestpd)]);
  3239. { if one of the parameters is a regular variant, fall back to the }
  3240. { default algorithm }
  3241. if (currpara.vardef.typ = variantdef) or
  3242. (bestpara.vardef.typ = variantdef) then
  3243. begin
  3244. result:=is_better_candidate(currpd,bestpd);
  3245. exit;
  3246. end;
  3247. currvcl:=get_variantequaltype(currpara.vardef);
  3248. bestvcl:=get_variantequaltype(bestpara.vardef);
  3249. { sanity check }
  3250. result:=-5;
  3251. { if both are the same, there is a conflict }
  3252. if (currvcl=bestvcl) then
  3253. result:=0
  3254. { if one of the two cannot be used as variant, the other is better }
  3255. else if (bestvcl=tve_incompatible) then
  3256. result:=1
  3257. else if (currvcl=tve_incompatible) then
  3258. result:=-1
  3259. { boolean and formal are better than chari64str, but conflict with }
  3260. { everything else }
  3261. else if (currvcl=tve_boolformal) or
  3262. (bestvcl=tve_boolformal) then
  3263. if (currvcl=tve_boolformal) then
  3264. result:=ord(bestvcl in [tve_chari64,tve_sstring,tve_astring,tve_wstring,tve_ustring])
  3265. else
  3266. result:=-ord(currvcl in [tve_chari64,tve_sstring,tve_astring,tve_wstring,tve_ustring])
  3267. { byte is better than everything else (we assume both aren't byte, }
  3268. { since there's only one parameter and that one can't be the same) }
  3269. else if (currvcl=tve_byte) or
  3270. (bestvcl=tve_byte) then
  3271. result:=calculate_relation(currvcl,bestvcl,tve_byte,[tve_shortint])
  3272. { shortint conflicts with word and cardinal, but is better than }
  3273. { everything else but byte (which has already been handled) }
  3274. else if (currvcl=tve_shortint) or
  3275. (bestvcl=tve_shortint) then
  3276. result:=calculate_relation(currvcl,bestvcl,tve_shortint,[tve_word, tve_cardinal])
  3277. { word conflicts with smallint, but is better than everything else }
  3278. { but shortint and byte (which has already been handled) }
  3279. else if (currvcl=tve_word) or
  3280. (bestvcl=tve_word) then
  3281. result:=calculate_relation(currvcl,bestvcl,tve_word,[tve_smallint])
  3282. { smallint conflicts with cardinal, but is better than everything }
  3283. { which has not yet been tested }
  3284. else if (currvcl=tve_smallint) or
  3285. (bestvcl=tve_smallint) then
  3286. result:=calculate_relation(currvcl,bestvcl,tve_smallint,[tve_cardinal])
  3287. { cardinal conflicts with each longint and is better than everything }
  3288. { which has not yet been tested }
  3289. else if (currvcl=tve_cardinal) or
  3290. (bestvcl=tve_cardinal) then
  3291. result:=calculate_relation(currvcl,bestvcl,tve_cardinal,[tve_longint])
  3292. { longint is better than everything which has not yet been tested }
  3293. else if (currvcl=tve_longint) or
  3294. (bestvcl=tve_longint) then
  3295. { if bestvcl=tve_longint then
  3296. result:=-1
  3297. else
  3298. result:=1 }
  3299. result:=1-2*ord(bestvcl=tve_longint)
  3300. { single is better than everything left }
  3301. else if (currvcl=tve_single) or
  3302. (bestvcl=tve_single) then
  3303. result:=1-2*ord(bestvcl=tve_single)
  3304. { double/comp/currency are better than everything left, and conflict }
  3305. { with each other (but that's already tested) }
  3306. else if (currvcl=tve_dblcurrency) or
  3307. (bestvcl=tve_dblcurrency) then
  3308. result:=1-2*ord(bestvcl=tve_dblcurrency)
  3309. { extended is better than everything left }
  3310. else if (currvcl=tve_extended) or
  3311. (bestvcl=tve_extended) then
  3312. result:=1-2*ord(bestvcl=tve_extended)
  3313. { widestring is better than everything left }
  3314. else if (currvcl=tve_wstring) or
  3315. (bestvcl=tve_wstring) then
  3316. result:=1-2*ord(bestvcl=tve_wstring)
  3317. { unicodestring is better than everything left }
  3318. else if (currvcl=tve_ustring) or
  3319. (bestvcl=tve_ustring) then
  3320. result:=1-2*ord(bestvcl=tve_ustring)
  3321. { ansistring is better than everything left }
  3322. else if (currvcl=tve_astring) or
  3323. (bestvcl=tve_astring) then
  3324. result:=1-2*ord(bestvcl=tve_astring)
  3325. { shortstring is better than everything left }
  3326. else if (currvcl=tve_sstring) or
  3327. (bestvcl=tve_sstring) then
  3328. result:=1-2*ord(bestvcl=tve_sstring);
  3329. { all possibilities should have been checked now }
  3330. if (result=-5) then
  3331. internalerror(2006122805);
  3332. end;
  3333. {$ifdef DISABLE_FAST_OVERLOAD_PATCH}
  3334. function tcallcandidates.choose_best(var bestpd:tabstractprocdef; singlevariant: boolean):integer;
  3335. var
  3336. pd: tprocdef;
  3337. besthpstart,
  3338. hp : pcandidate;
  3339. cntpd,
  3340. res : integer;
  3341. begin
  3342. {
  3343. Returns the number of candidates left and the
  3344. first candidate is returned in pdbest
  3345. }
  3346. { Setup the first procdef as best, only count it as a result
  3347. when it is valid }
  3348. bestpd:=FCandidateProcs^.data;
  3349. if FCandidateProcs^.invalid then
  3350. cntpd:=0
  3351. else
  3352. cntpd:=1;
  3353. if assigned(FCandidateProcs^.next) then
  3354. begin
  3355. besthpstart:=FCandidateProcs;
  3356. hp:=FCandidateProcs^.next;
  3357. while assigned(hp) do
  3358. begin
  3359. if not singlevariant then
  3360. res:=is_better_candidate(hp,besthpstart)
  3361. else
  3362. res:=is_better_candidate_single_variant(hp,besthpstart);
  3363. if (res>0) then
  3364. begin
  3365. { hp is better, flag all procs to be incompatible }
  3366. while (besthpstart<>hp) do
  3367. begin
  3368. besthpstart^.invalid:=true;
  3369. besthpstart:=besthpstart^.next;
  3370. end;
  3371. { besthpstart is already set to hp }
  3372. bestpd:=besthpstart^.data;
  3373. cntpd:=1;
  3374. end
  3375. else
  3376. if (res<0) then
  3377. begin
  3378. { besthpstart is better, flag current hp to be incompatible }
  3379. hp^.invalid:=true;
  3380. end
  3381. else
  3382. begin
  3383. { res=0, both are valid }
  3384. if not hp^.invalid then
  3385. inc(cntpd);
  3386. end;
  3387. hp:=hp^.next;
  3388. end;
  3389. end;
  3390. { if we've found one, check the procdefs ignored for overload choosing
  3391. to see whether they contain one from a child class with the same
  3392. parameters (so the overload choosing was not influenced by their
  3393. presence, but now that we've decided which overloaded version to call,
  3394. make sure we call the version closest in terms of visibility }
  3395. if cntpd=1 then
  3396. begin
  3397. for res:=0 to FIgnoredCandidateProcs.count-1 do
  3398. begin
  3399. pd:=tprocdef(FIgnoredCandidateProcs[res]);
  3400. { stop searching when we start comparing methods of parent of
  3401. the struct in which the current best method was found }
  3402. if assigned(pd.struct) and
  3403. (pd.struct<>tprocdef(bestpd).struct) and
  3404. def_is_related(tprocdef(bestpd).struct,pd.struct) then
  3405. break;
  3406. if (pd.proctypeoption=bestpd.proctypeoption) and
  3407. ((pd.procoptions*[po_classmethod,po_methodpointer])=(bestpd.procoptions*[po_classmethod,po_methodpointer])) and
  3408. (compare_paras(pd.paras,bestpd.paras,cp_all,[cpo_ignorehidden,cpo_ignoreuniv,cpo_openequalisexact])=te_exact) then
  3409. begin
  3410. { first one encountered is closest in terms of visibility }
  3411. bestpd:=pd;
  3412. break;
  3413. end;
  3414. end;
  3415. end;
  3416. result:=cntpd;
  3417. end;
  3418. {$else}
  3419. function compare_by_old_sortout_check(pd,bestpd:pcandidate):integer;
  3420. var cpoptions : tcompare_paras_options;
  3421. begin
  3422. { don't add duplicates, only compare visible parameters for the user }
  3423. cpoptions:=[cpo_ignorehidden];
  3424. if (po_compilerproc in bestpd^.data.procoptions) then
  3425. cpoptions:=cpoptions+[cpo_compilerproc];
  3426. if (po_rtlproc in bestpd^.data.procoptions) then
  3427. cpoptions:=cpoptions+[cpo_rtlproc];
  3428. compare_by_old_sortout_check := 0; // can't decide, bestpd probably wasn't sorted out in unpatched
  3429. if (compare_paras(pd^.data.paras,bestpd^.data.paras,cp_value_equal_const,cpoptions)>=te_equal) and
  3430. (not(po_objc in bestpd^.data.procoptions) or (bestpd^.data.messageinf.str^=pd^.data.messageinf.str^)) then
  3431. compare_by_old_sortout_check := 1; // bestpd was sorted out before patch
  3432. { for implicit specializations non-generics should take precedence so
  3433. when comparing a specialization to a non-specialization mark as undecided
  3434. and it will be re-evaluated in is_better_candidate }
  3435. if (m_implicit_function_specialization in current_settings.modeswitches)
  3436. and (pd^.data.is_specialization <> bestpd^.data.is_specialization) then
  3437. compare_by_old_sortout_check:=0;
  3438. end;
  3439. function decide_restart(pd,bestpd:pcandidate) : boolean;
  3440. begin
  3441. decide_restart := false;
  3442. if assigned(bestpd) then
  3443. begin
  3444. { don't restart if bestpd is marked invalid already }
  3445. if not bestpd^.invalid then
  3446. decide_restart := compare_by_old_sortout_check(pd,bestpd)<>0;
  3447. end;
  3448. end;
  3449. procedure save_validity(c : pcandidate);
  3450. begin
  3451. while assigned(c) do
  3452. begin
  3453. c^.saved_validity := c^.invalid;
  3454. c := c^.next;
  3455. end;
  3456. end;
  3457. procedure restore_validity(c : pcandidate);
  3458. begin
  3459. while assigned(c) do begin
  3460. c^.invalid := c^.saved_validity;
  3461. c := c^.next;
  3462. end;
  3463. end;
  3464. function tcallcandidates.choose_best(var bestpd:tabstractprocdef; singlevariant: boolean):integer;
  3465. var
  3466. pd: tprocdef;
  3467. besthpstart,
  3468. hp,hp2 : pcandidate;
  3469. cntpd,
  3470. res : integer;
  3471. restart : boolean;
  3472. begin
  3473. res:=0;
  3474. {
  3475. Returns the number of candidates left and the
  3476. first candidate is returned in pdbest
  3477. }
  3478. if not(assigned(FCandidateProcs)) then
  3479. begin
  3480. choose_best := 0;
  3481. exit;
  3482. end;
  3483. bestpd:=FCandidateProcs^.data;
  3484. if FCandidateProcs^.invalid then
  3485. cntpd:=0
  3486. else
  3487. cntpd:=1;
  3488. if assigned(FCandidateProcs^.next) then
  3489. begin
  3490. save_validity(FCandidateProcs);
  3491. restart := false;
  3492. { keep restarting, until there wasn't a sorted-out besthpstart }
  3493. repeat
  3494. besthpstart:=FCandidateProcs;
  3495. bestpd:=FCandidateProcs^.data;
  3496. if restart then
  3497. begin
  3498. restore_validity(FCandidateProcs);
  3499. restart := false;
  3500. end;
  3501. { Setup the first procdef as best, only count it as a result
  3502. when it is valid }
  3503. if besthpstart^.invalid then
  3504. cntpd:=0
  3505. else
  3506. cntpd:=1;
  3507. hp:=FCandidateProcs^.next;
  3508. while assigned(hp) and not(restart) do
  3509. begin
  3510. restart := decide_restart(hp,besthpstart);
  3511. if not restart then
  3512. begin
  3513. if besthpstart^.invalid then res := 1
  3514. else if hp^.invalid then res := -1
  3515. else if not singlevariant then
  3516. res:=is_better_candidate(hp,besthpstart)
  3517. else
  3518. res:=is_better_candidate_single_variant(hp,besthpstart);
  3519. end;
  3520. if restart then
  3521. begin
  3522. { mark the sorted out invalid globally }
  3523. besthpstart^.saved_validity := true;
  3524. end
  3525. else if (res>0) then
  3526. begin
  3527. { hp is better, flag all procs to be incompatible }
  3528. while (besthpstart<>hp) do
  3529. begin
  3530. besthpstart^.invalid:=true;
  3531. besthpstart:=besthpstart^.next;
  3532. end;
  3533. { besthpstart is already set to hp }
  3534. bestpd:=besthpstart^.data;
  3535. if besthpstart^.invalid then
  3536. cntpd:=0
  3537. else
  3538. cntpd:=1;
  3539. end
  3540. else if (res<0) then
  3541. begin
  3542. { besthpstart is better, flag current hp to be incompatible }
  3543. hp^.invalid:=true;
  3544. end
  3545. else
  3546. begin
  3547. { res=0, both are valid }
  3548. if not hp^.invalid then
  3549. inc(cntpd);
  3550. end;
  3551. hp:=hp^.next;
  3552. end;
  3553. until not(restart);
  3554. end;
  3555. { check the alternate choices if they would have been sorted out before patch... }
  3556. { note we have procadded the candidates, so order is reversed procadd order here.
  3557. this was also used above: each sorted-out always has an "outsorter" counterpart
  3558. deeper down the next chain
  3559. }
  3560. { for the intial implementation, let's first do some more consistency checking}
  3561. res := 0;
  3562. hp := FCandidateProcs;
  3563. while assigned(hp) do
  3564. begin
  3565. if not(hp^.invalid) then
  3566. inc(res);
  3567. hp := hp^.next;
  3568. end;
  3569. if (res<>cntpd) then
  3570. internalerror(202002161);
  3571. { check all valid choices for sortout }
  3572. cntpd := 0;
  3573. hp := FCandidateProcs;
  3574. while assigned(hp) do
  3575. begin
  3576. if not(hp^.invalid) then
  3577. begin
  3578. hp2 := hp^.next;
  3579. while assigned(hp2) do begin
  3580. if compare_by_old_sortout_check(hp2,hp)<>0 then
  3581. begin
  3582. hp^.invalid := true;
  3583. hp2 := nil;
  3584. end
  3585. else
  3586. hp2:=hp2^.next;
  3587. end;
  3588. if not(hp^.invalid) then
  3589. begin
  3590. inc(cntpd);
  3591. { check for the impossible event bestpd had become invalid}
  3592. if (cntpd=1) and (hp^.data<>bestpd) then
  3593. internalerror(202002162);
  3594. end;
  3595. end;
  3596. hp := hp^.next;
  3597. end;
  3598. { if we've found one, check the procdefs ignored for overload choosing
  3599. to see whether they contain one from a child class with the same
  3600. parameters (so the overload choosing was not influenced by their
  3601. presence, but now that we've decided which overloaded version to call,
  3602. make sure we call the version closest in terms of visibility }
  3603. if cntpd=1 then
  3604. begin
  3605. for res:=0 to FIgnoredCandidateProcs.count-1 do
  3606. begin
  3607. pd:=tprocdef(FIgnoredCandidateProcs[res]);
  3608. { stop searching when we start comparing methods of parent of
  3609. the struct in which the current best method was found }
  3610. if assigned(pd.struct) and
  3611. (pd.struct<>tprocdef(bestpd).struct) and
  3612. def_is_related(tprocdef(bestpd).struct,pd.struct) then
  3613. break;
  3614. if (pd.proctypeoption=bestpd.proctypeoption) and
  3615. ((pd.procoptions*[po_classmethod,po_methodpointer])=(bestpd.procoptions*[po_classmethod,po_methodpointer])) and
  3616. (compare_paras(pd.paras,bestpd.paras,cp_all,[cpo_ignorehidden,cpo_ignoreuniv,cpo_openequalisexact])=te_exact) then
  3617. begin
  3618. { first one encountered is closest in terms of visibility }
  3619. bestpd:=pd;
  3620. break;
  3621. end;
  3622. end;
  3623. end;
  3624. result:=cntpd;
  3625. end;
  3626. {$endif}
  3627. procedure tcallcandidates.find_wrong_para;
  3628. var
  3629. currparanr : smallint;
  3630. hp : pcandidate;
  3631. pt : tcallparanode;
  3632. wrongpara : tparavarsym;
  3633. begin
  3634. { Only process the first overloaded procdef }
  3635. hp:=FCandidateProcs;
  3636. { Find callparanode corresponding to the argument }
  3637. pt:=tcallparanode(FParanode);
  3638. currparanr:=FParalength;
  3639. while assigned(pt) and
  3640. (currparanr>hp^.wrongparanr) do
  3641. begin
  3642. pt:=tcallparanode(pt.right);
  3643. dec(currparanr);
  3644. end;
  3645. if (currparanr<>hp^.wrongparanr) or
  3646. not assigned(pt) then
  3647. internalerror(200212094);
  3648. { Show error message, when it was a var or out parameter
  3649. guess that it is a missing typeconv }
  3650. wrongpara:=tparavarsym(hp^.data.paras[hp^.wrongparaidx]);
  3651. if wrongpara.varspez in [vs_var,vs_out] then
  3652. begin
  3653. { Maybe passing the correct type but passing a const to var parameter }
  3654. if (compare_defs(pt.resultdef,wrongpara.vardef,pt.nodetype)<>te_incompatible) and
  3655. not valid_for_var(pt.left,true) then
  3656. CGMessagePos(pt.left.fileinfo,type_e_variable_id_expected)
  3657. else
  3658. CGMessagePos3(pt.left.fileinfo,parser_e_call_by_ref_without_typeconv,tostr(hp^.wrongparanr),
  3659. FullTypeName(pt.left.resultdef,wrongpara.vardef),
  3660. FullTypeName(wrongpara.vardef,pt.left.resultdef))
  3661. end
  3662. else
  3663. CGMessagePos3(pt.left.fileinfo,type_e_wrong_parameter_type,tostr(hp^.wrongparanr),
  3664. FullTypeName(pt.left.resultdef,wrongpara.vardef),
  3665. FullTypeName(wrongpara.vardef,pt.left.resultdef));
  3666. end;
  3667. procedure check_ranges(const location: tfileposinfo; source: tnode; destdef: tdef);
  3668. begin
  3669. if not(cs_check_ordinal_size in current_settings.localswitches) then
  3670. exit;
  3671. { check if the assignment may cause a range check error }
  3672. { if its not explicit, and only if the values are }
  3673. { ordinals, enumdef and floatdef }
  3674. if assigned(destdef) and
  3675. (destdef.typ in [enumdef,orddef,floatdef]) and
  3676. not is_boolean(destdef) and
  3677. assigned(source.resultdef) and
  3678. (source.resultdef.typ in [enumdef,orddef,floatdef]) and
  3679. not is_boolean(source.resultdef) and
  3680. not is_constrealnode(source) and
  3681. { constants are handled via regular range checking }
  3682. (source.nodetype<>ordconstn) then
  3683. begin
  3684. if ((destdef.size < source.resultdef.size) and
  3685. { s80real and sc80real have a different size but the same precision }
  3686. not((destdef.typ=floatdef) and
  3687. (source.resultdef.typ=floatdef) and
  3688. (tfloatdef(source.resultdef).floattype in [s80real,sc80real]) and
  3689. (tfloatdef(destdef).floattype in [s80real,sc80real]))) or
  3690. ((destdef.typ<>floatdef) and
  3691. (source.resultdef.typ<>floatdef) and
  3692. not is_in_limit(source.resultdef,destdef)) then
  3693. begin
  3694. if (cs_check_range in current_settings.localswitches) then
  3695. MessagePos(location,type_w_smaller_possible_range_check)
  3696. else
  3697. MessagePos(location,type_h_smaller_possible_range_check);
  3698. end;
  3699. end;
  3700. end;
  3701. function is_valid_for_default(def:tdef):boolean;
  3702. function is_valid_record_or_object(def:tabstractrecorddef):boolean;
  3703. var
  3704. sym : tsym;
  3705. i : longint;
  3706. begin
  3707. for i:=0 to def.symtable.symlist.count-1 do
  3708. begin
  3709. sym:=tsym(def.symtable.symlist[i]);
  3710. if not is_normal_fieldvarsym(sym) then
  3711. continue;
  3712. if not is_valid_for_default(tfieldvarsym(sym).vardef) then
  3713. begin
  3714. result:=false;
  3715. exit;
  3716. end;
  3717. end;
  3718. result:=true;
  3719. end;
  3720. begin
  3721. case def.typ of
  3722. recorddef:
  3723. result:=is_valid_record_or_object(tabstractrecorddef(def));
  3724. objectdef:
  3725. if is_implicit_pointer_object_type(def) then
  3726. result:=true
  3727. else
  3728. if is_object(def) then
  3729. result:=is_valid_record_or_object(tabstractrecorddef(def))
  3730. else
  3731. result:=false;
  3732. arraydef:
  3733. if not (ado_isdynamicarray in tarraydef(def).arrayoptions) then
  3734. result:=is_valid_for_default(tarraydef(def).elementdef)
  3735. else
  3736. result:=true;
  3737. formaldef,
  3738. abstractdef,
  3739. filedef:
  3740. result:=false;
  3741. else
  3742. result:=true;
  3743. end;
  3744. end;
  3745. end.