htypechk.pas 147 KB

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