htypechk.pas 147 KB

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