htypechk.pas 142 KB

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