htypechk.pas 135 KB

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