htypechk.pas 149 KB

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