htypechk.pas 160 KB

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