2
0

htypechk.pas 149 KB

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