htypechk.pas 159 KB

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