htypechk.pas 161 KB

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