htypechk.pas 159 KB

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