htypechk.pas 158 KB

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