htypechk.pas 147 KB

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