htypechk.pas 145 KB

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