htypechk.pas 141 KB

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