htypechk.pas 158 KB

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