htypechk.pas 157 KB

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