htypechk.pas 148 KB

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