htypechk.pas 141 KB

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