htypechk.pas 159 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977
  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. objectdef :
  1994. begin
  1995. tmpeq:=te_incompatible;
  1996. { in tp/macpas mode proc -> funcref is allowed }
  1997. if ((m_tp_procvar in current_settings.modeswitches) or
  1998. (m_mac_procvar in current_settings.modeswitches)) and
  1999. (p.left.nodetype=calln) and
  2000. is_invokable(def_to) then
  2001. tmpeq:=proc_to_funcref_equal(tprocdef(tcallnode(p.left).procdefinition),tobjectdef(def_to));
  2002. if tmpeq<>te_incompatible then
  2003. eq:=tmpeq;
  2004. end;
  2005. arraydef :
  2006. begin
  2007. { an arrayconstructor of proccalls may have to be converted to
  2008. an array of procvars }
  2009. if ((m_tp_procvar in current_settings.modeswitches) or
  2010. (m_mac_procvar in current_settings.modeswitches)) and
  2011. (tarraydef(def_to).elementdef.typ=procvardef) and
  2012. is_array_constructor(p.resultdef) and
  2013. not is_variant_array(p.resultdef) then
  2014. begin
  2015. acn:=tarrayconstructornode(p.left);
  2016. if assigned(acn.left) then
  2017. begin
  2018. eq:=te_exact;
  2019. while assigned(acn) and
  2020. (eq<>te_incompatible) do
  2021. begin
  2022. if (acn.left.nodetype=calln) then
  2023. tmpeq:=proc_to_procvar_equal(tprocdef(tcallnode(acn.left).procdefinition),tprocvardef(tarraydef(def_to).elementdef),false)
  2024. else
  2025. tmpeq:=compare_defs(acn.left.resultdef,tarraydef(def_to).elementdef,acn.left.nodetype);
  2026. if tmpeq<eq then
  2027. eq:=tmpeq;
  2028. acn:=tarrayconstructornode(acn.right);
  2029. end;
  2030. end
  2031. end;
  2032. end;
  2033. else
  2034. ;
  2035. end;
  2036. end;
  2037. function allowenumop(nt:tnodetype):boolean;
  2038. begin
  2039. result:=(nt in [equaln,unequaln,ltn,lten,gtn,gten]) or
  2040. ((cs_allow_enum_calc in current_settings.localswitches) and
  2041. (nt in [addn,subn]));
  2042. end;
  2043. {****************************************************************************
  2044. TCallCandidates
  2045. ****************************************************************************}
  2046. constructor tcallcandidates.create(sym:tprocsym;st:TSymtable;ppn:tnode;ignorevisibility,allowdefaultparas,objcidcall,explicitunit,searchhelpers,anoninherited:boolean;spezcontext:tspecializationcontext);
  2047. begin
  2048. if not assigned(sym) then
  2049. internalerror(200411015);
  2050. FOperator:=NOTOKEN;
  2051. FProcsym:=sym;
  2052. FProcsymtable:=st;
  2053. FParanode:=ppn;
  2054. FIgnoredCandidateProcs:=tfpobjectlist.create(false);
  2055. create_candidate_list(ignorevisibility,allowdefaultparas,objcidcall,explicitunit,searchhelpers,anoninherited,spezcontext);
  2056. end;
  2057. constructor tcallcandidates.create_operator(op:ttoken;ppn:tnode);
  2058. begin
  2059. FOperator:=op;
  2060. FProcsym:=nil;
  2061. FProcsymtable:=nil;
  2062. FParanode:=ppn;
  2063. FIgnoredCandidateProcs:=tfpobjectlist.create(false);
  2064. create_candidate_list(false,false,false,false,false,false,nil);
  2065. end;
  2066. destructor tcallcandidates.destroy;
  2067. var
  2068. hpnext,
  2069. hp : pcandidate;
  2070. psym : tprocsym;
  2071. i : longint;
  2072. sym : tsym;
  2073. begin
  2074. FIgnoredCandidateProcs.free;
  2075. { free any symbols for anonymous parameter types that we're used for
  2076. specialization when no specialization was picked }
  2077. if assigned(FParaAnonSyms) then
  2078. begin
  2079. for i := 0 to FParaAnonSyms.count-1 do
  2080. tsym(FParaAnonSyms[i]).free;
  2081. FParaAnonSyms.free;
  2082. end;
  2083. hp:=FCandidateProcs;
  2084. while assigned(hp) do
  2085. begin
  2086. hpnext:=hp^.next;
  2087. { free those procdef specializations that are not owned (thus were discarded) }
  2088. if hp^.data.is_specialization and not hp^.data.is_registered then
  2089. begin
  2090. { also remove the procdef from its symbol's procdeflist }
  2091. psym:=tprocsym(hp^.data.procsym);
  2092. for i:=0 to psym.procdeflist.count-1 do
  2093. begin
  2094. if psym.procdeflist[i]=hp^.data then
  2095. begin
  2096. psym.procdeflist.delete(i);
  2097. break;
  2098. end;
  2099. end;
  2100. hp^.data.free;
  2101. end;
  2102. dispose(hp);
  2103. hp:=hpnext;
  2104. end;
  2105. end;
  2106. procedure tcallcandidates.collect_overloads_in_struct(structdef:tabstractrecorddef;ProcdefOverloadList:TFPObjectList;searchhelpers,anoninherited:boolean;spezcontext:tspecializationcontext);
  2107. var
  2108. changedhierarchy : boolean;
  2109. function processprocsym(srsym:tprocsym; out foundanything: boolean):boolean;
  2110. var
  2111. j : integer;
  2112. pd : tprocdef;
  2113. begin
  2114. { add all definitions }
  2115. result:=false;
  2116. foundanything:=false;
  2117. { try to specialize the procsym }
  2118. if srsym.could_be_implicitly_specialized and
  2119. try_implicit_specialization(srsym,FParaNode,ProcdefOverloadList,FParaAnonSyms,tsym(FProcsym),result) then
  2120. foundanything:=true;
  2121. for j:=0 to srsym.ProcdefList.Count-1 do
  2122. begin
  2123. pd:=tprocdef(srsym.ProcdefList[j]);
  2124. if not finalize_specialization(pd,spezcontext) then
  2125. continue;
  2126. if (po_ignore_for_overload_resolution in pd.procoptions) then
  2127. begin
  2128. FIgnoredCandidateProcs.add(pd);
  2129. continue;
  2130. end;
  2131. { in case of anonymous inherited, only match procdefs identical
  2132. to the current one (apart from hidden parameters), rather than
  2133. anything compatible to the parameters -- except in case of
  2134. the presence of a messagestr/int, in which case those have to
  2135. match exactly }
  2136. if anoninherited then
  2137. if po_msgint in current_procinfo.procdef.procoptions then
  2138. begin
  2139. if not(po_msgint in pd.procoptions) or
  2140. (pd.messageinf.i<>current_procinfo.procdef.messageinf.i) then
  2141. continue
  2142. end
  2143. else if po_msgstr in current_procinfo.procdef.procoptions then
  2144. begin
  2145. if not(po_msgstr in pd.procoptions) or
  2146. (pd.messageinf.str^<>current_procinfo.procdef.messageinf.str^) then
  2147. continue
  2148. end
  2149. else if (compare_paras(current_procinfo.procdef.paras,pd.paras,cp_all,[cpo_ignorehidden])<te_equal) then
  2150. continue;
  2151. foundanything:=true;
  2152. { Store first procsym found }
  2153. if not assigned(FProcsym) then
  2154. FProcsym:=tprocsym(srsym);
  2155. if po_overload in pd.procoptions then
  2156. result:=true;
  2157. { if the hierarchy had been changed we need to check for duplicates }
  2158. if not changedhierarchy or (ProcdefOverloadList.IndexOf(pd)<0) then
  2159. ProcdefOverloadList.Add(pd);
  2160. end;
  2161. end;
  2162. function processhelper(hashedid:THashedIDString;helperdef:tobjectdef):boolean;
  2163. var
  2164. srsym : tsym;
  2165. hasoverload,foundanything : boolean;
  2166. begin
  2167. result:=false;
  2168. srsym:=nil;
  2169. hasoverload:=false;
  2170. while assigned(helperdef) do
  2171. begin
  2172. srsym:=tsym(helperdef.symtable.FindWithHash(hashedid));
  2173. if assigned(srsym) and
  2174. { Delphi allows hiding a property by a procedure with the same name }
  2175. (srsym.typ=procsym) and
  2176. (tprocsym(srsym).procdeflist.count>0) then
  2177. begin
  2178. hasoverload:=processprocsym(tprocsym(srsym),foundanything);
  2179. { when there is no explicit overload we stop searching }
  2180. if foundanything and
  2181. not hasoverload then
  2182. break;
  2183. end;
  2184. helperdef:=helperdef.childof;
  2185. end;
  2186. if not hasoverload and assigned(srsym) then
  2187. exit(true);
  2188. end;
  2189. var
  2190. srsym : tsym;
  2191. hashedid : THashedIDString;
  2192. hasoverload,
  2193. foundanything : boolean;
  2194. extendeddef : tabstractrecorddef;
  2195. helperdef : tobjectdef;
  2196. helperlist : TFPObjectList;
  2197. i : integer;
  2198. begin
  2199. if FOperator=NOTOKEN then
  2200. hashedid.id:=FProcsym.name
  2201. else
  2202. hashedid.id:=overloaded_names[FOperator];
  2203. hasoverload:=false;
  2204. extendeddef:=nil;
  2205. changedhierarchy:=false;
  2206. while assigned(structdef) do
  2207. begin
  2208. { first search in helpers for this type }
  2209. if ((structdef.typ=recorddef) or
  2210. (
  2211. (structdef.typ=objectdef) and
  2212. (tobjectdef(structdef).objecttype in objecttypes_with_helpers)
  2213. )
  2214. )
  2215. and searchhelpers then
  2216. begin
  2217. if m_multi_helpers in current_settings.modeswitches then
  2218. begin
  2219. helperlist:=get_objectpascal_helpers(structdef);
  2220. if assigned(helperlist) and (helperlist.count>0) then
  2221. begin
  2222. i:=helperlist.count-1;
  2223. repeat
  2224. helperdef:=tobjectdef(helperlist[i]);
  2225. if (helperdef.owner.symtabletype in [staticsymtable,globalsymtable]) or
  2226. is_visible_for_object(helperdef.typesym,helperdef) then
  2227. if processhelper(hashedid,helperdef) then
  2228. exit;
  2229. dec(i);
  2230. until (i<0);
  2231. end;
  2232. end
  2233. else if search_last_objectpascal_helper(structdef,nil,helperdef) and processhelper(hashedid,helperdef) then
  2234. exit;
  2235. end;
  2236. { now search in the type itself }
  2237. srsym:=tsym(structdef.symtable.FindWithHash(hashedid));
  2238. if assigned(srsym) and
  2239. { Delphi allows hiding a property by a procedure with the same name }
  2240. (srsym.typ=procsym) then
  2241. begin
  2242. hasoverload:=processprocsym(tprocsym(srsym),foundanything);
  2243. { when there is no explicit overload we stop searching }
  2244. if foundanything and
  2245. not hasoverload then
  2246. break;
  2247. end;
  2248. if is_objectpascal_helper(structdef) and
  2249. (
  2250. (tobjectdef(structdef).extendeddef.typ=recorddef) or
  2251. (
  2252. (tobjectdef(structdef).extendeddef.typ=objectdef) and
  2253. (tobjectdef(tobjectdef(structdef).extendeddef).objecttype in objecttypes_with_helpers)
  2254. )
  2255. ) then
  2256. begin
  2257. { remember the first extendeddef of the hierarchy }
  2258. if not assigned(extendeddef) then
  2259. extendeddef:=tabstractrecorddef(tobjectdef(structdef).extendeddef);
  2260. { search methods in the extended type as well }
  2261. srsym:=tprocsym(tabstractrecorddef(tobjectdef(structdef).extendeddef).symtable.FindWithHash(hashedid));
  2262. if assigned(srsym) and
  2263. { Delphi allows hiding a property by a procedure with the same name }
  2264. (srsym.typ=procsym) and
  2265. (tprocsym(srsym).procdeflist.count>0) then
  2266. begin
  2267. hasoverload:=processprocsym(tprocsym(srsym),foundanything);
  2268. { when there is no explicit overload we stop searching }
  2269. if foundanything and
  2270. not hasoverload then
  2271. break;
  2272. end;
  2273. end;
  2274. { next parent }
  2275. if (structdef.typ=objectdef) then
  2276. structdef:=tobjectdef(structdef).childof
  2277. else
  2278. structdef:=nil;
  2279. { switch over to the extended def's hierarchy }
  2280. if not assigned(structdef) and assigned(extendeddef) then
  2281. begin
  2282. structdef:=extendeddef;
  2283. extendeddef:=nil;
  2284. changedhierarchy:=true;
  2285. end;
  2286. end;
  2287. end;
  2288. procedure tcallcandidates.collect_overloads_in_units(ProcdefOverloadList:TFPObjectList; objcidcall,explicitunit: boolean;spezcontext:tspecializationcontext);
  2289. var
  2290. j : integer;
  2291. pd : tprocdef;
  2292. srsymtable : TSymtable;
  2293. srsym : tsym;
  2294. checkstack : psymtablestackitem;
  2295. hashedid : THashedIDString;
  2296. foundanything,
  2297. hasoverload : boolean;
  2298. begin
  2299. { we search all overloaded operator definitions in the symtablestack. The found
  2300. entries are only added to the procs list and not the procsym, because
  2301. the list can change in every situation }
  2302. if FOperator=NOTOKEN then
  2303. begin
  2304. if not objcidcall then
  2305. hashedid.id:=FProcsym.name
  2306. else
  2307. hashedid.id:=class_helper_prefix+FProcsym.name;
  2308. end
  2309. else
  2310. hashedid.id:=overloaded_names[FOperator];
  2311. checkstack:=symtablestack.stack;
  2312. if assigned(FProcsymtable) then
  2313. begin
  2314. while assigned(checkstack) and
  2315. (checkstack^.symtable<>FProcsymtable) do
  2316. checkstack:=checkstack^.next;
  2317. end;
  2318. while assigned(checkstack) do
  2319. begin
  2320. srsymtable:=checkstack^.symtable;
  2321. { if the unit in which the routine has to be searched has been
  2322. specified explicitly, stop searching after its symtable(s) have
  2323. been checked (can be both the static and the global symtable
  2324. in case it's the current unit itself) }
  2325. if explicitunit and
  2326. (FProcsymtable.symtabletype in [globalsymtable,staticsymtable]) and
  2327. (srsymtable.moduleid<>FProcsymtable.moduleid) then
  2328. break;
  2329. if (srsymtable.symtabletype in [localsymtable,staticsymtable,globalsymtable]) and
  2330. (
  2331. (FOperator=NOTOKEN) or
  2332. (sto_has_operator in srsymtable.tableoptions)
  2333. )
  2334. then
  2335. begin
  2336. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  2337. if assigned(srsym) and
  2338. (srsym.typ=procsym) and
  2339. (
  2340. (tprocsym(srsym).procdeflist.count>0) or
  2341. (sp_generic_dummy in srsym.symoptions)
  2342. ) then
  2343. begin
  2344. { add all definitions }
  2345. hasoverload:=false;
  2346. foundanything:=false;
  2347. if tprocsym(srsym).could_be_implicitly_specialized then
  2348. foundanything:=try_implicit_specialization(srsym,FParaNode,ProcdefOverloadList,FParaAnonSyms,tsym(FProcsym),hasoverload);
  2349. for j:=0 to tprocsym(srsym).ProcdefList.Count-1 do
  2350. begin
  2351. pd:=tprocdef(tprocsym(srsym).ProcdefList[j]);
  2352. if not finalize_specialization(pd,spezcontext) then
  2353. continue;
  2354. if (po_ignore_for_overload_resolution in pd.procoptions) then
  2355. begin
  2356. FIgnoredCandidateProcs.add(pd);
  2357. continue;
  2358. end;
  2359. { Store first procsym found }
  2360. if not assigned(FProcsym) then
  2361. FProcsym:=tprocsym(srsym);
  2362. if po_overload in pd.procoptions then
  2363. hasoverload:=true;
  2364. ProcdefOverloadList.Add(pd);
  2365. foundanything:=true;
  2366. end;
  2367. { when there is no explicit overload we stop searching,
  2368. except for Objective-C methods called via id }
  2369. if foundanything and
  2370. not hasoverload and
  2371. not objcidcall then
  2372. break;
  2373. end;
  2374. end;
  2375. checkstack:=checkstack^.next
  2376. end;
  2377. end;
  2378. procedure tcallcandidates.create_candidate_list(ignorevisibility,allowdefaultparas,objcidcall,explicitunit,searchhelpers,anoninherited:boolean;spezcontext:tspecializationcontext);
  2379. var
  2380. j : integer;
  2381. pd : tprocdef;
  2382. hp : pcandidate;
  2383. pt : tcallparanode;
  2384. found,
  2385. added : boolean;
  2386. st : TSymtable;
  2387. contextstructdef : tabstractrecorddef;
  2388. ProcdefOverloadList : TFPObjectList;
  2389. cpoptions : tcompare_paras_options;
  2390. begin
  2391. FCandidateProcs:=nil;
  2392. { Find all available overloads for this procsym }
  2393. ProcdefOverloadList:=TFPObjectList.Create(false);
  2394. if not objcidcall and
  2395. (FOperator=NOTOKEN) and
  2396. (FProcsym.owner.symtabletype in [objectsymtable,recordsymtable]) then
  2397. collect_overloads_in_struct(tabstractrecorddef(FProcsym.owner.defowner),ProcdefOverloadList,searchhelpers,anoninherited,spezcontext)
  2398. else
  2399. if (FOperator<>NOTOKEN) then
  2400. begin
  2401. { check operands and if they contain records then search in records,
  2402. then search in unit }
  2403. pt:=tcallparanode(FParaNode);
  2404. while assigned(pt) do
  2405. begin
  2406. if (pt.resultdef.typ=recorddef) and
  2407. (sto_has_operator in tabstractrecorddef(pt.resultdef).symtable.tableoptions) then
  2408. collect_overloads_in_struct(tabstractrecorddef(pt.resultdef),ProcdefOverloadList,searchhelpers,anoninherited,spezcontext);
  2409. pt:=tcallparanode(pt.right);
  2410. end;
  2411. collect_overloads_in_units(ProcdefOverloadList,objcidcall,explicitunit,spezcontext);
  2412. end
  2413. else
  2414. collect_overloads_in_units(ProcdefOverloadList,objcidcall,explicitunit,spezcontext);
  2415. { determine length of parameter list.
  2416. for operators also enable the variant-operators if
  2417. a variant parameter is passed }
  2418. FParalength:=0;
  2419. FAllowVariant:=(FOperator=NOTOKEN);
  2420. pt:=tcallparanode(FParaNode);
  2421. while assigned(pt) do
  2422. begin
  2423. if (pt.resultdef.typ=variantdef) then
  2424. FAllowVariant:=true;
  2425. inc(FParalength);
  2426. pt:=tcallparanode(pt.right);
  2427. end;
  2428. { when the class passed is defined in this unit we
  2429. need to use the scope of that class. This is a trick
  2430. that can be used to access protected members in other
  2431. units. At least kylix supports it this way (PFV) }
  2432. if assigned(FProcSymtable) and
  2433. (
  2434. (FProcSymtable.symtabletype in [ObjectSymtable,recordsymtable]) or
  2435. ((FProcSymtable.symtabletype=withsymtable) and
  2436. (FProcSymtable.defowner.typ in [objectdef,recorddef]))
  2437. ) and
  2438. (FProcSymtable.defowner.owner.symtabletype in [globalsymtable,staticsymtable,objectsymtable,recordsymtable]) and
  2439. FProcSymtable.defowner.owner.iscurrentunit then
  2440. contextstructdef:=tabstractrecorddef(FProcSymtable.defowner)
  2441. else
  2442. contextstructdef:=current_structdef;
  2443. { symtable is needed later to calculate the distance }
  2444. if assigned(FProcsym) then
  2445. st:=FProcsym.Owner
  2446. else
  2447. st:=nil;
  2448. { Process all found overloads }
  2449. for j:=0 to ProcdefOverloadList.Count-1 do
  2450. begin
  2451. pd:=tprocdef(ProcdefOverloadList[j]);
  2452. added:=false;
  2453. { only when the # of parameter are supported by the procedure and
  2454. it is visible }
  2455. {$ifdef DISABLE_FAST_OVERLOAD_PATCH}
  2456. if (FParalength>=pd.minparacount) and
  2457. {$else}
  2458. if (pd.seenmarker<>pointer(self)) and (FParalength>=pd.minparacount) and
  2459. {$endif}
  2460. (
  2461. (
  2462. allowdefaultparas and
  2463. (
  2464. (FParalength<=pd.maxparacount) or
  2465. (po_varargs in pd.procoptions)
  2466. )
  2467. ) or
  2468. (
  2469. not allowdefaultparas and
  2470. (FParalength=pd.maxparacount)
  2471. )
  2472. ) and
  2473. (
  2474. ignorevisibility or
  2475. (
  2476. pd.is_specialization and not assigned(pd.owner) and
  2477. (
  2478. not (pd.genericdef.owner.symtabletype in [objectsymtable,recordsymtable]) or
  2479. is_visible_for_object(tprocdef(pd.genericdef),contextstructdef)
  2480. )
  2481. ) or
  2482. (
  2483. assigned(pd.owner) and
  2484. (
  2485. not (pd.owner.symtabletype in [objectsymtable,recordsymtable]) or
  2486. is_visible_for_object(pd,contextstructdef)
  2487. )
  2488. )
  2489. ) then
  2490. begin
  2491. { don't add duplicates, only compare visible parameters for the user }
  2492. cpoptions:=[cpo_ignorehidden];
  2493. if (po_compilerproc in pd.procoptions) then
  2494. cpoptions:=cpoptions+[cpo_compilerproc];
  2495. if (po_rtlproc in pd.procoptions) then
  2496. cpoptions:=cpoptions+[cpo_rtlproc];
  2497. found:=false;
  2498. hp:=FCandidateProcs;
  2499. {$ifdef DISABLE_FAST_OVERLOAD_PATCH}
  2500. while assigned(hp) do
  2501. begin
  2502. if (compare_paras(hp^.data.paras,pd.paras,cp_value_equal_const,cpoptions)>=te_equal) and
  2503. (not(po_objc in pd.procoptions) or
  2504. (pd.messageinf.str^=hp^.data.messageinf.str^)) then
  2505. begin
  2506. found:=true;
  2507. break;
  2508. end;
  2509. hp:=hp^.next;
  2510. end;
  2511. {$endif}
  2512. if not found then
  2513. begin
  2514. proc_add(st,pd,objcidcall);
  2515. added:=true;
  2516. {$ifndef DISABLE_FAST_OVERLOAD_PATCH}
  2517. pd.seenmarker:=self;
  2518. {$endif}
  2519. end;
  2520. end;
  2521. { we need to remove all specializations that were not used from their
  2522. procsyms as no code must be generated for them (if they are used
  2523. later on they'll be added like the ones that were used now) }
  2524. if not added and assigned(spezcontext) and not pd.is_registered then
  2525. begin
  2526. if tprocsym(pd.procsym).procdeflist.extract(pd)<>pd then
  2527. internalerror(20150828);
  2528. pd.free;
  2529. end;
  2530. end;
  2531. {$ifndef DISABLE_FAST_OVERLOAD_PATCH}
  2532. {cleanup modified duplicate pd markers}
  2533. hp := FCandidateProcs;
  2534. while assigned(hp) do begin
  2535. hp^.data.seenmarker := nil;
  2536. hp := hp^.next;
  2537. end;
  2538. {$endif}
  2539. calc_distance(st,objcidcall);
  2540. ProcdefOverloadList.Free;
  2541. end;
  2542. procedure tcallcandidates.calc_distance(st_root: tsymtable; objcidcall: boolean);
  2543. var
  2544. pd:tprocdef;
  2545. candidate:pcandidate;
  2546. st: tsymtable;
  2547. begin
  2548. { Give a small penalty for overloaded methods not defined in the
  2549. current class/unit }
  2550. st:=nil;
  2551. if objcidcall or
  2552. not assigned(st_root) or
  2553. not assigned(st_root.defowner) or
  2554. (st_root.defowner.typ<>objectdef) then
  2555. st:=st_root
  2556. else
  2557. repeat
  2558. { In case of a method, st_root is the symtable of the first found
  2559. procsym with the called method's name, but this procsym may not
  2560. contain any of the overloads that match the used parameters (which
  2561. are the procdefs that have been collected as candidates) -> walk
  2562. up the class hierarchy and look for the first class that actually
  2563. defines at least one of the candidate procdefs.
  2564. The reason is that we will penalise methods in other classes/
  2565. symtables, so if we pick a symtable that does not contain any of
  2566. the candidates, this won't help with picking the best/
  2567. most-inner-scoped one (since all of them will be penalised) }
  2568. candidate:=FCandidateProcs;
  2569. { the current class contains one of the candidates? }
  2570. while assigned(candidate) do
  2571. begin
  2572. pd:=candidate^.data;
  2573. if pd.owner=st_root then
  2574. begin
  2575. { yes -> choose this class }
  2576. st:=st_root;
  2577. break;
  2578. end;
  2579. candidate:=candidate^.next;
  2580. end;
  2581. { None found -> go to parent class }
  2582. if not assigned(st) then
  2583. begin
  2584. if not assigned(st_root.defowner) then
  2585. internalerror(201605301);
  2586. { no more parent class -> take current class as root anyway
  2587. (could maybe happen in case of a class helper?) }
  2588. if not assigned(tobjectdef(st_root.defowner).childof) then
  2589. begin
  2590. st:=st_root;
  2591. break;
  2592. end;
  2593. st_root:=tobjectdef(st_root.defowner).childof.symtable;
  2594. end;
  2595. until assigned(st);
  2596. candidate:=FCandidateProcs;
  2597. { when calling Objective-C methods via id.method, then the found
  2598. procsym will be inside an arbitrary ObjectSymtable, and we don't
  2599. want to give the methods of that particular objcclass precedence
  2600. over other methods, so instead check against the symtable in
  2601. which this objcclass is defined }
  2602. if objcidcall then
  2603. st:=st.defowner.owner;
  2604. while assigned(candidate) do
  2605. begin
  2606. pd:=candidate^.data;
  2607. if st<>pd.owner then
  2608. candidate^.ordinal_distance:=candidate^.ordinal_distance+1.0;
  2609. candidate:=candidate^.next;
  2610. end;
  2611. end;
  2612. function tcallcandidates.proc_add(st:tsymtable;pd:tprocdef;objcidcall: boolean):pcandidate;
  2613. var
  2614. defaultparacnt : integer;
  2615. begin
  2616. { generate new candidate entry }
  2617. new(result);
  2618. fillchar(result^,sizeof(tcandidate),0);
  2619. result^.data:=pd;
  2620. result^.next:=FCandidateProcs;
  2621. FCandidateProcs:=result;
  2622. inc(FProccnt);
  2623. { Find last parameter, skip all default parameters
  2624. that are not passed. Ignore this skipping for varargs }
  2625. result^.firstparaidx:=pd.paras.count-1;
  2626. if not(po_varargs in pd.procoptions) then
  2627. begin
  2628. { ignore hidden parameters }
  2629. while (result^.firstparaidx>=0) and (vo_is_hidden_para in tparavarsym(pd.paras[result^.firstparaidx]).varoptions) do
  2630. dec(result^.firstparaidx);
  2631. defaultparacnt:=pd.maxparacount-FParalength;
  2632. if defaultparacnt>0 then
  2633. begin
  2634. if defaultparacnt>result^.firstparaidx+1 then
  2635. internalerror(200401141);
  2636. dec(result^.firstparaidx,defaultparacnt);
  2637. end;
  2638. end;
  2639. end;
  2640. procedure tcallcandidates.list(all:boolean);
  2641. var
  2642. hp : pcandidate;
  2643. begin
  2644. hp:=FCandidateProcs;
  2645. while assigned(hp) do
  2646. begin
  2647. if all or
  2648. (not hp^.invalid) then
  2649. MessagePos1(hp^.data.fileinfo,sym_h_param_list,hp^.data.fullprocname(false));
  2650. hp:=hp^.next;
  2651. end;
  2652. end;
  2653. {$ifdef EXTDEBUG}
  2654. procedure tcallcandidates.dump_info(lvl:longint);
  2655. function ParaTreeStr(p:tcallparanode):string;
  2656. begin
  2657. result:='';
  2658. while assigned(p) do
  2659. begin
  2660. if result<>'' then
  2661. result:=','+result;
  2662. result:=p.resultdef.typename+result;
  2663. p:=tcallparanode(p.right);
  2664. end;
  2665. end;
  2666. var
  2667. hp : pcandidate;
  2668. i : integer;
  2669. currpara : tparavarsym;
  2670. begin
  2671. if not CheckVerbosity(lvl) then
  2672. exit;
  2673. Comment(lvl+V_LineInfo,'Overloaded callnode: '+FProcsym.name+'('+ParaTreeStr(tcallparanode(FParaNode))+')');
  2674. hp:=FCandidateProcs;
  2675. while assigned(hp) do
  2676. begin
  2677. Comment(lvl,' '+hp^.data.fullprocname(false));
  2678. if (hp^.invalid) then
  2679. Comment(lvl,' invalid')
  2680. else
  2681. begin
  2682. Comment(lvl,' ex: '+tostr(hp^.exact_count)+
  2683. ' eq: '+tostr(hp^.equal_count)+
  2684. ' l1: '+tostr(hp^.cl1_count)+
  2685. ' l2: '+tostr(hp^.cl2_count)+
  2686. ' l3: '+tostr(hp^.cl3_count)+
  2687. ' l4: '+tostr(hp^.cl4_count)+
  2688. ' l5: '+tostr(hp^.cl5_count)+
  2689. ' l6: '+tostr(hp^.cl6_count)+
  2690. ' oper: '+tostr(hp^.coper_count)+
  2691. ' ord: '+realtostr(hp^.ordinal_distance));
  2692. { Print parameters in left-right order }
  2693. for i:=0 to hp^.data.paras.count-1 do
  2694. begin
  2695. currpara:=tparavarsym(hp^.data.paras[i]);
  2696. if not(vo_is_hidden_para in currpara.varoptions) then
  2697. Comment(lvl,' - '+currpara.vardef.typename+' : '+EqualTypeName[currpara.eqval]);
  2698. end;
  2699. end;
  2700. hp:=hp^.next;
  2701. end;
  2702. end;
  2703. {$endif EXTDEBUG}
  2704. procedure tcallcandidates.get_information;
  2705. var
  2706. hp : pcandidate;
  2707. currpara : tparavarsym;
  2708. paraidx : integer;
  2709. currparanr : byte;
  2710. rfh,rth : double;
  2711. obj_from,
  2712. obj_to : tobjectdef;
  2713. def_from,
  2714. def_to : tdef;
  2715. currpt,
  2716. pt : tcallparanode;
  2717. eq,
  2718. mineq : tequaltype;
  2719. convtype : tconverttype;
  2720. pdtemp,
  2721. pdoper : tprocdef;
  2722. releasecurrpt : boolean;
  2723. cdoptions : tcompare_defs_options;
  2724. n : tnode;
  2725. {$push}
  2726. {$r-}
  2727. {$q-}
  2728. const
  2729. inf=1.0/0.0;
  2730. {$pop}
  2731. begin
  2732. cdoptions:=[cdo_check_operator];
  2733. if FAllowVariant then
  2734. include(cdoptions,cdo_allow_variant);
  2735. { process all procs }
  2736. hp:=FCandidateProcs;
  2737. while assigned(hp) do
  2738. begin
  2739. { We compare parameters in reverse order (right to left),
  2740. the firstpara is already pointing to the last parameter
  2741. were we need to start comparing }
  2742. currparanr:=FParalength;
  2743. paraidx:=hp^.firstparaidx;
  2744. while (paraidx>=0) and (vo_is_hidden_para in tparavarsym(hp^.data.paras[paraidx]).varoptions) do
  2745. dec(paraidx);
  2746. pt:=tcallparanode(FParaNode);
  2747. while assigned(pt) and (paraidx>=0) do
  2748. begin
  2749. currpara:=tparavarsym(hp^.data.paras[paraidx]);
  2750. { currpt can be changed from loadn to calln when a procvar
  2751. is passed. This is to prevent that the change is permanent }
  2752. currpt:=pt;
  2753. releasecurrpt:=false;
  2754. { retrieve current parameter definitions to compares }
  2755. eq:=te_incompatible;
  2756. def_from:=currpt.resultdef;
  2757. def_to:=currpara.vardef;
  2758. if not(assigned(def_from)) then
  2759. internalerror(200212091);
  2760. if not(
  2761. assigned(def_to) or
  2762. ((po_varargs in hp^.data.procoptions) and
  2763. (currparanr>hp^.data.minparacount))
  2764. ) then
  2765. internalerror(200212092);
  2766. { Convert tp procvars when not expecting a procvar }
  2767. if (currpt.left.resultdef.typ=procvardef) and
  2768. not(def_to.typ in [procvardef,formaldef]) and
  2769. { Only convert to call when there is no overload or the return type
  2770. is equal to the expected type. }
  2771. (
  2772. (count=1) or
  2773. equal_defs(tprocvardef(currpt.left.resultdef).returndef,def_to)
  2774. ) and
  2775. { and if it doesn't require any parameters }
  2776. (tprocvardef(currpt.left.resultdef).minparacount=0) then
  2777. begin
  2778. releasecurrpt:=true;
  2779. currpt:=tcallparanode(pt.getcopy);
  2780. if maybe_call_procvar(currpt.left,true) then
  2781. begin
  2782. currpt.resultdef:=currpt.left.resultdef;
  2783. def_from:=currpt.left.resultdef;
  2784. end;
  2785. end;
  2786. { If we expect a procvar and the left is loadnode that
  2787. returns a procdef we need to find the correct overloaded
  2788. procdef that matches the expected procvar. The loadnode
  2789. temporary returned the first procdef (PFV) }
  2790. if (def_to.typ=procvardef) and
  2791. (currpt.left.nodetype=loadn) and
  2792. (currpt.left.resultdef.typ=procdef) then
  2793. begin
  2794. pdtemp:=tprocsym(Tloadnode(currpt.left).symtableentry).Find_procdef_byprocvardef(Tprocvardef(def_to));
  2795. if assigned(pdtemp) then
  2796. begin
  2797. tloadnode(currpt.left).setprocdef(pdtemp);
  2798. currpt.resultdef:=currpt.left.resultdef;
  2799. def_from:=currpt.left.resultdef;
  2800. end;
  2801. end;
  2802. { varargs are always equal, but not exact }
  2803. if (po_varargs in hp^.data.procoptions) and
  2804. (currparanr>hp^.data.minparacount) and
  2805. not is_array_of_const(def_from) and
  2806. not is_array_constructor(def_from) then
  2807. eq:=te_equal
  2808. else
  2809. { same definition -> exact }
  2810. if (def_from=def_to) then
  2811. eq:=te_exact
  2812. else
  2813. { for value and const parameters check if a integer is constant or
  2814. included in other integer -> equal and calc ordinal_distance }
  2815. if not(currpara.varspez in [vs_var,vs_out]) and
  2816. is_integer(def_from) and
  2817. is_integer(def_to) and
  2818. is_in_limit(def_from,def_to) then
  2819. begin
  2820. eq:=te_equal;
  2821. hp^.ordinal_distance:=hp^.ordinal_distance+
  2822. abs(bestreal(torddef(def_from).low)-bestreal(torddef(def_to).low));
  2823. rth:=bestreal(torddef(def_to).high);
  2824. rfh:=bestreal(torddef(def_from).high);
  2825. hp^.ordinal_distance:=hp^.ordinal_distance+abs(rth-rfh);
  2826. { Give wrong sign a small penalty, this is need to get a diffrence
  2827. from word->[longword,longint] }
  2828. if is_signed(def_from)<>is_signed(def_to) then
  2829. {$push}
  2830. {$r-}
  2831. {$q-}
  2832. hp^.ordinal_distance:=nextafter(hp^.ordinal_distance,inf);
  2833. {$pop}
  2834. end
  2835. else
  2836. { for value and const parameters check precision of real, give
  2837. penalty for loosing of precision. var and out parameters must match exactly }
  2838. if not(currpara.varspez in [vs_var,vs_out]) and
  2839. is_real_or_cextended(def_from) and
  2840. is_real_or_cextended(def_to) then
  2841. begin
  2842. eq:=te_equal;
  2843. if is_extended(def_to) then
  2844. rth:=4
  2845. else
  2846. if is_double (def_to) then
  2847. rth:=2
  2848. else
  2849. rth:=1;
  2850. if is_extended(def_from) then
  2851. rfh:=4
  2852. else
  2853. if is_double (def_from) then
  2854. rfh:=2
  2855. else
  2856. rfh:=1;
  2857. { penalty for shrinking of precision }
  2858. if rth<rfh then
  2859. rfh:=(rfh-rth)*16
  2860. else
  2861. rfh:=rth-rfh;
  2862. hp^.ordinal_distance:=hp^.ordinal_distance+rfh;
  2863. end
  2864. else
  2865. { related object parameters also need to determine the distance between the current
  2866. object and the object we are comparing with. var and out parameters must match exactly }
  2867. if not(currpara.varspez in [vs_var,vs_out]) and
  2868. (def_from.typ=objectdef) and
  2869. (def_to.typ=objectdef) and
  2870. (tobjectdef(def_from).objecttype=tobjectdef(def_to).objecttype) and
  2871. def_is_related(tobjectdef(def_from),tobjectdef(def_to)) then
  2872. begin
  2873. eq:=te_convert_l1;
  2874. { resolve anonymous external class definitions }
  2875. obj_from:=find_real_class_definition(tobjectdef(def_from),false);
  2876. obj_to:=find_real_class_definition(tobjectdef(def_to),false);
  2877. while assigned(obj_from) do
  2878. begin
  2879. if obj_from=obj_to then
  2880. break;
  2881. hp^.ordinal_distance:=hp^.ordinal_distance+1;
  2882. obj_from:=obj_from.childof;
  2883. end;
  2884. end
  2885. { compare_defs_ext compares sets and array constructors very poorly because
  2886. it has too little information. So we do explicitly a detailed comparisation,
  2887. see also bug #11288 (FK)
  2888. }
  2889. else if (def_to.typ=setdef) and is_array_constructor(currpt.left.resultdef) then
  2890. begin
  2891. n:=currpt.left.getcopy;
  2892. arrayconstructor_to_set(n);
  2893. eq:=compare_defs_ext(n.resultdef,def_to,n.nodetype,convtype,pdoper,cdoptions);
  2894. n.free;
  2895. end
  2896. else if is_open_array(def_to) and
  2897. is_class_or_interface_or_dispinterface_or_objc_or_java(tarraydef(def_to).elementdef) and
  2898. is_array_constructor(currpt.left.resultdef) and
  2899. assigned(tarrayconstructornode(currpt.left).left) then
  2900. begin
  2901. { ensure that [nil] can be converted to "array of tobject",
  2902. because if we just try to convert "array of pointer" to
  2903. "array of tobject", we get type conversion errors in
  2904. non-Delphi modes }
  2905. n:=currpt.left;
  2906. mineq:=te_exact;
  2907. repeat
  2908. if tarrayconstructornode(n).left.nodetype=arrayconstructorrangen then
  2909. eq:=te_incompatible
  2910. else
  2911. eq:=compare_defs_ext(tarrayconstructornode(n).left.resultdef,tarraydef(def_to).elementdef,tarrayconstructornode(n).left.nodetype,convtype,pdoper,cdoptions);
  2912. if eq<mineq then
  2913. mineq:=eq;
  2914. if eq=te_incompatible then
  2915. break;
  2916. n:=tarrayconstructornode(n).right;
  2917. until not assigned(n);
  2918. eq:=mineq;
  2919. end
  2920. else
  2921. { generic type comparision }
  2922. begin
  2923. if (hp^.data.procoptions*[po_rtlproc,po_compilerproc]=[]) and
  2924. is_ansistring(def_from) and
  2925. is_ansistring(def_to) and
  2926. (tstringdef(def_from).encoding<>tstringdef(def_to).encoding) and
  2927. (currpara.varspez in [vs_var,vs_out]) then
  2928. eq:=te_convert_l1 // don't allow to pass different ansistring types to each-other
  2929. else
  2930. eq:=compare_defs_ext(def_from,def_to,currpt.left.nodetype,convtype,pdoper,cdoptions);
  2931. { when the types are not equal we need to check
  2932. some special case for parameter passing }
  2933. if (eq<te_equal) then
  2934. begin
  2935. if currpara.varspez in [vs_var,vs_out] then
  2936. begin
  2937. { para requires an equal type so the previous found
  2938. match was not good enough, reset to incompatible }
  2939. eq:=te_incompatible;
  2940. { var_para_allowed will return te_equal and te_convert_l1 to
  2941. make a difference for best matching }
  2942. var_para_allowed(eq,currpt.resultdef,currpara.vardef,currpt.left)
  2943. end
  2944. else
  2945. para_allowed(eq,currpt,def_to);
  2946. end;
  2947. end;
  2948. { univ parameters match if the size matches (don't override the
  2949. comparison result if it was ok, since a match based on the
  2950. "univ" character is the lowest possible match) }
  2951. if (eq=te_incompatible) and
  2952. currpara.univpara and
  2953. is_valid_univ_para_type(def_from) and
  2954. (def_from.size=def_to.size) then
  2955. eq:=te_convert_l5;
  2956. { when a procvar was changed to a call an exact match is
  2957. downgraded to equal. This way an overload call with the
  2958. procvar is choosen. See tb0471 (PFV) }
  2959. if (pt<>currpt) and (eq=te_exact) then
  2960. eq:=te_equal;
  2961. { increase correct counter }
  2962. case eq of
  2963. te_exact :
  2964. inc(hp^.exact_count);
  2965. te_equal :
  2966. inc(hp^.equal_count);
  2967. te_convert_l1 :
  2968. inc(hp^.cl1_count);
  2969. te_convert_l2 :
  2970. inc(hp^.cl2_count);
  2971. te_convert_l3 :
  2972. inc(hp^.cl3_count);
  2973. te_convert_l4 :
  2974. inc(hp^.cl4_count);
  2975. te_convert_l5 :
  2976. inc(hp^.cl5_count);
  2977. te_convert_l6 :
  2978. inc(hp^.cl6_count);
  2979. te_convert_operator :
  2980. inc(hp^.coper_count);
  2981. te_incompatible :
  2982. hp^.invalid:=true;
  2983. end;
  2984. { stop checking when an incompatible parameter is found }
  2985. if hp^.invalid then
  2986. begin
  2987. { store the current parameter info for
  2988. a nice error message when no procedure is found }
  2989. hp^.wrongparaidx:=paraidx;
  2990. hp^.wrongparanr:=currparanr;
  2991. break;
  2992. end;
  2993. {$ifdef EXTDEBUG}
  2994. { store equal in node tree for dump }
  2995. currpara.eqval:=eq;
  2996. {$endif EXTDEBUG}
  2997. { maybe release temp currpt }
  2998. if releasecurrpt then
  2999. currpt.free;
  3000. { next parameter in the call tree }
  3001. pt:=tcallparanode(pt.right);
  3002. { next parameter for definition, only goto next para
  3003. if we're out of the varargs }
  3004. if not(po_varargs in hp^.data.procoptions) or
  3005. (currparanr<=hp^.data.maxparacount) then
  3006. begin
  3007. { Ignore vs_hidden parameters }
  3008. repeat
  3009. dec(paraidx);
  3010. until (paraidx<0) or not(vo_is_hidden_para in tparavarsym(hp^.data.paras[paraidx]).varoptions);
  3011. end;
  3012. dec(currparanr);
  3013. end;
  3014. if not(hp^.invalid) and
  3015. (assigned(pt) or (paraidx>=0) or (currparanr<>0)) then
  3016. internalerror(200212141);
  3017. { next candidate }
  3018. hp:=hp^.next;
  3019. end;
  3020. end;
  3021. function get_variantequaltype(def: tdef): tvariantequaltype;
  3022. const
  3023. variantorddef_cl: array[tordtype] of tvariantequaltype =
  3024. (tve_incompatible,tve_byte,tve_word,tve_cardinal,tve_chari64,tve_incompatible,
  3025. tve_shortint,tve_smallint,tve_longint,tve_chari64,tve_incompatible,
  3026. tve_boolformal,tve_boolformal,tve_boolformal,tve_boolformal,tve_boolformal,
  3027. tve_boolformal,tve_boolformal,tve_boolformal,tve_boolformal,
  3028. tve_chari64,tve_chari64,tve_dblcurrency,tve_incompatible);
  3029. { TODO: fixme for 128 bit floats }
  3030. variantfloatdef_cl: array[tfloattype] of tvariantequaltype =
  3031. (tve_single,tve_dblcurrency,tve_extended,tve_extended,
  3032. tve_dblcurrency,tve_dblcurrency,tve_extended);
  3033. variantstringdef_cl: array[tstringtype] of tvariantequaltype =
  3034. (tve_sstring,tve_astring,tve_astring,tve_wstring,tve_ustring);
  3035. begin
  3036. case def.typ of
  3037. orddef:
  3038. begin
  3039. result:=variantorddef_cl[torddef(def).ordtype];
  3040. end;
  3041. floatdef:
  3042. begin
  3043. result:=variantfloatdef_cl[tfloatdef(def).floattype];
  3044. end;
  3045. stringdef:
  3046. begin
  3047. result:=variantstringdef_cl[tstringdef(def).stringtype];
  3048. end;
  3049. formaldef:
  3050. begin
  3051. result:=tve_boolformal;
  3052. end;
  3053. else
  3054. begin
  3055. result:=tve_incompatible;
  3056. end;
  3057. end
  3058. end;
  3059. function is_better_candidate(currpd,bestpd:pcandidate):integer;
  3060. var
  3061. res : integer;
  3062. begin
  3063. {
  3064. Return values:
  3065. > 0 when currpd is better than bestpd
  3066. < 0 when bestpd is better than currpd
  3067. = 0 when both are equal
  3068. To choose the best candidate we use the following order:
  3069. - Incompatible flag
  3070. - (Smaller) Number of convert operator parameters.
  3071. - (Smaller) Number of convertlevel 2 parameters.
  3072. - (Smaller) Number of convertlevel 1 parameters.
  3073. - (Bigger) Number of exact parameters.
  3074. - (Smaller) Number of equal parameters.
  3075. - (Smaller) Total of ordinal distance. For example, the distance of a word
  3076. to a byte is 65535-255=65280.
  3077. }
  3078. if bestpd^.invalid then
  3079. begin
  3080. if currpd^.invalid then
  3081. res:=0
  3082. else
  3083. res:=1;
  3084. end
  3085. else
  3086. if currpd^.invalid then
  3087. res:=-1
  3088. else
  3089. begin
  3090. { less operator parameters? }
  3091. res:=(bestpd^.coper_count-currpd^.coper_count);
  3092. if (res=0) then
  3093. begin
  3094. { less cl6 parameters? }
  3095. res:=(bestpd^.cl6_count-currpd^.cl6_count);
  3096. if (res=0) then
  3097. begin
  3098. { less cl5 parameters? }
  3099. res:=(bestpd^.cl5_count-currpd^.cl5_count);
  3100. if (res=0) then
  3101. begin
  3102. { less cl4 parameters? }
  3103. res:=(bestpd^.cl4_count-currpd^.cl4_count);
  3104. if (res=0) then
  3105. begin
  3106. { less cl3 parameters? }
  3107. res:=(bestpd^.cl3_count-currpd^.cl3_count);
  3108. if (res=0) then
  3109. begin
  3110. { less cl2 parameters? }
  3111. res:=(bestpd^.cl2_count-currpd^.cl2_count);
  3112. if (res=0) then
  3113. begin
  3114. { less cl1 parameters? }
  3115. res:=(bestpd^.cl1_count-currpd^.cl1_count);
  3116. if (res=0) then
  3117. begin
  3118. { more exact parameters? }
  3119. res:=(currpd^.exact_count-bestpd^.exact_count);
  3120. if (res=0) then
  3121. begin
  3122. { less equal parameters? }
  3123. res:=(bestpd^.equal_count-currpd^.equal_count);
  3124. if (res=0) then
  3125. begin
  3126. { smaller ordinal distance? }
  3127. if (currpd^.ordinal_distance<bestpd^.ordinal_distance) then
  3128. res:=1
  3129. else
  3130. if (currpd^.ordinal_distance>bestpd^.ordinal_distance) then
  3131. res:=-1
  3132. else
  3133. res:=0;
  3134. { if a specialization is better than a non-specialization then
  3135. the non-generic always wins }
  3136. if m_implicit_function_specialization in current_settings.modeswitches then
  3137. begin
  3138. if (currpd^.data.is_specialization and not bestpd^.data.is_specialization) then
  3139. res:=-1
  3140. else if (not currpd^.data.is_specialization and bestpd^.data.is_specialization) then
  3141. res:=1;
  3142. end;
  3143. end;
  3144. end;
  3145. end;
  3146. end;
  3147. end;
  3148. end;
  3149. end;
  3150. end;
  3151. end;
  3152. end;
  3153. is_better_candidate:=res;
  3154. end;
  3155. { Delphi precedence rules extracted from test programs. Only valid if passing
  3156. a variant parameter to overloaded procedures expecting exactly one parameter.
  3157. single > (char, currency, int64, shortstring, ansistring, widestring, unicodestring, extended, double)
  3158. double/currency > (char, int64, shortstring, ansistring, widestring, unicodestring, extended)
  3159. extended > (char, int64, shortstring, ansistring, widestring, unicodestring)
  3160. longint/cardinal > (int64, shortstring, ansistring, widestring, unicodestring, extended, double, single, char, currency)
  3161. smallint > (longint, int64, shortstring, ansistring, widestring, unicodestring, extended, double single, char, currency);
  3162. word > (longint, cardinal, int64, shortstring, ansistring, widestring, unicodestring, extended, double single, char, currency);
  3163. shortint > (longint, smallint, int64, shortstring, ansistring, widestring, unicodestring, extended, double, single, char, currency)
  3164. byte > (longint, cardinal, word, smallint, int64, shortstring, ansistring, widestring, unicodestring, extended, double, single, char, currency);
  3165. boolean/formal > (char, int64, shortstring, ansistring, widestring, unicodestring)
  3166. widestring > (char, int64, shortstring, ansistring, unicodestring)
  3167. unicodestring > (char, int64, shortstring, ansistring)
  3168. ansistring > (char, int64, shortstring)
  3169. shortstring > (char, int64)
  3170. Relations not mentioned mean that they conflict: no decision possible }
  3171. function is_better_candidate_single_variant(currpd,bestpd:pcandidate):integer;
  3172. function calculate_relation(const currvcl, bestvcl, testvcl:
  3173. tvariantequaltype; const conflictvcls: tvariantequaltypes):integer;
  3174. begin
  3175. { if (bestvcl=conflictvcl) or
  3176. (currvcl=conflictvcl) then
  3177. result:=0
  3178. else if (bestvcl=testvcl) then
  3179. result:=-1
  3180. else result:=1 }
  3181. result:=1-2*ord(bestvcl=testvcl)+
  3182. ord(currvcl in conflictvcls)-ord(bestvcl in conflictvcls);
  3183. end;
  3184. function getfirstrealparaidx(pd: pcandidate): integer;
  3185. begin
  3186. { can be different for currpd and bestpd in case of overloaded }
  3187. { functions, e.g. lowercase():char and lowercase():shortstring }
  3188. { (depending on the calling convention and parameter order) }
  3189. result:=pd^.firstparaidx;
  3190. while (result>=0) and (vo_is_hidden_para in tparavarsym(pd^.data.paras[result]).varoptions) do
  3191. dec(result);
  3192. if (vo_is_hidden_para in tparavarsym(pd^.data.paras[result]).varoptions) then
  3193. internalerror(2006122803);
  3194. end;
  3195. var
  3196. currpara, bestpara: tparavarsym;
  3197. currvcl, bestvcl: tvariantequaltype;
  3198. begin
  3199. {
  3200. Return values:
  3201. > 0 when currpd is better than bestpd
  3202. < 0 when bestpd is better than currpd
  3203. = 0 when both are equal
  3204. }
  3205. currpara:=tparavarsym(currpd^.data.paras[getfirstrealparaidx(currpd)]);
  3206. bestpara:=tparavarsym(bestpd^.data.paras[getfirstrealparaidx(bestpd)]);
  3207. { if one of the parameters is a regular variant, fall back to the }
  3208. { default algorithm }
  3209. if (currpara.vardef.typ = variantdef) or
  3210. (bestpara.vardef.typ = variantdef) then
  3211. begin
  3212. result:=is_better_candidate(currpd,bestpd);
  3213. exit;
  3214. end;
  3215. currvcl:=get_variantequaltype(currpara.vardef);
  3216. bestvcl:=get_variantequaltype(bestpara.vardef);
  3217. { sanity check }
  3218. result:=-5;
  3219. { if both are the same, there is a conflict }
  3220. if (currvcl=bestvcl) then
  3221. result:=0
  3222. { if one of the two cannot be used as variant, the other is better }
  3223. else if (bestvcl=tve_incompatible) then
  3224. result:=1
  3225. else if (currvcl=tve_incompatible) then
  3226. result:=-1
  3227. { boolean and formal are better than chari64str, but conflict with }
  3228. { everything else }
  3229. else if (currvcl=tve_boolformal) or
  3230. (bestvcl=tve_boolformal) then
  3231. if (currvcl=tve_boolformal) then
  3232. result:=ord(bestvcl in [tve_chari64,tve_sstring,tve_astring,tve_wstring,tve_ustring])
  3233. else
  3234. result:=-ord(currvcl in [tve_chari64,tve_sstring,tve_astring,tve_wstring,tve_ustring])
  3235. { byte is better than everything else (we assume both aren't byte, }
  3236. { since there's only one parameter and that one can't be the same) }
  3237. else if (currvcl=tve_byte) or
  3238. (bestvcl=tve_byte) then
  3239. result:=calculate_relation(currvcl,bestvcl,tve_byte,[tve_shortint])
  3240. { shortint conflicts with word and cardinal, but is better than }
  3241. { everything else but byte (which has already been handled) }
  3242. else if (currvcl=tve_shortint) or
  3243. (bestvcl=tve_shortint) then
  3244. result:=calculate_relation(currvcl,bestvcl,tve_shortint,[tve_word, tve_cardinal])
  3245. { word conflicts with smallint, but is better than everything else }
  3246. { but shortint and byte (which has already been handled) }
  3247. else if (currvcl=tve_word) or
  3248. (bestvcl=tve_word) then
  3249. result:=calculate_relation(currvcl,bestvcl,tve_word,[tve_smallint])
  3250. { smallint conflicts with cardinal, but is better than everything }
  3251. { which has not yet been tested }
  3252. else if (currvcl=tve_smallint) or
  3253. (bestvcl=tve_smallint) then
  3254. result:=calculate_relation(currvcl,bestvcl,tve_smallint,[tve_cardinal])
  3255. { cardinal conflicts with each longint and is better than everything }
  3256. { which has not yet been tested }
  3257. else if (currvcl=tve_cardinal) or
  3258. (bestvcl=tve_cardinal) then
  3259. result:=calculate_relation(currvcl,bestvcl,tve_cardinal,[tve_longint])
  3260. { longint is better than everything which has not yet been tested }
  3261. else if (currvcl=tve_longint) or
  3262. (bestvcl=tve_longint) then
  3263. { if bestvcl=tve_longint then
  3264. result:=-1
  3265. else
  3266. result:=1 }
  3267. result:=1-2*ord(bestvcl=tve_longint)
  3268. { single is better than everything left }
  3269. else if (currvcl=tve_single) or
  3270. (bestvcl=tve_single) then
  3271. result:=1-2*ord(bestvcl=tve_single)
  3272. { double/comp/currency are better than everything left, and conflict }
  3273. { with each other (but that's already tested) }
  3274. else if (currvcl=tve_dblcurrency) or
  3275. (bestvcl=tve_dblcurrency) then
  3276. result:=1-2*ord(bestvcl=tve_dblcurrency)
  3277. { extended is better than everything left }
  3278. else if (currvcl=tve_extended) or
  3279. (bestvcl=tve_extended) then
  3280. result:=1-2*ord(bestvcl=tve_extended)
  3281. { widestring is better than everything left }
  3282. else if (currvcl=tve_wstring) or
  3283. (bestvcl=tve_wstring) then
  3284. result:=1-2*ord(bestvcl=tve_wstring)
  3285. { unicodestring is better than everything left }
  3286. else if (currvcl=tve_ustring) or
  3287. (bestvcl=tve_ustring) then
  3288. result:=1-2*ord(bestvcl=tve_ustring)
  3289. { ansistring is better than everything left }
  3290. else if (currvcl=tve_astring) or
  3291. (bestvcl=tve_astring) then
  3292. result:=1-2*ord(bestvcl=tve_astring)
  3293. { shortstring is better than everything left }
  3294. else if (currvcl=tve_sstring) or
  3295. (bestvcl=tve_sstring) then
  3296. result:=1-2*ord(bestvcl=tve_sstring);
  3297. { all possibilities should have been checked now }
  3298. if (result=-5) then
  3299. internalerror(2006122805);
  3300. end;
  3301. {$ifdef DISABLE_FAST_OVERLOAD_PATCH}
  3302. function tcallcandidates.choose_best(var bestpd:tabstractprocdef; singlevariant: boolean):integer;
  3303. var
  3304. pd: tprocdef;
  3305. besthpstart,
  3306. hp : pcandidate;
  3307. cntpd,
  3308. res : integer;
  3309. begin
  3310. {
  3311. Returns the number of candidates left and the
  3312. first candidate is returned in pdbest
  3313. }
  3314. { Setup the first procdef as best, only count it as a result
  3315. when it is valid }
  3316. bestpd:=FCandidateProcs^.data;
  3317. if FCandidateProcs^.invalid then
  3318. cntpd:=0
  3319. else
  3320. cntpd:=1;
  3321. if assigned(FCandidateProcs^.next) then
  3322. begin
  3323. besthpstart:=FCandidateProcs;
  3324. hp:=FCandidateProcs^.next;
  3325. while assigned(hp) do
  3326. begin
  3327. if not singlevariant then
  3328. res:=is_better_candidate(hp,besthpstart)
  3329. else
  3330. res:=is_better_candidate_single_variant(hp,besthpstart);
  3331. if (res>0) then
  3332. begin
  3333. { hp is better, flag all procs to be incompatible }
  3334. while (besthpstart<>hp) do
  3335. begin
  3336. besthpstart^.invalid:=true;
  3337. besthpstart:=besthpstart^.next;
  3338. end;
  3339. { besthpstart is already set to hp }
  3340. bestpd:=besthpstart^.data;
  3341. cntpd:=1;
  3342. end
  3343. else
  3344. if (res<0) then
  3345. begin
  3346. { besthpstart is better, flag current hp to be incompatible }
  3347. hp^.invalid:=true;
  3348. end
  3349. else
  3350. begin
  3351. { res=0, both are valid }
  3352. if not hp^.invalid then
  3353. inc(cntpd);
  3354. end;
  3355. hp:=hp^.next;
  3356. end;
  3357. end;
  3358. { if we've found one, check the procdefs ignored for overload choosing
  3359. to see whether they contain one from a child class with the same
  3360. parameters (so the overload choosing was not influenced by their
  3361. presence, but now that we've decided which overloaded version to call,
  3362. make sure we call the version closest in terms of visibility }
  3363. if cntpd=1 then
  3364. begin
  3365. for res:=0 to FIgnoredCandidateProcs.count-1 do
  3366. begin
  3367. pd:=tprocdef(FIgnoredCandidateProcs[res]);
  3368. { stop searching when we start comparing methods of parent of
  3369. the struct in which the current best method was found }
  3370. if assigned(pd.struct) and
  3371. (pd.struct<>tprocdef(bestpd).struct) and
  3372. def_is_related(tprocdef(bestpd).struct,pd.struct) then
  3373. break;
  3374. if (pd.proctypeoption=bestpd.proctypeoption) and
  3375. ((pd.procoptions*[po_classmethod,po_methodpointer])=(bestpd.procoptions*[po_classmethod,po_methodpointer])) and
  3376. (compare_paras(pd.paras,bestpd.paras,cp_all,[cpo_ignorehidden,cpo_ignoreuniv,cpo_openequalisexact])=te_exact) then
  3377. begin
  3378. { first one encountered is closest in terms of visibility }
  3379. bestpd:=pd;
  3380. break;
  3381. end;
  3382. end;
  3383. end;
  3384. result:=cntpd;
  3385. end;
  3386. {$else}
  3387. function compare_by_old_sortout_check(pd,bestpd:pcandidate):integer;
  3388. var cpoptions : tcompare_paras_options;
  3389. begin
  3390. { don't add duplicates, only compare visible parameters for the user }
  3391. cpoptions:=[cpo_ignorehidden];
  3392. if (po_compilerproc in bestpd^.data.procoptions) then
  3393. cpoptions:=cpoptions+[cpo_compilerproc];
  3394. if (po_rtlproc in bestpd^.data.procoptions) then
  3395. cpoptions:=cpoptions+[cpo_rtlproc];
  3396. compare_by_old_sortout_check := 0; // can't decide, bestpd probably wasn't sorted out in unpatched
  3397. if (compare_paras(pd^.data.paras,bestpd^.data.paras,cp_value_equal_const,cpoptions)>=te_equal) and
  3398. (not(po_objc in bestpd^.data.procoptions) or (bestpd^.data.messageinf.str^=pd^.data.messageinf.str^)) then
  3399. compare_by_old_sortout_check := 1; // bestpd was sorted out before patch
  3400. { for implicit specializations non-generics should take precedence so
  3401. when comparing a specialization to a non-specialization mark as undecided
  3402. and it will be re-evaluated in is_better_candidate }
  3403. if (m_implicit_function_specialization in current_settings.modeswitches)
  3404. and (pd^.data.is_specialization <> bestpd^.data.is_specialization) then
  3405. compare_by_old_sortout_check:=0;
  3406. end;
  3407. function decide_restart(pd,bestpd:pcandidate) : boolean;
  3408. begin
  3409. decide_restart := false;
  3410. if assigned(bestpd) then
  3411. begin
  3412. { don't restart if bestpd is marked invalid already }
  3413. if not bestpd^.invalid then
  3414. decide_restart := compare_by_old_sortout_check(pd,bestpd)<>0;
  3415. end;
  3416. end;
  3417. procedure save_validity(c : pcandidate);
  3418. begin
  3419. while assigned(c) do
  3420. begin
  3421. c^.saved_validity := c^.invalid;
  3422. c := c^.next;
  3423. end;
  3424. end;
  3425. procedure restore_validity(c : pcandidate);
  3426. begin
  3427. while assigned(c) do begin
  3428. c^.invalid := c^.saved_validity;
  3429. c := c^.next;
  3430. end;
  3431. end;
  3432. function tcallcandidates.choose_best(var bestpd:tabstractprocdef; singlevariant: boolean):integer;
  3433. var
  3434. pd: tprocdef;
  3435. besthpstart,
  3436. hp,hp2 : pcandidate;
  3437. cntpd,
  3438. res : integer;
  3439. restart : boolean;
  3440. begin
  3441. res:=0;
  3442. {
  3443. Returns the number of candidates left and the
  3444. first candidate is returned in pdbest
  3445. }
  3446. if not(assigned(FCandidateProcs)) then
  3447. begin
  3448. choose_best := 0;
  3449. exit;
  3450. end;
  3451. bestpd:=FCandidateProcs^.data;
  3452. if FCandidateProcs^.invalid then
  3453. cntpd:=0
  3454. else
  3455. cntpd:=1;
  3456. if assigned(FCandidateProcs^.next) then
  3457. begin
  3458. save_validity(FCandidateProcs);
  3459. restart := false;
  3460. { keep restarting, until there wasn't a sorted-out besthpstart }
  3461. repeat
  3462. besthpstart:=FCandidateProcs;
  3463. bestpd:=FCandidateProcs^.data;
  3464. if restart then
  3465. begin
  3466. restore_validity(FCandidateProcs);
  3467. restart := false;
  3468. end;
  3469. { Setup the first procdef as best, only count it as a result
  3470. when it is valid }
  3471. if besthpstart^.invalid then
  3472. cntpd:=0
  3473. else
  3474. cntpd:=1;
  3475. hp:=FCandidateProcs^.next;
  3476. while assigned(hp) and not(restart) do
  3477. begin
  3478. restart := decide_restart(hp,besthpstart);
  3479. if not restart then
  3480. begin
  3481. if besthpstart^.invalid then res := 1
  3482. else if hp^.invalid then res := -1
  3483. else if not singlevariant then
  3484. res:=is_better_candidate(hp,besthpstart)
  3485. else
  3486. res:=is_better_candidate_single_variant(hp,besthpstart);
  3487. end;
  3488. if restart then
  3489. begin
  3490. { mark the sorted out invalid globally }
  3491. besthpstart^.saved_validity := true;
  3492. end
  3493. else if (res>0) then
  3494. begin
  3495. { hp is better, flag all procs to be incompatible }
  3496. while (besthpstart<>hp) do
  3497. begin
  3498. besthpstart^.invalid:=true;
  3499. besthpstart:=besthpstart^.next;
  3500. end;
  3501. { besthpstart is already set to hp }
  3502. bestpd:=besthpstart^.data;
  3503. if besthpstart^.invalid then
  3504. cntpd:=0
  3505. else
  3506. cntpd:=1;
  3507. end
  3508. else if (res<0) then
  3509. begin
  3510. { besthpstart is better, flag current hp to be incompatible }
  3511. hp^.invalid:=true;
  3512. end
  3513. else
  3514. begin
  3515. { res=0, both are valid }
  3516. if not hp^.invalid then
  3517. inc(cntpd);
  3518. end;
  3519. hp:=hp^.next;
  3520. end;
  3521. until not(restart);
  3522. end;
  3523. { check the alternate choices if they would have been sorted out before patch... }
  3524. { note we have procadded the candidates, so order is reversed procadd order here.
  3525. this was also used above: each sorted-out always has an "outsorter" counterpart
  3526. deeper down the next chain
  3527. }
  3528. { for the intial implementation, let's first do some more consistency checking}
  3529. res := 0;
  3530. hp := FCandidateProcs;
  3531. while assigned(hp) do
  3532. begin
  3533. if not(hp^.invalid) then
  3534. inc(res);
  3535. hp := hp^.next;
  3536. end;
  3537. if (res<>cntpd) then
  3538. internalerror(202002161);
  3539. { check all valid choices for sortout }
  3540. cntpd := 0;
  3541. hp := FCandidateProcs;
  3542. while assigned(hp) do
  3543. begin
  3544. if not(hp^.invalid) then
  3545. begin
  3546. hp2 := hp^.next;
  3547. while assigned(hp2) do begin
  3548. if compare_by_old_sortout_check(hp2,hp)<>0 then
  3549. begin
  3550. hp^.invalid := true;
  3551. hp2 := nil;
  3552. end
  3553. else
  3554. hp2:=hp2^.next;
  3555. end;
  3556. if not(hp^.invalid) then
  3557. begin
  3558. inc(cntpd);
  3559. { check for the impossible event bestpd had become invalid}
  3560. if (cntpd=1) and (hp^.data<>bestpd) then
  3561. internalerror(202002162);
  3562. end;
  3563. end;
  3564. hp := hp^.next;
  3565. end;
  3566. { if we've found one, check the procdefs ignored for overload choosing
  3567. to see whether they contain one from a child class with the same
  3568. parameters (so the overload choosing was not influenced by their
  3569. presence, but now that we've decided which overloaded version to call,
  3570. make sure we call the version closest in terms of visibility }
  3571. if cntpd=1 then
  3572. begin
  3573. for res:=0 to FIgnoredCandidateProcs.count-1 do
  3574. begin
  3575. pd:=tprocdef(FIgnoredCandidateProcs[res]);
  3576. { stop searching when we start comparing methods of parent of
  3577. the struct in which the current best method was found }
  3578. if assigned(pd.struct) and
  3579. (pd.struct<>tprocdef(bestpd).struct) and
  3580. def_is_related(tprocdef(bestpd).struct,pd.struct) then
  3581. break;
  3582. if (pd.proctypeoption=bestpd.proctypeoption) and
  3583. ((pd.procoptions*[po_classmethod,po_methodpointer])=(bestpd.procoptions*[po_classmethod,po_methodpointer])) and
  3584. (compare_paras(pd.paras,bestpd.paras,cp_all,[cpo_ignorehidden,cpo_ignoreuniv,cpo_openequalisexact])=te_exact) then
  3585. begin
  3586. { first one encountered is closest in terms of visibility }
  3587. bestpd:=pd;
  3588. break;
  3589. end;
  3590. end;
  3591. end;
  3592. result:=cntpd;
  3593. end;
  3594. {$endif}
  3595. procedure tcallcandidates.find_wrong_para;
  3596. var
  3597. currparanr : smallint;
  3598. hp : pcandidate;
  3599. pt : tcallparanode;
  3600. wrongpara : tparavarsym;
  3601. begin
  3602. { Only process the first overloaded procdef }
  3603. hp:=FCandidateProcs;
  3604. { Find callparanode corresponding to the argument }
  3605. pt:=tcallparanode(FParanode);
  3606. currparanr:=FParalength;
  3607. while assigned(pt) and
  3608. (currparanr>hp^.wrongparanr) do
  3609. begin
  3610. pt:=tcallparanode(pt.right);
  3611. dec(currparanr);
  3612. end;
  3613. if (currparanr<>hp^.wrongparanr) or
  3614. not assigned(pt) then
  3615. internalerror(200212094);
  3616. { Show error message, when it was a var or out parameter
  3617. guess that it is a missing typeconv }
  3618. wrongpara:=tparavarsym(hp^.data.paras[hp^.wrongparaidx]);
  3619. if wrongpara.varspez in [vs_var,vs_out] then
  3620. begin
  3621. { Maybe passing the correct type but passing a const to var parameter }
  3622. if (compare_defs(pt.resultdef,wrongpara.vardef,pt.nodetype)<>te_incompatible) and
  3623. not valid_for_var(pt.left,true) then
  3624. CGMessagePos(pt.left.fileinfo,type_e_variable_id_expected)
  3625. else
  3626. CGMessagePos3(pt.left.fileinfo,parser_e_call_by_ref_without_typeconv,tostr(hp^.wrongparanr),
  3627. FullTypeName(pt.left.resultdef,wrongpara.vardef),
  3628. FullTypeName(wrongpara.vardef,pt.left.resultdef))
  3629. end
  3630. else
  3631. CGMessagePos3(pt.left.fileinfo,type_e_wrong_parameter_type,tostr(hp^.wrongparanr),
  3632. FullTypeName(pt.left.resultdef,wrongpara.vardef),
  3633. FullTypeName(wrongpara.vardef,pt.left.resultdef));
  3634. end;
  3635. procedure check_ranges(const location: tfileposinfo; source: tnode; destdef: tdef);
  3636. begin
  3637. if not(cs_check_ordinal_size in current_settings.localswitches) then
  3638. exit;
  3639. { check if the assignment may cause a range check error }
  3640. { if its not explicit, and only if the values are }
  3641. { ordinals, enumdef and floatdef }
  3642. if assigned(destdef) and
  3643. (destdef.typ in [enumdef,orddef,floatdef]) and
  3644. not is_boolean(destdef) and
  3645. assigned(source.resultdef) and
  3646. (source.resultdef.typ in [enumdef,orddef,floatdef]) and
  3647. not is_boolean(source.resultdef) and
  3648. not is_constrealnode(source) and
  3649. { constants are handled via regular range checking }
  3650. (source.nodetype<>ordconstn) then
  3651. begin
  3652. if ((destdef.size < source.resultdef.size) and
  3653. { s80real and sc80real have a different size but the same precision }
  3654. not((destdef.typ=floatdef) and
  3655. (source.resultdef.typ=floatdef) and
  3656. (tfloatdef(source.resultdef).floattype in [s80real,sc80real]) and
  3657. (tfloatdef(destdef).floattype in [s80real,sc80real]))) or
  3658. ((destdef.typ<>floatdef) and
  3659. (source.resultdef.typ<>floatdef) and
  3660. not is_in_limit(source.resultdef,destdef)) then
  3661. begin
  3662. if (cs_check_range in current_settings.localswitches) then
  3663. MessagePos(location,type_w_smaller_possible_range_check)
  3664. else
  3665. MessagePos(location,type_h_smaller_possible_range_check);
  3666. end;
  3667. end;
  3668. end;
  3669. function is_valid_for_default(def:tdef):boolean;
  3670. function is_valid_record_or_object(def:tabstractrecorddef):boolean;
  3671. var
  3672. sym : tsym;
  3673. i : longint;
  3674. begin
  3675. for i:=0 to def.symtable.symlist.count-1 do
  3676. begin
  3677. sym:=tsym(def.symtable.symlist[i]);
  3678. if not is_normal_fieldvarsym(sym) then
  3679. continue;
  3680. if not is_valid_for_default(tfieldvarsym(sym).vardef) then
  3681. begin
  3682. result:=false;
  3683. exit;
  3684. end;
  3685. end;
  3686. result:=true;
  3687. end;
  3688. begin
  3689. case def.typ of
  3690. recorddef:
  3691. result:=is_valid_record_or_object(tabstractrecorddef(def));
  3692. objectdef:
  3693. if is_implicit_pointer_object_type(def) then
  3694. result:=true
  3695. else
  3696. if is_object(def) then
  3697. result:=is_valid_record_or_object(tabstractrecorddef(def))
  3698. else
  3699. result:=false;
  3700. arraydef:
  3701. if not (ado_isdynamicarray in tarraydef(def).arrayoptions) then
  3702. result:=is_valid_for_default(tarraydef(def).elementdef)
  3703. else
  3704. result:=true;
  3705. formaldef,
  3706. abstractdef,
  3707. filedef:
  3708. result:=false;
  3709. else
  3710. result:=true;
  3711. end;
  3712. end;
  3713. end.