htypechk.pas 157 KB

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