htypechk.pas 148 KB

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