htypechk.pas 158 KB

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