htypechk.pas 160 KB

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