htypechk.pas 146 KB

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