htypechk.pas 148 KB

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