htypechk.pas 159 KB

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