htypechk.pas 149 KB

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