htypechk.pas 145 KB

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