htypechk.pas 135 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364
  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. { load easier access variables }
  797. ld:=tbinarynode(t).left.resultdef;
  798. rd:=tbinarynode(t).right.resultdef;
  799. if not isbinaryoperatoroverloadable(t.nodetype,ld,tbinarynode(t).left.nodetype,rd,tbinarynode(t).right.nodetype) then
  800. exit;
  801. { operator overload is possible }
  802. result:=true;
  803. case t.nodetype of
  804. equaln:
  805. optoken:=_EQ;
  806. unequaln:
  807. optoken:=_NE;
  808. addn:
  809. optoken:=_PLUS;
  810. subn:
  811. optoken:=_MINUS;
  812. muln:
  813. optoken:=_STAR;
  814. starstarn:
  815. optoken:=_STARSTAR;
  816. slashn:
  817. optoken:=_SLASH;
  818. ltn:
  819. optoken:=_LT;
  820. gtn:
  821. optoken:=_GT;
  822. lten:
  823. optoken:=_LTE;
  824. gten:
  825. optoken:=_GTE;
  826. symdifn :
  827. optoken:=_SYMDIF;
  828. modn :
  829. optoken:=_OP_MOD;
  830. orn :
  831. optoken:=_OP_OR;
  832. xorn :
  833. optoken:=_OP_XOR;
  834. andn :
  835. optoken:=_OP_AND;
  836. divn :
  837. optoken:=_OP_DIV;
  838. shln :
  839. optoken:=_OP_SHL;
  840. shrn :
  841. optoken:=_OP_SHR;
  842. inn :
  843. optoken:=_OP_IN;
  844. else
  845. begin
  846. CGMessage(parser_e_operator_not_overloaded);
  847. t:=cnothingnode.create;
  848. exit;
  849. end;
  850. end;
  851. cand_cnt:=search_operator(optoken,optoken<>_NE);
  852. { no operator found for "<>" then search for "=" operator }
  853. if (cand_cnt=0) and (optoken=_NE) then
  854. begin
  855. ppn.free;
  856. ppn:=nil;
  857. operpd:=nil;
  858. optoken:=_EQ;
  859. cand_cnt:=search_operator(optoken,true);
  860. end;
  861. if (cand_cnt=0) then
  862. begin
  863. ppn.free;
  864. t:=cnothingnode.create;
  865. exit;
  866. end;
  867. addsymref(operpd.procsym);
  868. { the nil as symtable signs firstcalln that this is
  869. an overloaded operator }
  870. ht:=ccallnode.create(ppn,Tprocsym(operpd.procsym),nil,nil,[]);
  871. { we already know the procdef to use, so it can
  872. skip the overload choosing in callnode.pass_typecheck }
  873. tcallnode(ht).procdefinition:=operpd;
  874. { if we found "=" operator for "<>" expression then use it
  875. together with "not" }
  876. if (t.nodetype=unequaln) and (optoken=_EQ) then
  877. ht:=cnotnode.create(ht);
  878. t:=ht;
  879. end;
  880. {****************************************************************************
  881. Register Calculation
  882. ****************************************************************************}
  883. { marks an lvalue as "unregable" }
  884. procedure make_not_regable_intern(p : tnode; how: tregableinfoflags; records_only: boolean);
  885. begin
  886. repeat
  887. case p.nodetype of
  888. subscriptn:
  889. begin
  890. records_only:=true;
  891. p:=tsubscriptnode(p).left;
  892. end;
  893. vecn:
  894. begin
  895. { if there's an implicit dereference, we can stop (just like
  896. when there is an actual derefn) }
  897. if ((tvecnode(p).left.resultdef.typ=arraydef) and
  898. not is_special_array(tvecnode(p).left.resultdef)) or
  899. ((tvecnode(p).left.resultdef.typ=stringdef) and
  900. (tstringdef(tvecnode(p).left.resultdef).stringtype in [st_shortstring,st_longstring])) then
  901. p:=tvecnode(p).left
  902. else
  903. break;
  904. end;
  905. typeconvn :
  906. begin
  907. { implicit dereference -> stop }
  908. if (ttypeconvnode(p).convtype=tc_pointer_2_array) then
  909. break;
  910. if (ttypeconvnode(p).resultdef.typ=recorddef) then
  911. records_only:=false;
  912. p:=ttypeconvnode(p).left;
  913. end;
  914. loadn :
  915. begin
  916. if (tloadnode(p).symtableentry.typ in [staticvarsym,localvarsym,paravarsym]) then
  917. begin
  918. if (ra_addr_taken in how) then
  919. tabstractvarsym(tloadnode(p).symtableentry).addr_taken:=true;
  920. if (tabstractvarsym(tloadnode(p).symtableentry).varregable <> vr_none) and
  921. ((not records_only) or
  922. (tabstractvarsym(tloadnode(p).symtableentry).vardef.typ = recorddef)) then
  923. if (tloadnode(p).symtableentry.typ = paravarsym) and
  924. (ra_addr_regable in how) then
  925. tabstractvarsym(tloadnode(p).symtableentry).varregable:=vr_addr
  926. else
  927. tabstractvarsym(tloadnode(p).symtableentry).varregable:=vr_none;
  928. end;
  929. break;
  930. end;
  931. temprefn :
  932. begin
  933. if (ra_addr_taken in how) then
  934. include(ttemprefnode(p).tempinfo^.flags,ti_addr_taken);
  935. if (ti_may_be_in_reg in ttemprefnode(p).tempinfo^.flags) and
  936. ((not records_only) or
  937. (ttemprefnode(p).tempinfo^.typedef.typ = recorddef)) then
  938. exclude(ttemprefnode(p).tempinfo^.flags,ti_may_be_in_reg);
  939. break;
  940. end;
  941. else
  942. break;
  943. end;
  944. until false;
  945. end;
  946. procedure make_not_regable(p : tnode; how: tregableinfoflags);
  947. begin
  948. make_not_regable_intern(p,how,false);
  949. end;
  950. {****************************************************************************
  951. Subroutine Handling
  952. ****************************************************************************}
  953. function is_proc2procvar_load(p:tnode;out realprocdef:tprocdef):boolean;
  954. begin
  955. result:=false;
  956. { remove voidpointer typecast for tp procvars }
  957. if ((m_tp_procvar in current_settings.modeswitches) or
  958. (m_mac_procvar in current_settings.modeswitches)) and
  959. (p.nodetype=typeconvn) and
  960. is_voidpointer(p.resultdef) then
  961. p:=tunarynode(p).left;
  962. result:=(p.nodetype=typeconvn) and
  963. (ttypeconvnode(p).convtype=tc_proc_2_procvar);
  964. if result then
  965. realprocdef:=tprocdef(ttypeconvnode(p).left.resultdef);
  966. end;
  967. function is_ambiguous_funcret_load(p: tnode; out owningprocdef: tprocdef): boolean;
  968. begin
  969. result:=false;
  970. { the funcret is an absolutevarsym, which gets converted into a type
  971. conversion node of the loadnode of the actual function result. Its
  972. resulttype is obviously the same as that of the real function result }
  973. if (p.nodetype=typeconvn) and
  974. (p.resultdef=ttypeconvnode(p).left.resultdef) then
  975. p:=ttypeconvnode(p).left;
  976. if (p.nodetype=loadn) and
  977. (tloadnode(p).symtableentry.typ in [absolutevarsym,localvarsym,paravarsym]) and
  978. ([vo_is_funcret,vo_is_result] * tabstractvarsym(tloadnode(p).symtableentry).varoptions = [vo_is_funcret]) then
  979. begin
  980. owningprocdef:=tprocdef(tloadnode(p).symtableentry.owner.defowner);
  981. result:=true;
  982. end;
  983. end;
  984. { local routines can't be assigned to procvars }
  985. procedure test_local_to_procvar(from_def:tprocvardef;to_def:tdef);
  986. begin
  987. if not(m_nested_procvars in current_settings.modeswitches) and
  988. (from_def.parast.symtablelevel>normal_function_level) and
  989. (to_def.typ=procvardef) then
  990. CGMessage(type_e_cannot_local_proc_to_procvar);
  991. end;
  992. procedure set_varstate(p:tnode;newstate:tvarstate;varstateflags:tvarstateflags);
  993. const
  994. vstrans: array[tvarstate,tvarstate] of tvarstate = (
  995. { vs_none -> ... }
  996. (vs_none,vs_declared,vs_initialised,vs_read,vs_read_not_warned,vs_referred_not_inited,vs_written,vs_readwritten),
  997. { vs_declared -> ... }
  998. (vs_none,vs_declared,vs_initialised,vs_read,vs_read_not_warned,vs_referred_not_inited,vs_written,vs_readwritten),
  999. { vs_initialised -> ... }
  1000. (vs_none,vs_initialised,vs_initialised,vs_read,vs_read,vs_read,vs_written,vs_readwritten),
  1001. { vs_read -> ... }
  1002. (vs_none,vs_read,vs_read,vs_read,vs_read,vs_read,vs_readwritten,vs_readwritten),
  1003. { vs_read_not_warned -> ... }
  1004. (vs_none,vs_read_not_warned,vs_read,vs_read,vs_read_not_warned,vs_read_not_warned,vs_readwritten,vs_readwritten),
  1005. { vs_referred_not_inited }
  1006. (vs_none,vs_referred_not_inited,vs_read,vs_read,vs_read_not_warned,vs_referred_not_inited,vs_written,vs_readwritten),
  1007. { vs_written -> ... }
  1008. (vs_none,vs_written,vs_written,vs_readwritten,vs_readwritten,vs_written,vs_written,vs_readwritten),
  1009. { vs_readwritten -> ... }
  1010. (vs_none,vs_readwritten,vs_readwritten,vs_readwritten,vs_readwritten,vs_readwritten,vs_readwritten,vs_readwritten));
  1011. var
  1012. hsym : tabstractvarsym;
  1013. begin
  1014. { make sure we can still warn about uninitialised use after high(v), @v etc }
  1015. if (newstate = vs_read) and
  1016. not(vsf_must_be_valid in varstateflags) then
  1017. newstate := vs_referred_not_inited;
  1018. while assigned(p) do
  1019. begin
  1020. case p.nodetype of
  1021. derefn:
  1022. begin
  1023. if (tderefnode(p).left.nodetype=temprefn) and
  1024. assigned(ttemprefnode(tderefnode(p).left).tempinfo^.withnode) then
  1025. p:=ttemprefnode(tderefnode(p).left).tempinfo^.withnode
  1026. else
  1027. break;
  1028. end;
  1029. typeconvn :
  1030. begin
  1031. case ttypeconvnode(p).convtype of
  1032. tc_cchar_2_pchar,
  1033. tc_cstring_2_pchar,
  1034. tc_array_2_pointer :
  1035. exclude(varstateflags,vsf_must_be_valid);
  1036. tc_pchar_2_string,
  1037. tc_pointer_2_array :
  1038. include(varstateflags,vsf_must_be_valid);
  1039. end;
  1040. p:=tunarynode(p).left;
  1041. end;
  1042. subscriptn :
  1043. begin
  1044. if is_implicit_pointer_object_type(tunarynode(p).left.resultdef) then
  1045. newstate := vs_read;
  1046. p:=tunarynode(p).left;
  1047. end;
  1048. vecn:
  1049. begin
  1050. set_varstate(tbinarynode(p).right,vs_read,[vsf_must_be_valid]);
  1051. if (newstate in [vs_read,vs_readwritten]) or
  1052. not(tunarynode(p).left.resultdef.typ in [stringdef,arraydef]) then
  1053. include(varstateflags,vsf_must_be_valid)
  1054. else if (newstate = vs_written) then
  1055. exclude(varstateflags,vsf_must_be_valid);
  1056. p:=tunarynode(p).left;
  1057. end;
  1058. { do not parse calln }
  1059. calln :
  1060. break;
  1061. loadn :
  1062. begin
  1063. if (tloadnode(p).symtableentry.typ in [localvarsym,paravarsym,staticvarsym]) then
  1064. begin
  1065. hsym:=tabstractvarsym(tloadnode(p).symtableentry);
  1066. { this check requires proper data flow analysis... }
  1067. (* if (hsym.varspez=vs_final) and
  1068. (hsym.varstate in [vs_written,vs_readwritten]) and
  1069. (newstate in [vs_written,vs_readwritten]) then
  1070. CGMessagePos1(p.fileinfo,sym_e_final_write_once); *)
  1071. if (vsf_must_be_valid in varstateflags) and
  1072. (hsym.varstate in [vs_declared,vs_read_not_warned,vs_referred_not_inited]) then
  1073. begin
  1074. { Give warning/note for uninitialized locals }
  1075. if assigned(hsym.owner) and
  1076. not(vo_is_external in hsym.varoptions) and
  1077. (hsym.owner.symtabletype in [parasymtable,localsymtable,staticsymtable]) and
  1078. ((hsym.owner=current_procinfo.procdef.localst) or
  1079. (hsym.owner=current_procinfo.procdef.parast)) then
  1080. begin
  1081. if vsf_use_hints in varstateflags then
  1082. include(tloadnode(p).loadnodeflags,loadnf_only_uninitialized_hint);
  1083. if not(cs_opt_nodedfa in current_settings.optimizerswitches) then
  1084. begin
  1085. if (vo_is_funcret in hsym.varoptions) then
  1086. begin
  1087. if (vsf_use_hints in varstateflags) then
  1088. CGMessagePos(p.fileinfo,sym_h_function_result_uninitialized)
  1089. else
  1090. CGMessagePos(p.fileinfo,sym_w_function_result_uninitialized)
  1091. end
  1092. else
  1093. begin
  1094. if tloadnode(p).symtable.symtabletype=localsymtable then
  1095. begin
  1096. { on the JVM, an uninitialized var-parameter
  1097. is just as fatal as a nil pointer dereference }
  1098. if (vsf_use_hints in varstateflags) and
  1099. not(target_info.system in systems_jvm) then
  1100. CGMessagePos1(p.fileinfo,sym_h_uninitialized_local_variable,hsym.realname)
  1101. else
  1102. CGMessagePos1(p.fileinfo,sym_w_uninitialized_local_variable,hsym.realname);
  1103. end
  1104. else
  1105. begin
  1106. { on the JVM, an uninitialized var-parameter
  1107. is just as fatal as a nil pointer dereference }
  1108. if (vsf_use_hints in varstateflags) and
  1109. not(target_info.system in systems_jvm) then
  1110. CGMessagePos1(p.fileinfo,sym_h_uninitialized_variable,hsym.realname)
  1111. else
  1112. CGMessagePos1(p.fileinfo,sym_w_uninitialized_variable,hsym.realname);
  1113. end;
  1114. end;
  1115. end;
  1116. end
  1117. else if (newstate = vs_read) then
  1118. newstate := vs_read_not_warned;
  1119. end;
  1120. hsym.varstate := vstrans[hsym.varstate,newstate];
  1121. end;
  1122. case newstate of
  1123. vs_written:
  1124. include(tloadnode(p).flags,nf_write);
  1125. vs_readwritten:
  1126. if not(nf_write in tloadnode(p).flags) then
  1127. include(tloadnode(p).flags,nf_modify);
  1128. end;
  1129. break;
  1130. end;
  1131. callparan :
  1132. internalerror(200310081);
  1133. else
  1134. break;
  1135. end;{case }
  1136. end;
  1137. end;
  1138. procedure set_unique(p : tnode);
  1139. begin
  1140. while assigned(p) do
  1141. begin
  1142. case p.nodetype of
  1143. vecn:
  1144. begin
  1145. include(p.flags,nf_callunique);
  1146. break;
  1147. end;
  1148. typeconvn,
  1149. subscriptn,
  1150. derefn:
  1151. p:=tunarynode(p).left;
  1152. else
  1153. break;
  1154. end;
  1155. end;
  1156. end;
  1157. function valid_for_assign(p:tnode;opts:TValidAssigns; report_errors: boolean):boolean;
  1158. var
  1159. typeconvs: tfpobjectlist;
  1160. hp2,
  1161. hp : tnode;
  1162. gotstring,
  1163. gotsubscript,
  1164. gotrecord,
  1165. gotpointer,
  1166. gotvec,
  1167. gotclass,
  1168. gotdynarray,
  1169. gotderef,
  1170. gottypeconv : boolean;
  1171. fromdef,
  1172. todef : tdef;
  1173. errmsg,
  1174. temp : longint;
  1175. function constaccessok(vs: tabstractvarsym): boolean;
  1176. begin
  1177. result:=false;
  1178. { allow p^:= constructions with p is const parameter }
  1179. if gotderef or gotdynarray or (Valid_Const in opts) or
  1180. ((hp.nodetype=loadn) and
  1181. (loadnf_isinternal_ignoreconst in tloadnode(hp).loadnodeflags)) then
  1182. result:=true
  1183. { final (class) fields can only be initialised in the (class) constructors of
  1184. class in which they have been declared (not in descendent constructors) }
  1185. else if vs.varspez=vs_final then
  1186. begin
  1187. if (current_procinfo.procdef.owner=vs.owner) then
  1188. if vs.typ=staticvarsym then
  1189. result:=current_procinfo.procdef.proctypeoption=potype_class_constructor
  1190. else
  1191. result:=current_procinfo.procdef.proctypeoption=potype_constructor;
  1192. if not result and
  1193. report_errors then
  1194. CGMessagePos(hp.fileinfo,type_e_invalid_final_assignment);
  1195. end
  1196. else
  1197. if report_errors then
  1198. CGMessagePos(hp.fileinfo,type_e_no_assign_to_const);
  1199. end;
  1200. procedure mayberesettypeconvs;
  1201. var
  1202. i: longint;
  1203. begin
  1204. if assigned(typeconvs) then
  1205. begin
  1206. if not report_errors and
  1207. not result then
  1208. for i:=0 to typeconvs.Count-1 do
  1209. ttypeconvnode(typeconvs[i]).assignment_side:=false;
  1210. typeconvs.free;
  1211. end;
  1212. end;
  1213. begin
  1214. if valid_const in opts then
  1215. errmsg:=type_e_variable_id_expected
  1216. else if valid_property in opts then
  1217. errmsg:=type_e_argument_cant_be_assigned
  1218. else
  1219. errmsg:=type_e_no_addr_of_constant;
  1220. result:=false;
  1221. gotsubscript:=false;
  1222. gotvec:=false;
  1223. gotderef:=false;
  1224. gotrecord:=false;
  1225. gotclass:=false;
  1226. gotpointer:=false;
  1227. gotdynarray:=false;
  1228. gotstring:=false;
  1229. gottypeconv:=false;
  1230. hp:=p;
  1231. if not(valid_void in opts) and
  1232. is_void(hp.resultdef) then
  1233. begin
  1234. if report_errors then
  1235. CGMessagePos(hp.fileinfo,errmsg);
  1236. exit;
  1237. end;
  1238. typeconvs:=nil;
  1239. while assigned(hp) do
  1240. begin
  1241. { property allowed? calln has a property check itself }
  1242. if (nf_isproperty in hp.flags) then
  1243. begin
  1244. { check return type }
  1245. case hp.resultdef.typ of
  1246. pointerdef :
  1247. gotpointer:=true;
  1248. objectdef :
  1249. gotclass:=is_implicit_pointer_object_type(hp.resultdef);
  1250. recorddef :
  1251. gotrecord:=true;
  1252. classrefdef :
  1253. gotclass:=true;
  1254. stringdef :
  1255. gotstring:=true;
  1256. end;
  1257. if (valid_property in opts) then
  1258. begin
  1259. { don't allow writing to calls that will create
  1260. temps like calls that return a structure and we
  1261. are assigning to a member }
  1262. if (valid_const in opts) or
  1263. { if we got a deref, we won't modify the property itself }
  1264. (gotderef) or
  1265. { same when we got a class and subscript (= deref) }
  1266. (gotclass and gotsubscript) or
  1267. { indexing a dynamic array = dereference }
  1268. (gotdynarray and gotvec) or
  1269. (
  1270. { allowing assignments to typecasted properties
  1271. a) is Delphi-incompatible
  1272. b) causes problems in case the getter is a function
  1273. (because then the result of the getter is
  1274. typecasted to this type, and then we "assign" to
  1275. this typecasted function result) -> always
  1276. disallow, since property accessors should be
  1277. transparantly changeable to functions at all
  1278. times
  1279. }
  1280. not(gottypeconv) and
  1281. not(gotsubscript and gotrecord) and
  1282. not(gotstring and gotvec) and
  1283. not(nf_no_lvalue in hp.flags)
  1284. ) then
  1285. result:=true
  1286. else
  1287. if report_errors then
  1288. CGMessagePos(hp.fileinfo,errmsg);
  1289. end
  1290. else
  1291. begin
  1292. { 1. if it returns a pointer and we've found a deref,
  1293. 2. if it returns a class and a subscription or with is found
  1294. 3. if the address is needed of a field (subscriptn, vecn) }
  1295. if (gotpointer and gotderef) or
  1296. (gotstring and gotvec) or
  1297. (gotclass and gotsubscript) or
  1298. (
  1299. (gotvec and gotdynarray)
  1300. ) or
  1301. (
  1302. (Valid_Addr in opts) and
  1303. (hp.nodetype in [subscriptn,vecn])
  1304. ) then
  1305. result:=true
  1306. else
  1307. if report_errors then
  1308. CGMessagePos(hp.fileinfo,errmsg);
  1309. end;
  1310. mayberesettypeconvs;
  1311. exit;
  1312. end;
  1313. case hp.nodetype of
  1314. temprefn :
  1315. begin
  1316. valid_for_assign := not(ti_readonly in ttemprefnode(hp).tempinfo^.flags);
  1317. mayberesettypeconvs;
  1318. exit;
  1319. end;
  1320. derefn :
  1321. begin
  1322. gotderef:=true;
  1323. hp:=tderefnode(hp).left;
  1324. end;
  1325. typeconvn :
  1326. begin
  1327. gottypeconv:=true;
  1328. { typecast sizes must match, exceptions:
  1329. - implicit typecast made by absolute
  1330. - from formaldef
  1331. - from void
  1332. - from/to open array
  1333. - typecast from pointer to array }
  1334. fromdef:=ttypeconvnode(hp).left.resultdef;
  1335. todef:=hp.resultdef;
  1336. { typeconversions on the assignment side must keep
  1337. left.location the same }
  1338. if not(gotderef or
  1339. ((target_info.system in systems_jvm) and
  1340. (gotsubscript or gotvec))) then
  1341. begin
  1342. ttypeconvnode(hp).assignment_side:=true;
  1343. if not assigned(typeconvs) then
  1344. typeconvs:=tfpobjectlist.create(false);
  1345. typeconvs.add(hp);
  1346. end;
  1347. { in managed VMs, you cannot typecast formaldef when assigning
  1348. to it, see http://hallvards.blogspot.com/2007/10/dn4dp24-net-vs-win32-untyped-parameters.html }
  1349. if (target_info.system in systems_managed_vm) and
  1350. (fromdef.typ=formaldef) then
  1351. begin
  1352. if report_errors then
  1353. CGMessagePos(hp.fileinfo,type_e_no_managed_formal_assign_typecast);
  1354. mayberesettypeconvs;
  1355. exit;
  1356. end
  1357. else if not((nf_absolute in ttypeconvnode(hp).flags) or
  1358. ttypeconvnode(hp).target_specific_general_typeconv or
  1359. ((nf_explicit in hp.flags) and
  1360. ttypeconvnode(hp).target_specific_explicit_typeconv) or
  1361. (fromdef.typ=formaldef) or
  1362. is_void(fromdef) or
  1363. is_open_array(fromdef) or
  1364. is_open_array(todef) or
  1365. ((fromdef.typ=pointerdef) and (todef.typ=arraydef)) or
  1366. (fromdef.is_related(todef))) and
  1367. (fromdef.size<>todef.size) then
  1368. begin
  1369. { in TP it is allowed to typecast to smaller types. But the variable can't
  1370. be in a register }
  1371. if (m_tp7 in current_settings.modeswitches) or
  1372. (todef.size<fromdef.size) then
  1373. make_not_regable(hp,[ra_addr_regable])
  1374. else
  1375. if report_errors then
  1376. CGMessagePos2(hp.fileinfo,type_e_typecast_wrong_size_for_assignment,tostr(fromdef.size),tostr(todef.size));
  1377. end;
  1378. { don't allow assignments to typeconvs that need special code }
  1379. if not(gotsubscript or gotvec or gotderef) and
  1380. not(ttypeconvnode(hp).assign_allowed) then
  1381. begin
  1382. if report_errors then
  1383. CGMessagePos(hp.fileinfo,errmsg);
  1384. mayberesettypeconvs;
  1385. exit;
  1386. end;
  1387. case hp.resultdef.typ of
  1388. pointerdef :
  1389. gotpointer:=true;
  1390. objectdef :
  1391. gotclass:=is_implicit_pointer_object_type(hp.resultdef);
  1392. classrefdef :
  1393. gotclass:=true;
  1394. arraydef :
  1395. begin
  1396. { pointer -> array conversion is done then we need to see it
  1397. as a deref, because a ^ is then not required anymore }
  1398. if (ttypeconvnode(hp).left.resultdef.typ=pointerdef) then
  1399. gotderef:=true;
  1400. end;
  1401. end;
  1402. hp:=ttypeconvnode(hp).left;
  1403. end;
  1404. vecn :
  1405. begin
  1406. if (tvecnode(hp).right.nodetype=rangen) and
  1407. not(valid_range in opts) then
  1408. begin
  1409. if report_errors then
  1410. CGMessagePos(tvecnode(hp).right.fileinfo,parser_e_illegal_expression);
  1411. mayberesettypeconvs;
  1412. exit;
  1413. end;
  1414. if { only check for first (= outermost) vec node }
  1415. not gotvec and
  1416. not(valid_packed in opts) and
  1417. (tvecnode(hp).left.resultdef.typ = arraydef) and
  1418. (ado_IsBitPacked in tarraydef(tvecnode(hp).left.resultdef).arrayoptions) and
  1419. ((tarraydef(tvecnode(hp).left.resultdef).elepackedbitsize mod 8 <> 0) or
  1420. (is_ordinal(tarraydef(tvecnode(hp).left.resultdef).elementdef) and
  1421. not ispowerof2(tarraydef(tvecnode(hp).left.resultdef).elepackedbitsize div 8,temp))) then
  1422. begin
  1423. if report_errors then
  1424. if (valid_property in opts) then
  1425. CGMessagePos(hp.fileinfo,parser_e_packed_element_no_loop)
  1426. else
  1427. CGMessagePos(hp.fileinfo,parser_e_packed_element_no_var_addr);
  1428. mayberesettypeconvs;
  1429. exit;
  1430. end;
  1431. gotvec:=true;
  1432. { accesses to dyn. arrays override read only access in delphi
  1433. -- now also in FPC, because the elements of a dynamic array
  1434. returned by a function can also be changed, or you can
  1435. assign the dynamic array to a variable and then change
  1436. its elements anyway }
  1437. if is_dynamic_array(tunarynode(hp).left.resultdef) then
  1438. gotdynarray:=true;
  1439. hp:=tunarynode(hp).left;
  1440. end;
  1441. asn :
  1442. begin
  1443. { asn can't be assigned directly, it returns the value in a register instead
  1444. of reference. }
  1445. if not(gotsubscript or gotderef or gotvec) then
  1446. begin
  1447. if report_errors then
  1448. CGMessagePos(hp.fileinfo,errmsg);
  1449. mayberesettypeconvs;
  1450. exit;
  1451. end;
  1452. hp:=tunarynode(hp).left;
  1453. end;
  1454. subscriptn :
  1455. begin
  1456. { only check first (= outermost) subscriptn }
  1457. if not gotsubscript and
  1458. not(valid_packed in opts) and
  1459. is_packed_record_or_object(tsubscriptnode(hp).left.resultdef) and
  1460. ((tsubscriptnode(hp).vs.fieldoffset mod 8 <> 0) or
  1461. (is_ordinal(tsubscriptnode(hp).resultdef) and
  1462. not ispowerof2(tsubscriptnode(hp).resultdef.packedbitsize div 8,temp))) then
  1463. begin
  1464. if report_errors then
  1465. if (valid_property in opts) then
  1466. CGMessagePos(hp.fileinfo,parser_e_packed_element_no_loop)
  1467. else
  1468. CGMessagePos(hp.fileinfo,parser_e_packed_element_no_var_addr);
  1469. mayberesettypeconvs;
  1470. exit;
  1471. end;
  1472. { check for final fields }
  1473. if (tsubscriptnode(hp).vs.varspez=vs_final) and
  1474. not constaccessok(tsubscriptnode(hp).vs) then
  1475. begin
  1476. mayberesettypeconvs;
  1477. exit;
  1478. end;
  1479. { if we assign something to a field of a record that is not
  1480. regable, then then the record can't be kept in a regvar,
  1481. because we will force the record into memory for this
  1482. subscript operation (to a temp location, so the assignment
  1483. will happen to the temp and be lost) }
  1484. if not gotsubscript and
  1485. not gotderef and
  1486. not gotvec and
  1487. not tstoreddef(hp.resultdef).is_intregable then
  1488. make_not_regable(hp,[ra_addr_regable]);
  1489. gotsubscript:=true;
  1490. { loop counter? }
  1491. if not(Valid_Const in opts) and
  1492. (vo_is_loop_counter in tsubscriptnode(hp).vs.varoptions) then
  1493. begin
  1494. if report_errors then
  1495. CGMessage1(parser_e_illegal_assignment_to_count_var,tsubscriptnode(hp).vs.realname);
  1496. mayberesettypeconvs;
  1497. exit;
  1498. end;
  1499. { implicit pointer object types result in dereferencing }
  1500. hp:=tsubscriptnode(hp).left;
  1501. if is_implicit_pointer_object_type(hp.resultdef) then
  1502. gotderef:=true;
  1503. end;
  1504. muln,
  1505. divn,
  1506. andn,
  1507. xorn,
  1508. orn,
  1509. notn,
  1510. subn,
  1511. addn :
  1512. begin
  1513. { Allow operators on a pointer, or an integer
  1514. and a pointer typecast and deref has been found }
  1515. if ((hp.resultdef.typ=pointerdef) or
  1516. (is_integer(hp.resultdef) and gotpointer)) and
  1517. gotderef then
  1518. result:=true
  1519. else
  1520. { Temp strings are stored in memory, for compatibility with
  1521. delphi only }
  1522. if (m_delphi in current_settings.modeswitches) and
  1523. ((valid_addr in opts) or
  1524. (valid_const in opts)) and
  1525. (hp.resultdef.typ=stringdef) then
  1526. result:=true
  1527. else
  1528. if report_errors then
  1529. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  1530. mayberesettypeconvs;
  1531. exit;
  1532. end;
  1533. niln,
  1534. pointerconstn :
  1535. begin
  1536. { to support e.g. @tmypointer(0)^.data; see tests/tbs/tb0481 }
  1537. if gotderef then
  1538. result:=true
  1539. else
  1540. if report_errors then
  1541. CGMessagePos(hp.fileinfo,type_e_no_assign_to_addr);
  1542. mayberesettypeconvs;
  1543. exit;
  1544. end;
  1545. ordconstn,
  1546. realconstn :
  1547. begin
  1548. { these constants will be passed by value }
  1549. if report_errors then
  1550. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  1551. mayberesettypeconvs;
  1552. exit;
  1553. end;
  1554. setconstn,
  1555. stringconstn,
  1556. guidconstn :
  1557. begin
  1558. { these constants will be passed by reference }
  1559. if valid_const in opts then
  1560. result:=true
  1561. else
  1562. if report_errors then
  1563. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  1564. mayberesettypeconvs;
  1565. exit;
  1566. end;
  1567. addrn :
  1568. begin
  1569. if gotderef then
  1570. result:=true
  1571. else
  1572. if report_errors then
  1573. CGMessagePos(hp.fileinfo,type_e_no_assign_to_addr);
  1574. mayberesettypeconvs;
  1575. exit;
  1576. end;
  1577. blockn,
  1578. calln :
  1579. begin
  1580. if (hp.nodetype=calln) or
  1581. (nf_no_lvalue in hp.flags) then
  1582. begin
  1583. { check return type }
  1584. case hp.resultdef.typ of
  1585. arraydef :
  1586. begin
  1587. { dynamic arrays are allowed when there is also a
  1588. vec node }
  1589. if is_dynamic_array(hp.resultdef) and
  1590. gotvec then
  1591. begin
  1592. gotderef:=true;
  1593. gotpointer:=true;
  1594. end;
  1595. end;
  1596. pointerdef :
  1597. gotpointer:=true;
  1598. objectdef :
  1599. gotclass:=is_implicit_pointer_object_type(hp.resultdef);
  1600. recorddef, { handle record like class it needs a subscription }
  1601. classrefdef :
  1602. gotclass:=true;
  1603. stringdef :
  1604. gotstring:=true;
  1605. end;
  1606. { 1. if it returns a pointer and we've found a deref,
  1607. 2. if it returns a class or record and a subscription or with is found
  1608. 3. string is returned }
  1609. if (gotstring and gotvec) or
  1610. (gotpointer and gotderef) or
  1611. (gotclass and gotsubscript) then
  1612. result:=true
  1613. else
  1614. { Temp strings are stored in memory, for compatibility with
  1615. delphi only }
  1616. if (m_delphi in current_settings.modeswitches) and
  1617. (valid_addr in opts) and
  1618. (hp.resultdef.typ=stringdef) then
  1619. result:=true
  1620. else
  1621. if ([valid_const,valid_addr] * opts = [valid_const]) then
  1622. result:=true
  1623. else
  1624. if report_errors then
  1625. CGMessagePos(hp.fileinfo,errmsg);
  1626. mayberesettypeconvs;
  1627. exit;
  1628. end
  1629. else
  1630. begin
  1631. hp2:=tblocknode(hp).statements;
  1632. if assigned(hp2) then
  1633. begin
  1634. if hp2.nodetype<>statementn then
  1635. internalerror(2006110801);
  1636. while assigned(tstatementnode(hp2).next) do
  1637. hp2:=tstatementnode(hp2).next;
  1638. hp:=tstatementnode(hp2).statement;
  1639. end
  1640. else
  1641. begin
  1642. if report_errors then
  1643. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  1644. mayberesettypeconvs;
  1645. exit;
  1646. end;
  1647. end;
  1648. end;
  1649. inlinen :
  1650. begin
  1651. if ((valid_const in opts) and
  1652. (tinlinenode(hp).inlinenumber in [in_typeof_x])) or
  1653. (tinlinenode(hp).inlinenumber in [in_unaligned_x,in_aligned_x]) then
  1654. result:=true
  1655. else
  1656. if report_errors then
  1657. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  1658. mayberesettypeconvs;
  1659. exit;
  1660. end;
  1661. dataconstn:
  1662. begin
  1663. { only created internally, so no additional checks necessary }
  1664. result:=true;
  1665. mayberesettypeconvs;
  1666. exit;
  1667. end;
  1668. nothingn :
  1669. begin
  1670. { generics can generate nothing nodes, just allow everything }
  1671. if df_generic in current_procinfo.procdef.defoptions then
  1672. result:=true
  1673. else if report_errors then
  1674. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  1675. mayberesettypeconvs;
  1676. exit;
  1677. end;
  1678. loadn :
  1679. begin
  1680. case tloadnode(hp).symtableentry.typ of
  1681. absolutevarsym,
  1682. staticvarsym,
  1683. localvarsym,
  1684. paravarsym :
  1685. begin
  1686. { loop counter? }
  1687. if not(Valid_Const in opts) and
  1688. not gotderef and
  1689. (vo_is_loop_counter in tabstractvarsym(tloadnode(hp).symtableentry).varoptions) then
  1690. begin
  1691. if report_errors then
  1692. CGMessage1(parser_e_illegal_assignment_to_count_var,tloadnode(hp).symtableentry.realname);
  1693. mayberesettypeconvs;
  1694. exit;
  1695. end;
  1696. { read-only variable? }
  1697. if (tabstractvarsym(tloadnode(hp).symtableentry).varspez in [vs_const,vs_constref,vs_final]) then
  1698. begin
  1699. result:=constaccessok(tabstractvarsym(tloadnode(hp).symtableentry));
  1700. mayberesettypeconvs;
  1701. exit;
  1702. end;
  1703. result:=true;
  1704. mayberesettypeconvs;
  1705. exit;
  1706. end;
  1707. procsym :
  1708. begin
  1709. if (Valid_Const in opts) then
  1710. result:=true
  1711. else
  1712. if report_errors then
  1713. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  1714. mayberesettypeconvs;
  1715. exit;
  1716. end;
  1717. labelsym :
  1718. begin
  1719. if (Valid_Addr in opts) then
  1720. result:=true
  1721. else
  1722. if report_errors then
  1723. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  1724. mayberesettypeconvs;
  1725. exit;
  1726. end;
  1727. constsym:
  1728. begin
  1729. if (tconstsym(tloadnode(hp).symtableentry).consttyp=constresourcestring) and
  1730. (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. else
  1739. begin
  1740. if report_errors then
  1741. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  1742. mayberesettypeconvs;
  1743. exit;
  1744. end;
  1745. end;
  1746. end;
  1747. else
  1748. begin
  1749. if report_errors then
  1750. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  1751. mayberesettypeconvs;
  1752. exit;
  1753. end;
  1754. end;
  1755. end;
  1756. mayberesettypeconvs;
  1757. end;
  1758. function valid_for_var(p:tnode; report_errors: boolean):boolean;
  1759. begin
  1760. valid_for_var:=valid_for_assign(p,[valid_range],report_errors);
  1761. end;
  1762. function valid_for_formal_var(p : tnode; report_errors: boolean) : boolean;
  1763. begin
  1764. valid_for_formal_var:=valid_for_assign(p,[valid_void,valid_range],report_errors);
  1765. end;
  1766. function valid_for_formal_const(p : tnode; report_errors: boolean) : boolean;
  1767. begin
  1768. valid_for_formal_const:=(p.resultdef.typ=formaldef) or
  1769. valid_for_assign(p,[valid_void,valid_const,valid_property,valid_range],report_errors);
  1770. end;
  1771. function valid_for_assignment(p:tnode; report_errors: boolean):boolean;
  1772. begin
  1773. valid_for_assignment:=valid_for_assign(p,[valid_property,valid_packed],report_errors);
  1774. end;
  1775. function valid_for_loopvar(p:tnode; report_errors: boolean):boolean;
  1776. begin
  1777. valid_for_loopvar:=valid_for_assign(p,[valid_property],report_errors);
  1778. end;
  1779. function valid_for_addr(p : tnode; report_errors: boolean) : boolean;
  1780. begin
  1781. result:=valid_for_assign(p,[valid_const,valid_addr,valid_void],report_errors);
  1782. end;
  1783. procedure var_para_allowed(var eq:tequaltype;def_from,def_to:Tdef; fromnode: tnode);
  1784. begin
  1785. { Note: eq must be already valid, it will only be updated! }
  1786. case def_to.typ of
  1787. formaldef :
  1788. begin
  1789. { all types can be passed to a formaldef,
  1790. but it is not the prefered way }
  1791. if not is_constnode(fromnode) then
  1792. eq:=te_convert_l2
  1793. else
  1794. eq:=te_incompatible;
  1795. end;
  1796. orddef :
  1797. begin
  1798. { allows conversion from word to integer and
  1799. byte to shortint, but only for TP7 compatibility }
  1800. if (m_tp7 in current_settings.modeswitches) and
  1801. (def_from.typ=orddef) and
  1802. (def_from.size=def_to.size) then
  1803. eq:=te_convert_l1;
  1804. end;
  1805. arraydef :
  1806. begin
  1807. if is_open_array(def_to) then
  1808. begin
  1809. if is_dynamic_array(def_from) and
  1810. equal_defs(tarraydef(def_from).elementdef,tarraydef(def_to).elementdef) then
  1811. eq:=te_convert_l2
  1812. else
  1813. if equal_defs(def_from,tarraydef(def_to).elementdef) then
  1814. eq:=te_convert_l3;
  1815. end;
  1816. end;
  1817. pointerdef :
  1818. begin
  1819. { an implicit pointer conversion is allowed }
  1820. if (def_from.typ=pointerdef) then
  1821. eq:=te_convert_l1;
  1822. end;
  1823. stringdef :
  1824. begin
  1825. { all shortstrings are allowed, size is not important }
  1826. if is_shortstring(def_from) and
  1827. is_shortstring(def_to) then
  1828. eq:=te_equal;
  1829. end;
  1830. objectdef :
  1831. begin
  1832. { child objects can be also passed }
  1833. { in non-delphi mode, otherwise }
  1834. { they must match exactly, except }
  1835. { if they are objects }
  1836. if (def_from.typ=objectdef) and
  1837. (
  1838. (tobjectdef(def_from).objecttype=odt_object) and
  1839. (tobjectdef(def_to).objecttype=odt_object)
  1840. ) and
  1841. (tobjectdef(def_from).is_related(tobjectdef(def_to))) then
  1842. eq:=te_convert_l1;
  1843. end;
  1844. filedef :
  1845. begin
  1846. { an implicit file conversion is also allowed }
  1847. { from a typed file to an untyped one }
  1848. if (def_from.typ=filedef) and
  1849. (tfiledef(def_from).filetyp = ft_typed) and
  1850. (tfiledef(def_to).filetyp = ft_untyped) then
  1851. eq:=te_convert_l1;
  1852. end;
  1853. end;
  1854. end;
  1855. procedure para_allowed(var eq:tequaltype;p:tcallparanode;def_to:tdef);
  1856. var
  1857. acn: tarrayconstructornode;
  1858. realprocdef: tprocdef;
  1859. tmpeq: tequaltype;
  1860. begin
  1861. { Note: eq must be already valid, it will only be updated! }
  1862. case def_to.typ of
  1863. formaldef :
  1864. begin
  1865. { all types can be passed to a formaldef }
  1866. eq:=te_equal;
  1867. end;
  1868. stringdef :
  1869. begin
  1870. { to support ansi/long/wide strings in a proper way }
  1871. { string and string[10] are assumed as equal }
  1872. { when searching the correct overloaded procedure }
  1873. if (p.resultdef.typ=stringdef) and
  1874. (tstringdef(def_to).stringtype=tstringdef(p.resultdef).stringtype) and
  1875. (tstringdef(def_to).encoding=tstringdef(p.resultdef).encoding) then
  1876. eq:=te_equal
  1877. end;
  1878. setdef :
  1879. begin
  1880. { set can also be a not yet converted array constructor }
  1881. if (p.resultdef.typ=arraydef) and
  1882. is_array_constructor(p.resultdef) and
  1883. not is_variant_array(p.resultdef) then
  1884. eq:=te_equal;
  1885. end;
  1886. procvardef :
  1887. begin
  1888. tmpeq:=te_incompatible;
  1889. { in tp/macpas mode proc -> procvar is allowed }
  1890. if ((m_tp_procvar in current_settings.modeswitches) or
  1891. (m_mac_procvar in current_settings.modeswitches)) and
  1892. (p.left.nodetype=calln) then
  1893. tmpeq:=proc_to_procvar_equal(tprocdef(tcallnode(p.left).procdefinition),tprocvardef(def_to),false);
  1894. if (tmpeq=te_incompatible) and
  1895. (m_nested_procvars in current_settings.modeswitches) and
  1896. is_proc2procvar_load(p.left,realprocdef) then
  1897. tmpeq:=proc_to_procvar_equal(realprocdef,tprocvardef(def_to),false);
  1898. if (tmpeq=te_incompatible) and
  1899. (m_mac in current_settings.modeswitches) and
  1900. is_ambiguous_funcret_load(p.left,realprocdef) then
  1901. tmpeq:=proc_to_procvar_equal(realprocdef,tprocvardef(def_to),false);
  1902. if tmpeq<>te_incompatible then
  1903. eq:=tmpeq;
  1904. end;
  1905. arraydef :
  1906. begin
  1907. { an arrayconstructor of proccalls may have to be converted to
  1908. an array of procvars }
  1909. if ((m_tp_procvar in current_settings.modeswitches) or
  1910. (m_mac_procvar in current_settings.modeswitches)) and
  1911. (tarraydef(def_to).elementdef.typ=procvardef) and
  1912. is_array_constructor(p.resultdef) and
  1913. not is_variant_array(p.resultdef) then
  1914. begin
  1915. acn:=tarrayconstructornode(p.left);
  1916. if assigned(acn.left) then
  1917. begin
  1918. eq:=te_exact;
  1919. while assigned(acn) and
  1920. (eq<>te_incompatible) do
  1921. begin
  1922. if (acn.left.nodetype=calln) then
  1923. tmpeq:=proc_to_procvar_equal(tprocdef(tcallnode(acn.left).procdefinition),tprocvardef(tarraydef(def_to).elementdef),false)
  1924. else
  1925. tmpeq:=compare_defs(acn.left.resultdef,tarraydef(def_to).elementdef,acn.left.nodetype);
  1926. if tmpeq<eq then
  1927. eq:=tmpeq;
  1928. acn:=tarrayconstructornode(acn.right);
  1929. end;
  1930. end
  1931. end;
  1932. end;
  1933. end;
  1934. end;
  1935. function allowenumop(nt:tnodetype):boolean;
  1936. begin
  1937. result:=(nt in [equaln,unequaln,ltn,lten,gtn,gten]) or
  1938. ((cs_allow_enum_calc in current_settings.localswitches) and
  1939. (nt in [addn,subn]));
  1940. end;
  1941. {****************************************************************************
  1942. TCallCandidates
  1943. ****************************************************************************}
  1944. constructor tcallcandidates.create(sym:tprocsym;st:TSymtable;ppn:tnode;ignorevisibility,allowdefaultparas,objcidcall,explicitunit,searchhelpers,anoninherited:boolean);
  1945. begin
  1946. if not assigned(sym) then
  1947. internalerror(200411015);
  1948. FOperator:=NOTOKEN;
  1949. FProcsym:=sym;
  1950. FProcsymtable:=st;
  1951. FParanode:=ppn;
  1952. FIgnoredCandidateProcs:=tfpobjectlist.create(false);
  1953. create_candidate_list(ignorevisibility,allowdefaultparas,objcidcall,explicitunit,searchhelpers,anoninherited);
  1954. end;
  1955. constructor tcallcandidates.create_operator(op:ttoken;ppn:tnode);
  1956. begin
  1957. FOperator:=op;
  1958. FProcsym:=nil;
  1959. FProcsymtable:=nil;
  1960. FParanode:=ppn;
  1961. FIgnoredCandidateProcs:=tfpobjectlist.create(false);
  1962. create_candidate_list(false,false,false,false,false,false);
  1963. end;
  1964. destructor tcallcandidates.destroy;
  1965. var
  1966. hpnext,
  1967. hp : pcandidate;
  1968. begin
  1969. FIgnoredCandidateProcs.free;
  1970. hp:=FCandidateProcs;
  1971. while assigned(hp) do
  1972. begin
  1973. hpnext:=hp^.next;
  1974. dispose(hp);
  1975. hp:=hpnext;
  1976. end;
  1977. end;
  1978. procedure tcallcandidates.collect_overloads_in_struct(structdef:tabstractrecorddef;ProcdefOverloadList:TFPObjectList;searchhelpers,anoninherited:boolean);
  1979. function processprocsym(srsym:tprocsym; out foundanything: boolean):boolean;
  1980. var
  1981. j : integer;
  1982. pd : tprocdef;
  1983. begin
  1984. { add all definitions }
  1985. result:=false;
  1986. foundanything:=false;
  1987. for j:=0 to srsym.ProcdefList.Count-1 do
  1988. begin
  1989. pd:=tprocdef(srsym.ProcdefList[j]);
  1990. if (po_ignore_for_overload_resolution in pd.procoptions) then
  1991. begin
  1992. FIgnoredCandidateProcs.add(pd);
  1993. continue;
  1994. end;
  1995. { in case of anonymous inherited, only match procdefs identical
  1996. to the current one (apart from hidden parameters), rather than
  1997. anything compatible to the parameters -- except in case of
  1998. the presence of a messagestr/int, in which case those have to
  1999. match exactly }
  2000. if anoninherited then
  2001. if po_msgint in current_procinfo.procdef.procoptions then
  2002. begin
  2003. if not(po_msgint in pd.procoptions) or
  2004. (pd.messageinf.i<>current_procinfo.procdef.messageinf.i) then
  2005. continue
  2006. end
  2007. else if po_msgstr in current_procinfo.procdef.procoptions then
  2008. begin
  2009. if not(po_msgstr in pd.procoptions) or
  2010. (pd.messageinf.str^<>current_procinfo.procdef.messageinf.str^) then
  2011. continue
  2012. end
  2013. else if (compare_paras(current_procinfo.procdef.paras,pd.paras,cp_all,[cpo_ignorehidden])<te_equal) then
  2014. continue;
  2015. foundanything:=true;
  2016. { Store first procsym found }
  2017. if not assigned(FProcsym) then
  2018. FProcsym:=tprocsym(srsym);
  2019. if po_overload in pd.procoptions then
  2020. result:=true;
  2021. ProcdefOverloadList.Add(srsym.ProcdefList[j]);
  2022. end;
  2023. end;
  2024. var
  2025. srsym : tsym;
  2026. hashedid : THashedIDString;
  2027. hasoverload,
  2028. foundanything : boolean;
  2029. helperdef : tobjectdef;
  2030. begin
  2031. if FOperator=NOTOKEN then
  2032. hashedid.id:=FProcsym.name
  2033. else
  2034. hashedid.id:=overloaded_names[FOperator];
  2035. hasoverload:=false;
  2036. while assigned(structdef) do
  2037. begin
  2038. { first search in helpers for this type }
  2039. if (is_class(structdef) or is_record(structdef))
  2040. and searchhelpers then
  2041. begin
  2042. if search_last_objectpascal_helper(structdef,nil,helperdef) then
  2043. begin
  2044. srsym:=nil;
  2045. while assigned(helperdef) do
  2046. begin
  2047. srsym:=tsym(helperdef.symtable.FindWithHash(hashedid));
  2048. if assigned(srsym) and
  2049. { Delphi allows hiding a property by a procedure with the same name }
  2050. (srsym.typ=procsym) then
  2051. begin
  2052. hasoverload:=processprocsym(tprocsym(srsym),foundanything);
  2053. { when there is no explicit overload we stop searching }
  2054. if foundanything and
  2055. not hasoverload then
  2056. break;
  2057. end;
  2058. helperdef:=helperdef.childof;
  2059. end;
  2060. if not hasoverload and assigned(srsym) then
  2061. exit;
  2062. end;
  2063. end;
  2064. { now search in the type itself }
  2065. srsym:=tsym(structdef.symtable.FindWithHash(hashedid));
  2066. if assigned(srsym) and
  2067. { Delphi allows hiding a property by a procedure with the same name }
  2068. (srsym.typ=procsym) then
  2069. begin
  2070. hasoverload:=processprocsym(tprocsym(srsym),foundanything);
  2071. { when there is no explicit overload we stop searching }
  2072. if foundanything and
  2073. not hasoverload then
  2074. break;
  2075. end;
  2076. if is_objectpascal_helper(structdef) and
  2077. (tobjectdef(structdef).typ in [recorddef,objectdef]) then
  2078. begin
  2079. { search methods in the extended type as well }
  2080. srsym:=tprocsym(tabstractrecorddef(tobjectdef(structdef).extendeddef).symtable.FindWithHash(hashedid));
  2081. if assigned(srsym) and
  2082. { Delphi allows hiding a property by a procedure with the same name }
  2083. (srsym.typ=procsym) then
  2084. begin
  2085. hasoverload:=processprocsym(tprocsym(srsym),foundanything);
  2086. { when there is no explicit overload we stop searching }
  2087. if foundanything and
  2088. not hasoverload then
  2089. break;
  2090. end;
  2091. end;
  2092. { next parent }
  2093. if (structdef.typ=objectdef) then
  2094. structdef:=tobjectdef(structdef).childof
  2095. else
  2096. structdef:=nil;
  2097. end;
  2098. end;
  2099. procedure tcallcandidates.collect_overloads_in_units(ProcdefOverloadList:TFPObjectList; objcidcall,explicitunit: boolean);
  2100. var
  2101. j : integer;
  2102. pd : tprocdef;
  2103. srsymtable : TSymtable;
  2104. srsym : tsym;
  2105. checkstack : psymtablestackitem;
  2106. hashedid : THashedIDString;
  2107. hasoverload : boolean;
  2108. begin
  2109. { we search all overloaded operator definitions in the symtablestack. The found
  2110. entries are only added to the procs list and not the procsym, because
  2111. the list can change in every situation }
  2112. if FOperator=NOTOKEN then
  2113. begin
  2114. if not objcidcall then
  2115. hashedid.id:=FProcsym.name
  2116. else
  2117. hashedid.id:=class_helper_prefix+FProcsym.name;
  2118. end
  2119. else
  2120. hashedid.id:=overloaded_names[FOperator];
  2121. checkstack:=symtablestack.stack;
  2122. if assigned(FProcsymtable) then
  2123. begin
  2124. while assigned(checkstack) and
  2125. (checkstack^.symtable<>FProcsymtable) do
  2126. checkstack:=checkstack^.next;
  2127. end;
  2128. while assigned(checkstack) do
  2129. begin
  2130. srsymtable:=checkstack^.symtable;
  2131. { if the unit in which the routine has to be searched has been
  2132. specified explicitly, stop searching after its symtable(s) have
  2133. been checked (can be both the static and the global symtable
  2134. in case it's the current unit itself) }
  2135. if explicitunit and
  2136. (FProcsymtable.symtabletype in [globalsymtable,staticsymtable]) and
  2137. (srsymtable.moduleid<>FProcsymtable.moduleid) then
  2138. break;
  2139. if srsymtable.symtabletype in [localsymtable,staticsymtable,globalsymtable] then
  2140. begin
  2141. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  2142. if assigned(srsym) and
  2143. (srsym.typ=procsym) then
  2144. begin
  2145. { add all definitions }
  2146. hasoverload:=false;
  2147. for j:=0 to tprocsym(srsym).ProcdefList.Count-1 do
  2148. begin
  2149. pd:=tprocdef(tprocsym(srsym).ProcdefList[j]);
  2150. if (po_ignore_for_overload_resolution in pd.procoptions) then
  2151. begin
  2152. FIgnoredCandidateProcs.add(pd);
  2153. continue;
  2154. end;
  2155. { Store first procsym found }
  2156. if not assigned(FProcsym) then
  2157. FProcsym:=tprocsym(srsym);
  2158. if po_overload in pd.procoptions then
  2159. hasoverload:=true;
  2160. ProcdefOverloadList.Add(tprocsym(srsym).ProcdefList[j]);
  2161. end;
  2162. { when there is no explicit overload we stop searching,
  2163. except for Objective-C methods called via id }
  2164. if not hasoverload and
  2165. not objcidcall then
  2166. break;
  2167. end;
  2168. end;
  2169. checkstack:=checkstack^.next
  2170. end;
  2171. end;
  2172. procedure tcallcandidates.create_candidate_list(ignorevisibility,allowdefaultparas,objcidcall,explicitunit,searchhelpers,anoninherited:boolean);
  2173. var
  2174. j : integer;
  2175. pd : tprocdef;
  2176. hp : pcandidate;
  2177. pt : tcallparanode;
  2178. found : boolean;
  2179. st : TSymtable;
  2180. contextstructdef : tabstractrecorddef;
  2181. ProcdefOverloadList : TFPObjectList;
  2182. cpoptions : tcompare_paras_options;
  2183. begin
  2184. FCandidateProcs:=nil;
  2185. { Find all available overloads for this procsym }
  2186. ProcdefOverloadList:=TFPObjectList.Create(false);
  2187. if not objcidcall and
  2188. (FOperator=NOTOKEN) and
  2189. (FProcsym.owner.symtabletype in [objectsymtable,recordsymtable]) then
  2190. collect_overloads_in_struct(tabstractrecorddef(FProcsym.owner.defowner),ProcdefOverloadList,searchhelpers,anoninherited)
  2191. else
  2192. if (FOperator<>NOTOKEN) then
  2193. begin
  2194. { check operands and if they contain records then search in records,
  2195. then search in unit }
  2196. pt:=tcallparanode(FParaNode);
  2197. while assigned(pt) do
  2198. begin
  2199. if (pt.resultdef.typ=recorddef) then
  2200. collect_overloads_in_struct(tabstractrecorddef(pt.resultdef),ProcdefOverloadList,searchhelpers,anoninherited);
  2201. pt:=tcallparanode(pt.right);
  2202. end;
  2203. collect_overloads_in_units(ProcdefOverloadList,objcidcall,explicitunit);
  2204. end
  2205. else
  2206. collect_overloads_in_units(ProcdefOverloadList,objcidcall,explicitunit);
  2207. { determine length of parameter list.
  2208. for operators also enable the variant-operators if
  2209. a variant parameter is passed }
  2210. FParalength:=0;
  2211. FAllowVariant:=(FOperator=NOTOKEN);
  2212. pt:=tcallparanode(FParaNode);
  2213. while assigned(pt) do
  2214. begin
  2215. if (pt.resultdef.typ=variantdef) then
  2216. FAllowVariant:=true;
  2217. inc(FParalength);
  2218. pt:=tcallparanode(pt.right);
  2219. end;
  2220. { when the class passed is defined in this unit we
  2221. need to use the scope of that class. This is a trick
  2222. that can be used to access protected members in other
  2223. units. At least kylix supports it this way (PFV) }
  2224. if assigned(FProcSymtable) and
  2225. (
  2226. (FProcSymtable.symtabletype in [ObjectSymtable,recordsymtable]) or
  2227. ((FProcSymtable.symtabletype=withsymtable) and
  2228. (FProcSymtable.defowner.typ in [objectdef,recorddef]))
  2229. ) and
  2230. (FProcSymtable.defowner.owner.symtabletype in [globalsymtable,staticsymtable,objectsymtable,recordsymtable]) and
  2231. FProcSymtable.defowner.owner.iscurrentunit then
  2232. contextstructdef:=tabstractrecorddef(FProcSymtable.defowner)
  2233. else
  2234. contextstructdef:=current_structdef;
  2235. { symtable is needed later to calculate the distance }
  2236. if assigned(FProcsym) then
  2237. st:=FProcsym.Owner
  2238. else
  2239. st:=nil;
  2240. { Process all found overloads }
  2241. for j:=0 to ProcdefOverloadList.Count-1 do
  2242. begin
  2243. pd:=tprocdef(ProcdefOverloadList[j]);
  2244. { only when the # of parameter are supported by the procedure and
  2245. it is visible }
  2246. if (FParalength>=pd.minparacount) and
  2247. (
  2248. (
  2249. allowdefaultparas and
  2250. (
  2251. (FParalength<=pd.maxparacount) or
  2252. (po_varargs in pd.procoptions)
  2253. )
  2254. ) or
  2255. (
  2256. not allowdefaultparas and
  2257. (FParalength=pd.maxparacount)
  2258. )
  2259. ) and
  2260. (
  2261. ignorevisibility or
  2262. not (pd.owner.symtabletype in [objectsymtable,recordsymtable]) or
  2263. is_visible_for_object(pd,contextstructdef)
  2264. ) then
  2265. begin
  2266. { don't add duplicates, only compare visible parameters for the user }
  2267. cpoptions:=[cpo_ignorehidden];
  2268. if (po_compilerproc in pd.procoptions) then
  2269. cpoptions:=cpoptions+[cpo_compilerproc];
  2270. if (po_rtlproc in pd.procoptions) then
  2271. cpoptions:=cpoptions+[cpo_rtlproc];
  2272. found:=false;
  2273. hp:=FCandidateProcs;
  2274. while assigned(hp) do
  2275. begin
  2276. if (compare_paras(hp^.data.paras,pd.paras,cp_value_equal_const,cpoptions)>=te_equal) and
  2277. (not(po_objc in pd.procoptions) or
  2278. (pd.messageinf.str^=hp^.data.messageinf.str^)) then
  2279. begin
  2280. found:=true;
  2281. break;
  2282. end;
  2283. hp:=hp^.next;
  2284. end;
  2285. if not found then
  2286. proc_add(st,pd,objcidcall);
  2287. end;
  2288. end;
  2289. ProcdefOverloadList.Free;
  2290. end;
  2291. function tcallcandidates.proc_add(st:tsymtable;pd:tprocdef;objcidcall: boolean):pcandidate;
  2292. var
  2293. defaultparacnt : integer;
  2294. begin
  2295. { generate new candidate entry }
  2296. new(result);
  2297. fillchar(result^,sizeof(tcandidate),0);
  2298. result^.data:=pd;
  2299. result^.next:=FCandidateProcs;
  2300. FCandidateProcs:=result;
  2301. inc(FProccnt);
  2302. { Find last parameter, skip all default parameters
  2303. that are not passed. Ignore this skipping for varargs }
  2304. result^.firstparaidx:=pd.paras.count-1;
  2305. if not(po_varargs in pd.procoptions) then
  2306. begin
  2307. { ignore hidden parameters }
  2308. while (result^.firstparaidx>=0) and (vo_is_hidden_para in tparavarsym(pd.paras[result^.firstparaidx]).varoptions) do
  2309. dec(result^.firstparaidx);
  2310. defaultparacnt:=pd.maxparacount-FParalength;
  2311. if defaultparacnt>0 then
  2312. begin
  2313. if defaultparacnt>result^.firstparaidx+1 then
  2314. internalerror(200401141);
  2315. dec(result^.firstparaidx,defaultparacnt);
  2316. end;
  2317. end;
  2318. { Give a small penalty for overloaded methods not in
  2319. defined the current class/unit }
  2320. { when calling Objective-C methods via id.method, then the found
  2321. procsym will be inside an arbitrary ObjectSymtable, and we don't
  2322. want togive the methods of that particular objcclass precedence over
  2323. other methods, so instead check against the symtable in which this
  2324. objcclass is defined }
  2325. if objcidcall then
  2326. st:=st.defowner.owner;
  2327. if (st<>pd.owner) then
  2328. result^.ordinal_distance:=result^.ordinal_distance+1.0;
  2329. end;
  2330. procedure tcallcandidates.list(all:boolean);
  2331. var
  2332. hp : pcandidate;
  2333. begin
  2334. hp:=FCandidateProcs;
  2335. while assigned(hp) do
  2336. begin
  2337. if all or
  2338. (not hp^.invalid) then
  2339. MessagePos1(hp^.data.fileinfo,sym_h_param_list,hp^.data.fullprocname(false));
  2340. hp:=hp^.next;
  2341. end;
  2342. end;
  2343. {$ifdef EXTDEBUG}
  2344. procedure tcallcandidates.dump_info(lvl:longint);
  2345. function ParaTreeStr(p:tcallparanode):string;
  2346. begin
  2347. result:='';
  2348. while assigned(p) do
  2349. begin
  2350. if result<>'' then
  2351. result:=','+result;
  2352. result:=p.resultdef.typename+result;
  2353. p:=tcallparanode(p.right);
  2354. end;
  2355. end;
  2356. var
  2357. hp : pcandidate;
  2358. i : integer;
  2359. currpara : tparavarsym;
  2360. begin
  2361. if not CheckVerbosity(lvl) then
  2362. exit;
  2363. Comment(lvl+V_LineInfo,'Overloaded callnode: '+FProcsym.name+'('+ParaTreeStr(tcallparanode(FParaNode))+')');
  2364. hp:=FCandidateProcs;
  2365. while assigned(hp) do
  2366. begin
  2367. Comment(lvl,' '+hp^.data.fullprocname(false));
  2368. if (hp^.invalid) then
  2369. Comment(lvl,' invalid')
  2370. else
  2371. begin
  2372. Comment(lvl,' ex: '+tostr(hp^.exact_count)+
  2373. ' eq: '+tostr(hp^.equal_count)+
  2374. ' l1: '+tostr(hp^.cl1_count)+
  2375. ' l2: '+tostr(hp^.cl2_count)+
  2376. ' l3: '+tostr(hp^.cl3_count)+
  2377. ' l4: '+tostr(hp^.cl4_count)+
  2378. ' l5: '+tostr(hp^.cl5_count)+
  2379. ' l6: '+tostr(hp^.cl6_count)+
  2380. ' oper: '+tostr(hp^.coper_count)+
  2381. ' ord: '+realtostr(hp^.ordinal_distance));
  2382. { Print parameters in left-right order }
  2383. for i:=0 to hp^.data.paras.count-1 do
  2384. begin
  2385. currpara:=tparavarsym(hp^.data.paras[i]);
  2386. if not(vo_is_hidden_para in currpara.varoptions) then
  2387. Comment(lvl,' - '+currpara.vardef.typename+' : '+EqualTypeName[currpara.eqval]);
  2388. end;
  2389. end;
  2390. hp:=hp^.next;
  2391. end;
  2392. end;
  2393. {$endif EXTDEBUG}
  2394. procedure tcallcandidates.get_information;
  2395. var
  2396. hp : pcandidate;
  2397. currpara : tparavarsym;
  2398. paraidx : integer;
  2399. currparanr : byte;
  2400. rfh,rth : double;
  2401. objdef : tobjectdef;
  2402. def_from,
  2403. def_to : tdef;
  2404. currpt,
  2405. pt : tcallparanode;
  2406. eq : tequaltype;
  2407. convtype : tconverttype;
  2408. pdtemp,
  2409. pdoper : tprocdef;
  2410. releasecurrpt : boolean;
  2411. cdoptions : tcompare_defs_options;
  2412. n : tnode;
  2413. {$push}
  2414. {$r-}
  2415. {$q-}
  2416. const
  2417. inf=1.0/0.0;
  2418. {$pop}
  2419. begin
  2420. cdoptions:=[cdo_check_operator];
  2421. if FAllowVariant then
  2422. include(cdoptions,cdo_allow_variant);
  2423. { process all procs }
  2424. hp:=FCandidateProcs;
  2425. while assigned(hp) do
  2426. begin
  2427. { We compare parameters in reverse order (right to left),
  2428. the firstpara is already pointing to the last parameter
  2429. were we need to start comparing }
  2430. currparanr:=FParalength;
  2431. paraidx:=hp^.firstparaidx;
  2432. while (paraidx>=0) and (vo_is_hidden_para in tparavarsym(hp^.data.paras[paraidx]).varoptions) do
  2433. dec(paraidx);
  2434. pt:=tcallparanode(FParaNode);
  2435. while assigned(pt) and (paraidx>=0) do
  2436. begin
  2437. currpara:=tparavarsym(hp^.data.paras[paraidx]);
  2438. { currpt can be changed from loadn to calln when a procvar
  2439. is passed. This is to prevent that the change is permanent }
  2440. currpt:=pt;
  2441. releasecurrpt:=false;
  2442. { retrieve current parameter definitions to compares }
  2443. eq:=te_incompatible;
  2444. def_from:=currpt.resultdef;
  2445. def_to:=currpara.vardef;
  2446. if not(assigned(def_from)) then
  2447. internalerror(200212091);
  2448. if not(
  2449. assigned(def_to) or
  2450. ((po_varargs in hp^.data.procoptions) and
  2451. (currparanr>hp^.data.minparacount))
  2452. ) then
  2453. internalerror(200212092);
  2454. { Convert tp procvars when not expecting a procvar }
  2455. if (currpt.left.resultdef.typ=procvardef) and
  2456. not(def_to.typ in [procvardef,formaldef]) and
  2457. { Only convert to call when there is no overload or the return type
  2458. is equal to the expected type. }
  2459. (
  2460. (count=1) or
  2461. equal_defs(tprocvardef(currpt.left.resultdef).returndef,def_to)
  2462. ) and
  2463. { and if it doesn't require any parameters }
  2464. (tprocvardef(currpt.left.resultdef).minparacount=0) then
  2465. begin
  2466. releasecurrpt:=true;
  2467. currpt:=tcallparanode(pt.getcopy);
  2468. if maybe_call_procvar(currpt.left,true) then
  2469. begin
  2470. currpt.resultdef:=currpt.left.resultdef;
  2471. def_from:=currpt.left.resultdef;
  2472. end;
  2473. end;
  2474. { If we expect a procvar and the left is loadnode that
  2475. returns a procdef we need to find the correct overloaded
  2476. procdef that matches the expected procvar. The loadnode
  2477. temporary returned the first procdef (PFV) }
  2478. if (def_to.typ=procvardef) and
  2479. (currpt.left.nodetype=loadn) and
  2480. (currpt.left.resultdef.typ=procdef) then
  2481. begin
  2482. pdtemp:=tprocsym(Tloadnode(currpt.left).symtableentry).Find_procdef_byprocvardef(Tprocvardef(def_to));
  2483. if assigned(pdtemp) then
  2484. begin
  2485. tloadnode(currpt.left).setprocdef(pdtemp);
  2486. currpt.resultdef:=currpt.left.resultdef;
  2487. def_from:=currpt.left.resultdef;
  2488. end;
  2489. end;
  2490. { varargs are always equal, but not exact }
  2491. if (po_varargs in hp^.data.procoptions) and
  2492. (currparanr>hp^.data.minparacount) and
  2493. not is_array_of_const(def_from) and
  2494. not is_array_constructor(def_from) then
  2495. eq:=te_equal
  2496. else
  2497. { same definition -> exact }
  2498. if (def_from=def_to) then
  2499. eq:=te_exact
  2500. else
  2501. { for value and const parameters check if a integer is constant or
  2502. included in other integer -> equal and calc ordinal_distance }
  2503. if not(currpara.varspez in [vs_var,vs_out]) and
  2504. is_integer(def_from) and
  2505. is_integer(def_to) and
  2506. is_in_limit(def_from,def_to) then
  2507. begin
  2508. eq:=te_equal;
  2509. hp^.ordinal_distance:=hp^.ordinal_distance+
  2510. abs(bestreal(torddef(def_from).low)-bestreal(torddef(def_to).low));
  2511. rth:=bestreal(torddef(def_to).high);
  2512. rfh:=bestreal(torddef(def_from).high);
  2513. hp^.ordinal_distance:=hp^.ordinal_distance+abs(rth-rfh);
  2514. { Give wrong sign a small penalty, this is need to get a diffrence
  2515. from word->[longword,longint] }
  2516. if is_signed(def_from)<>is_signed(def_to) then
  2517. {$push}
  2518. {$r-}
  2519. {$q-}
  2520. hp^.ordinal_distance:=nextafter(hp^.ordinal_distance,inf);
  2521. {$pop}
  2522. end
  2523. else
  2524. { for value and const parameters check precision of real, give
  2525. penalty for loosing of precision. var and out parameters must match exactly }
  2526. if not(currpara.varspez in [vs_var,vs_out]) and
  2527. is_real(def_from) and
  2528. is_real(def_to) then
  2529. begin
  2530. eq:=te_equal;
  2531. if is_extended(def_to) then
  2532. rth:=4
  2533. else
  2534. if is_double (def_to) then
  2535. rth:=2
  2536. else
  2537. rth:=1;
  2538. if is_extended(def_from) then
  2539. rfh:=4
  2540. else
  2541. if is_double (def_from) then
  2542. rfh:=2
  2543. else
  2544. rfh:=1;
  2545. { penalty for shrinking of precision }
  2546. if rth<rfh then
  2547. rfh:=(rfh-rth)*16
  2548. else
  2549. rfh:=rth-rfh;
  2550. hp^.ordinal_distance:=hp^.ordinal_distance+rfh;
  2551. end
  2552. else
  2553. { related object parameters also need to determine the distance between the current
  2554. object and the object we are comparing with. var and out parameters must match exactly }
  2555. if not(currpara.varspez in [vs_var,vs_out]) and
  2556. (def_from.typ=objectdef) and
  2557. (def_to.typ=objectdef) and
  2558. (tobjectdef(def_from).objecttype=tobjectdef(def_to).objecttype) and
  2559. tobjectdef(def_from).is_related(tobjectdef(def_to)) then
  2560. begin
  2561. eq:=te_convert_l1;
  2562. objdef:=tobjectdef(def_from);
  2563. while assigned(objdef) do
  2564. begin
  2565. if objdef=def_to then
  2566. break;
  2567. hp^.ordinal_distance:=hp^.ordinal_distance+1;
  2568. objdef:=objdef.childof;
  2569. end;
  2570. end
  2571. { compare_defs_ext compares sets and array constructors very poorly because
  2572. it has too little information. So we do explicitly a detailed comparisation,
  2573. see also bug #11288 (FK)
  2574. }
  2575. else if (def_to.typ=setdef) and is_array_constructor(currpt.left.resultdef) then
  2576. begin
  2577. n:=currpt.left.getcopy;
  2578. arrayconstructor_to_set(n);
  2579. eq:=compare_defs_ext(n.resultdef,def_to,n.nodetype,convtype,pdoper,cdoptions);
  2580. n.free;
  2581. end
  2582. else
  2583. { generic type comparision }
  2584. begin
  2585. if (hp^.data.procoptions*[po_rtlproc,po_compilerproc]=[]) and
  2586. is_ansistring(def_from) and
  2587. is_ansistring(def_to) and
  2588. (tstringdef(def_from).encoding<>tstringdef(def_to).encoding) and
  2589. (currpara.varspez in [vs_var,vs_out]) then
  2590. eq:=te_convert_l1 // don't allow to pass different ansistring types to each-other
  2591. else
  2592. eq:=compare_defs_ext(def_from,def_to,currpt.left.nodetype,convtype,pdoper,cdoptions);
  2593. { when the types are not equal we need to check
  2594. some special case for parameter passing }
  2595. if (eq<te_equal) then
  2596. begin
  2597. if currpara.varspez in [vs_var,vs_out] then
  2598. begin
  2599. { para requires an equal type so the previous found
  2600. match was not good enough, reset to incompatible }
  2601. eq:=te_incompatible;
  2602. { var_para_allowed will return te_equal and te_convert_l1 to
  2603. make a difference for best matching }
  2604. var_para_allowed(eq,currpt.resultdef,currpara.vardef,currpt.left)
  2605. end
  2606. else
  2607. para_allowed(eq,currpt,def_to);
  2608. end;
  2609. end;
  2610. { univ parameters match if the size matches (don't override the
  2611. comparison result if it was ok, since a match based on the
  2612. "univ" character is the lowest possible match) }
  2613. if (eq=te_incompatible) and
  2614. currpara.univpara and
  2615. is_valid_univ_para_type(def_from) and
  2616. (def_from.size=def_to.size) then
  2617. eq:=te_convert_l5;
  2618. { when a procvar was changed to a call an exact match is
  2619. downgraded to equal. This way an overload call with the
  2620. procvar is choosen. See tb0471 (PFV) }
  2621. if (pt<>currpt) and (eq=te_exact) then
  2622. eq:=te_equal;
  2623. { increase correct counter }
  2624. case eq of
  2625. te_exact :
  2626. inc(hp^.exact_count);
  2627. te_equal :
  2628. inc(hp^.equal_count);
  2629. te_convert_l1 :
  2630. inc(hp^.cl1_count);
  2631. te_convert_l2 :
  2632. inc(hp^.cl2_count);
  2633. te_convert_l3 :
  2634. inc(hp^.cl3_count);
  2635. te_convert_l4 :
  2636. inc(hp^.cl4_count);
  2637. te_convert_l5 :
  2638. inc(hp^.cl5_count);
  2639. te_convert_l6 :
  2640. inc(hp^.cl6_count);
  2641. te_convert_operator :
  2642. inc(hp^.coper_count);
  2643. te_incompatible :
  2644. hp^.invalid:=true;
  2645. else
  2646. internalerror(200212072);
  2647. end;
  2648. { stop checking when an incompatible parameter is found }
  2649. if hp^.invalid then
  2650. begin
  2651. { store the current parameter info for
  2652. a nice error message when no procedure is found }
  2653. hp^.wrongparaidx:=paraidx;
  2654. hp^.wrongparanr:=currparanr;
  2655. break;
  2656. end;
  2657. {$ifdef EXTDEBUG}
  2658. { store equal in node tree for dump }
  2659. currpara.eqval:=eq;
  2660. {$endif EXTDEBUG}
  2661. { maybe release temp currpt }
  2662. if releasecurrpt then
  2663. currpt.free;
  2664. { next parameter in the call tree }
  2665. pt:=tcallparanode(pt.right);
  2666. { next parameter for definition, only goto next para
  2667. if we're out of the varargs }
  2668. if not(po_varargs in hp^.data.procoptions) or
  2669. (currparanr<=hp^.data.maxparacount) then
  2670. begin
  2671. { Ignore vs_hidden parameters }
  2672. repeat
  2673. dec(paraidx);
  2674. until (paraidx<0) or not(vo_is_hidden_para in tparavarsym(hp^.data.paras[paraidx]).varoptions);
  2675. end;
  2676. dec(currparanr);
  2677. end;
  2678. if not(hp^.invalid) and
  2679. (assigned(pt) or (paraidx>=0) or (currparanr<>0)) then
  2680. internalerror(200212141);
  2681. { next candidate }
  2682. hp:=hp^.next;
  2683. end;
  2684. end;
  2685. function get_variantequaltype(def: tdef): tvariantequaltype;
  2686. const
  2687. variantorddef_cl: array[tordtype] of tvariantequaltype =
  2688. (tve_incompatible,tve_byte,tve_word,tve_cardinal,tve_chari64,
  2689. tve_shortint,tve_smallint,tve_longint,tve_chari64,
  2690. tve_boolformal,tve_boolformal,tve_boolformal,tve_boolformal,
  2691. tve_boolformal,tve_boolformal,tve_boolformal,tve_boolformal,
  2692. tve_chari64,tve_chari64,tve_dblcurrency);
  2693. { TODO: fixme for 128 bit floats }
  2694. variantfloatdef_cl: array[tfloattype] of tvariantequaltype =
  2695. (tve_single,tve_dblcurrency,tve_extended,tve_extended,
  2696. tve_dblcurrency,tve_dblcurrency,tve_extended);
  2697. variantstringdef_cl: array[tstringtype] of tvariantequaltype =
  2698. (tve_sstring,tve_astring,tve_astring,tve_wstring,tve_ustring);
  2699. begin
  2700. case def.typ of
  2701. orddef:
  2702. begin
  2703. result:=variantorddef_cl[torddef(def).ordtype];
  2704. end;
  2705. floatdef:
  2706. begin
  2707. result:=variantfloatdef_cl[tfloatdef(def).floattype];
  2708. end;
  2709. stringdef:
  2710. begin
  2711. result:=variantstringdef_cl[tstringdef(def).stringtype];
  2712. end;
  2713. formaldef:
  2714. begin
  2715. result:=tve_boolformal;
  2716. end;
  2717. else
  2718. begin
  2719. result:=tve_incompatible;
  2720. end;
  2721. end
  2722. end;
  2723. function is_better_candidate(currpd,bestpd:pcandidate):integer;
  2724. var
  2725. res : integer;
  2726. begin
  2727. {
  2728. Return values:
  2729. > 0 when currpd is better than bestpd
  2730. < 0 when bestpd is better than currpd
  2731. = 0 when both are equal
  2732. To choose the best candidate we use the following order:
  2733. - Incompatible flag
  2734. - (Smaller) Number of convert operator parameters.
  2735. - (Smaller) Number of convertlevel 2 parameters.
  2736. - (Smaller) Number of convertlevel 1 parameters.
  2737. - (Bigger) Number of exact parameters.
  2738. - (Smaller) Number of equal parameters.
  2739. - (Smaller) Total of ordinal distance. For example, the distance of a word
  2740. to a byte is 65535-255=65280.
  2741. }
  2742. if bestpd^.invalid then
  2743. begin
  2744. if currpd^.invalid then
  2745. res:=0
  2746. else
  2747. res:=1;
  2748. end
  2749. else
  2750. if currpd^.invalid then
  2751. res:=-1
  2752. else
  2753. begin
  2754. { less operator parameters? }
  2755. res:=(bestpd^.coper_count-currpd^.coper_count);
  2756. if (res=0) then
  2757. begin
  2758. { less cl6 parameters? }
  2759. res:=(bestpd^.cl6_count-currpd^.cl6_count);
  2760. if (res=0) then
  2761. begin
  2762. { less cl5 parameters? }
  2763. res:=(bestpd^.cl5_count-currpd^.cl5_count);
  2764. if (res=0) then
  2765. begin
  2766. { less cl4 parameters? }
  2767. res:=(bestpd^.cl4_count-currpd^.cl4_count);
  2768. if (res=0) then
  2769. begin
  2770. { less cl3 parameters? }
  2771. res:=(bestpd^.cl3_count-currpd^.cl3_count);
  2772. if (res=0) then
  2773. begin
  2774. { less cl2 parameters? }
  2775. res:=(bestpd^.cl2_count-currpd^.cl2_count);
  2776. if (res=0) then
  2777. begin
  2778. { less cl1 parameters? }
  2779. res:=(bestpd^.cl1_count-currpd^.cl1_count);
  2780. if (res=0) then
  2781. begin
  2782. { more exact parameters? }
  2783. res:=(currpd^.exact_count-bestpd^.exact_count);
  2784. if (res=0) then
  2785. begin
  2786. { less equal parameters? }
  2787. res:=(bestpd^.equal_count-currpd^.equal_count);
  2788. if (res=0) then
  2789. begin
  2790. { smaller ordinal distance? }
  2791. if (currpd^.ordinal_distance<bestpd^.ordinal_distance) then
  2792. res:=1
  2793. else
  2794. if (currpd^.ordinal_distance>bestpd^.ordinal_distance) then
  2795. res:=-1
  2796. else
  2797. res:=0;
  2798. end;
  2799. end;
  2800. end;
  2801. end;
  2802. end;
  2803. end;
  2804. end;
  2805. end;
  2806. end;
  2807. end;
  2808. is_better_candidate:=res;
  2809. end;
  2810. { Delphi precedence rules extracted from test programs. Only valid if passing
  2811. a variant parameter to overloaded procedures expecting exactly one parameter.
  2812. single > (char, currency, int64, shortstring, ansistring, widestring, unicodestring, extended, double)
  2813. double/currency > (char, int64, shortstring, ansistring, widestring, unicodestring, extended)
  2814. extended > (char, int64, shortstring, ansistring, widestring, unicodestring)
  2815. longint/cardinal > (int64, shortstring, ansistring, widestring, unicodestring, extended, double, single, char, currency)
  2816. smallint > (longint, int64, shortstring, ansistring, widestring, unicodestring, extended, double single, char, currency);
  2817. word > (longint, cardinal, int64, shortstring, ansistring, widestring, unicodestring, extended, double single, char, currency);
  2818. shortint > (longint, smallint, int64, shortstring, ansistring, widestring, unicodestring, extended, double, single, char, currency)
  2819. byte > (longint, cardinal, word, smallint, int64, shortstring, ansistring, widestring, unicodestring, extended, double, single, char, currency);
  2820. boolean/formal > (char, int64, shortstring, ansistring, widestring, unicodestring)
  2821. widestring > (char, int64, shortstring, ansistring, unicodestring)
  2822. unicodestring > (char, int64, shortstring, ansistring)
  2823. ansistring > (char, int64, shortstring)
  2824. shortstring > (char, int64)
  2825. Relations not mentioned mean that they conflict: no decision possible }
  2826. function is_better_candidate_single_variant(currpd,bestpd:pcandidate):integer;
  2827. function calculate_relation(const currvcl, bestvcl, testvcl:
  2828. tvariantequaltype; const conflictvcls: tvariantequaltypes):integer;
  2829. begin
  2830. { if (bestvcl=conflictvcl) or
  2831. (currvcl=conflictvcl) then
  2832. result:=0
  2833. else if (bestvcl=testvcl) then
  2834. result:=-1
  2835. else result:=1 }
  2836. result:=1-2*ord(bestvcl=testvcl)+
  2837. ord(currvcl in conflictvcls)-ord(bestvcl in conflictvcls);
  2838. end;
  2839. function getfirstrealparaidx(pd: pcandidate): integer;
  2840. begin
  2841. { can be different for currpd and bestpd in case of overloaded }
  2842. { functions, e.g. lowercase():char and lowercase():shortstring }
  2843. { (depending on the calling convention and parameter order) }
  2844. result:=pd^.firstparaidx;
  2845. while (result>=0) and (vo_is_hidden_para in tparavarsym(pd^.data.paras[result]).varoptions) do
  2846. dec(result);
  2847. if (vo_is_hidden_para in tparavarsym(pd^.data.paras[result]).varoptions) then
  2848. internalerror(2006122803);
  2849. end;
  2850. var
  2851. currpara, bestpara: tparavarsym;
  2852. currvcl, bestvcl: tvariantequaltype;
  2853. begin
  2854. {
  2855. Return values:
  2856. > 0 when currpd is better than bestpd
  2857. < 0 when bestpd is better than currpd
  2858. = 0 when both are equal
  2859. }
  2860. currpara:=tparavarsym(currpd^.data.paras[getfirstrealparaidx(currpd)]);
  2861. bestpara:=tparavarsym(bestpd^.data.paras[getfirstrealparaidx(bestpd)]);
  2862. { if one of the parameters is a regular variant, fall back to the }
  2863. { default algorithm }
  2864. if (currpara.vardef.typ = variantdef) or
  2865. (bestpara.vardef.typ = variantdef) then
  2866. begin
  2867. result:=is_better_candidate(currpd,bestpd);
  2868. exit;
  2869. end;
  2870. currvcl:=get_variantequaltype(currpara.vardef);
  2871. bestvcl:=get_variantequaltype(bestpara.vardef);
  2872. { sanity check }
  2873. result:=-5;
  2874. { if both are the same, there is a conflict }
  2875. if (currvcl=bestvcl) then
  2876. result:=0
  2877. { if one of the two cannot be used as variant, the other is better }
  2878. else if (bestvcl=tve_incompatible) then
  2879. result:=1
  2880. else if (currvcl=tve_incompatible) then
  2881. result:=-1
  2882. { boolean and formal are better than chari64str, but conflict with }
  2883. { everything else }
  2884. else if (currvcl=tve_boolformal) or
  2885. (bestvcl=tve_boolformal) then
  2886. if (currvcl=tve_boolformal) then
  2887. result:=ord(bestvcl in [tve_chari64,tve_sstring,tve_astring,tve_wstring,tve_ustring])
  2888. else
  2889. result:=-ord(currvcl in [tve_chari64,tve_sstring,tve_astring,tve_wstring,tve_ustring])
  2890. { byte is better than everything else (we assume both aren't byte, }
  2891. { since there's only one parameter and that one can't be the same) }
  2892. else if (currvcl=tve_byte) or
  2893. (bestvcl=tve_byte) then
  2894. result:=calculate_relation(currvcl,bestvcl,tve_byte,[tve_shortint])
  2895. { shortint conflicts with word and cardinal, but is better than }
  2896. { everything else but byte (which has already been handled) }
  2897. else if (currvcl=tve_shortint) or
  2898. (bestvcl=tve_shortint) then
  2899. result:=calculate_relation(currvcl,bestvcl,tve_shortint,[tve_word, tve_cardinal])
  2900. { word conflicts with smallint, but is better than everything else }
  2901. { but shortint and byte (which has already been handled) }
  2902. else if (currvcl=tve_word) or
  2903. (bestvcl=tve_word) then
  2904. result:=calculate_relation(currvcl,bestvcl,tve_word,[tve_smallint])
  2905. { smallint conflicts with cardinal, but is better than everything }
  2906. { which has not yet been tested }
  2907. else if (currvcl=tve_smallint) or
  2908. (bestvcl=tve_smallint) then
  2909. result:=calculate_relation(currvcl,bestvcl,tve_smallint,[tve_cardinal])
  2910. { cardinal conflicts with each longint and is better than everything }
  2911. { which has not yet been tested }
  2912. else if (currvcl=tve_cardinal) or
  2913. (bestvcl=tve_cardinal) then
  2914. result:=calculate_relation(currvcl,bestvcl,tve_cardinal,[tve_longint])
  2915. { longint is better than everything which has not yet been tested }
  2916. else if (currvcl=tve_longint) or
  2917. (bestvcl=tve_longint) then
  2918. { if bestvcl=tve_longint then
  2919. result:=-1
  2920. else
  2921. result:=1 }
  2922. result:=1-2*ord(bestvcl=tve_longint)
  2923. { single is better than everything left }
  2924. else if (currvcl=tve_single) or
  2925. (bestvcl=tve_single) then
  2926. result:=1-2*ord(bestvcl=tve_single)
  2927. { double/comp/currency are better than everything left, and conflict }
  2928. { with each other (but that's already tested) }
  2929. else if (currvcl=tve_dblcurrency) or
  2930. (bestvcl=tve_dblcurrency) then
  2931. result:=1-2*ord(bestvcl=tve_dblcurrency)
  2932. { extended is better than everything left }
  2933. else if (currvcl=tve_extended) or
  2934. (bestvcl=tve_extended) then
  2935. result:=1-2*ord(bestvcl=tve_extended)
  2936. { widestring is better than everything left }
  2937. else if (currvcl=tve_wstring) or
  2938. (bestvcl=tve_wstring) then
  2939. result:=1-2*ord(bestvcl=tve_wstring)
  2940. { unicodestring is better than everything left }
  2941. else if (currvcl=tve_ustring) or
  2942. (bestvcl=tve_ustring) then
  2943. result:=1-2*ord(bestvcl=tve_ustring)
  2944. { ansistring is better than everything left }
  2945. else if (currvcl=tve_astring) or
  2946. (bestvcl=tve_astring) then
  2947. result:=1-2*ord(bestvcl=tve_astring)
  2948. { shortstring is better than everything left }
  2949. else if (currvcl=tve_sstring) or
  2950. (bestvcl=tve_sstring) then
  2951. result:=1-2*ord(bestvcl=tve_sstring);
  2952. { all possibilities should have been checked now }
  2953. if (result=-5) then
  2954. internalerror(2006122805);
  2955. end;
  2956. function tcallcandidates.choose_best(var bestpd:tabstractprocdef; singlevariant: boolean):integer;
  2957. var
  2958. pd: tprocdef;
  2959. besthpstart,
  2960. hp : pcandidate;
  2961. cntpd,
  2962. res : integer;
  2963. begin
  2964. {
  2965. Returns the number of candidates left and the
  2966. first candidate is returned in pdbest
  2967. }
  2968. { Setup the first procdef as best, only count it as a result
  2969. when it is valid }
  2970. bestpd:=FCandidateProcs^.data;
  2971. if FCandidateProcs^.invalid then
  2972. cntpd:=0
  2973. else
  2974. cntpd:=1;
  2975. if assigned(FCandidateProcs^.next) then
  2976. begin
  2977. besthpstart:=FCandidateProcs;
  2978. hp:=FCandidateProcs^.next;
  2979. while assigned(hp) do
  2980. begin
  2981. if not singlevariant then
  2982. res:=is_better_candidate(hp,besthpstart)
  2983. else
  2984. res:=is_better_candidate_single_variant(hp,besthpstart);
  2985. if (res>0) then
  2986. begin
  2987. { hp is better, flag all procs to be incompatible }
  2988. while (besthpstart<>hp) do
  2989. begin
  2990. besthpstart^.invalid:=true;
  2991. besthpstart:=besthpstart^.next;
  2992. end;
  2993. { besthpstart is already set to hp }
  2994. bestpd:=besthpstart^.data;
  2995. cntpd:=1;
  2996. end
  2997. else
  2998. if (res<0) then
  2999. begin
  3000. { besthpstart is better, flag current hp to be incompatible }
  3001. hp^.invalid:=true;
  3002. end
  3003. else
  3004. begin
  3005. { res=0, both are valid }
  3006. if not hp^.invalid then
  3007. inc(cntpd);
  3008. end;
  3009. hp:=hp^.next;
  3010. end;
  3011. end;
  3012. { if we've found one, check the procdefs ignored for overload choosing
  3013. to see whether they contain one from a child class with the same
  3014. parameters (so the overload choosing was not influenced by their
  3015. presence, but now that we've decided which overloaded version to call,
  3016. make sure we call the version closest in terms of visibility }
  3017. if cntpd=1 then
  3018. begin
  3019. for res:=0 to FIgnoredCandidateProcs.count-1 do
  3020. begin
  3021. pd:=tprocdef(FIgnoredCandidateProcs[res]);
  3022. { stop searching when we start comparing methods of parent of
  3023. the struct in which the current best method was found }
  3024. if assigned(pd.struct) and
  3025. (pd.struct<>tprocdef(bestpd).struct) and
  3026. tprocdef(bestpd).struct.is_related(pd.struct) then
  3027. break;
  3028. if (pd.proctypeoption=bestpd.proctypeoption) and
  3029. ((pd.procoptions*[po_classmethod,po_methodpointer])=(bestpd.procoptions*[po_classmethod,po_methodpointer])) and
  3030. (compare_paras(pd.paras,bestpd.paras,cp_all,[cpo_ignorehidden,cpo_ignoreuniv,cpo_openequalisexact])=te_exact) then
  3031. begin
  3032. { first one encountered is closest in terms of visibility }
  3033. bestpd:=pd;
  3034. break;
  3035. end;
  3036. end;
  3037. end;
  3038. result:=cntpd;
  3039. end;
  3040. procedure tcallcandidates.find_wrong_para;
  3041. var
  3042. currparanr : smallint;
  3043. hp : pcandidate;
  3044. pt : tcallparanode;
  3045. wrongpara : tparavarsym;
  3046. begin
  3047. { Only process the first overloaded procdef }
  3048. hp:=FCandidateProcs;
  3049. { Find callparanode corresponding to the argument }
  3050. pt:=tcallparanode(FParanode);
  3051. currparanr:=FParalength;
  3052. while assigned(pt) and
  3053. (currparanr>hp^.wrongparanr) do
  3054. begin
  3055. pt:=tcallparanode(pt.right);
  3056. dec(currparanr);
  3057. end;
  3058. if (currparanr<>hp^.wrongparanr) or
  3059. not assigned(pt) then
  3060. internalerror(200212094);
  3061. { Show error message, when it was a var or out parameter
  3062. guess that it is a missing typeconv }
  3063. wrongpara:=tparavarsym(hp^.data.paras[hp^.wrongparaidx]);
  3064. if wrongpara.varspez in [vs_var,vs_out] then
  3065. begin
  3066. { Maybe passing the correct type but passing a const to var parameter }
  3067. if (compare_defs(pt.resultdef,wrongpara.vardef,pt.nodetype)<>te_incompatible) and
  3068. not valid_for_var(pt.left,true) then
  3069. CGMessagePos(pt.left.fileinfo,type_e_variable_id_expected)
  3070. else
  3071. CGMessagePos3(pt.left.fileinfo,parser_e_call_by_ref_without_typeconv,tostr(hp^.wrongparanr),
  3072. FullTypeName(pt.left.resultdef,wrongpara.vardef),
  3073. FullTypeName(wrongpara.vardef,pt.left.resultdef))
  3074. end
  3075. else
  3076. CGMessagePos3(pt.left.fileinfo,type_e_wrong_parameter_type,tostr(hp^.wrongparanr),
  3077. FullTypeName(pt.left.resultdef,wrongpara.vardef),
  3078. FullTypeName(wrongpara.vardef,pt.left.resultdef));
  3079. end;
  3080. procedure check_ranges(const location: tfileposinfo; source: tnode; destdef: tdef);
  3081. begin
  3082. if not(cs_check_ordinal_size in current_settings.localswitches) then
  3083. exit;
  3084. { check if the assignment may cause a range check error }
  3085. { if its not explicit, and only if the values are }
  3086. { ordinals, enumdef and floatdef }
  3087. if assigned(destdef) and
  3088. (destdef.typ in [enumdef,orddef,floatdef]) and
  3089. not is_boolean(destdef) and
  3090. assigned(source.resultdef) and
  3091. (source.resultdef.typ in [enumdef,orddef,floatdef]) and
  3092. not is_boolean(source.resultdef) and
  3093. not is_constrealnode(source) and
  3094. { constants are handled via regular range checking }
  3095. (source.nodetype<>ordconstn) then
  3096. begin
  3097. if ((destdef.size < source.resultdef.size) and
  3098. { s80real and sc80real have a different size but the same precision }
  3099. not((destdef.typ=floatdef) and
  3100. (source.resultdef.typ=floatdef) and
  3101. (tfloatdef(source.resultdef).floattype in [s80real,sc80real]) and
  3102. (tfloatdef(destdef).floattype in [s80real,sc80real]))) or
  3103. ((destdef.typ<>floatdef) and
  3104. (source.resultdef.typ<>floatdef) and
  3105. not is_in_limit(source.resultdef,destdef)) then
  3106. begin
  3107. if (cs_check_range in current_settings.localswitches) then
  3108. MessagePos(location,type_w_smaller_possible_range_check)
  3109. else
  3110. MessagePos(location,type_h_smaller_possible_range_check);
  3111. end;
  3112. end;
  3113. end;
  3114. function is_valid_for_default(def:tdef):boolean;
  3115. function is_valid_record_or_object(def:tabstractrecorddef):boolean;
  3116. var
  3117. sym : tsym;
  3118. i : longint;
  3119. begin
  3120. for i:=0 to def.symtable.symlist.count-1 do
  3121. begin
  3122. sym:=tsym(def.symtable.symlist[i]);
  3123. if sym.typ<>fieldvarsym then
  3124. continue;
  3125. if not is_valid_for_default(tfieldvarsym(sym).vardef) then
  3126. begin
  3127. result:=false;
  3128. exit;
  3129. end;
  3130. end;
  3131. result:=true;
  3132. end;
  3133. begin
  3134. case def.typ of
  3135. recorddef:
  3136. result:=is_valid_record_or_object(tabstractrecorddef(def));
  3137. objectdef:
  3138. if is_implicit_pointer_object_type(def) then
  3139. result:=true
  3140. else
  3141. if is_object(def) then
  3142. result:=is_valid_record_or_object(tabstractrecorddef(def))
  3143. else
  3144. result:=false;
  3145. arraydef:
  3146. if not (ado_isdynamicarray in tarraydef(def).arrayoptions) then
  3147. result:=is_valid_for_default(tarraydef(def).elementdef)
  3148. else
  3149. result:=true;
  3150. formaldef,
  3151. abstractdef,
  3152. filedef:
  3153. result:=false;
  3154. else
  3155. result:=true;
  3156. end;
  3157. end;
  3158. end.