htypechk.pas 133 KB

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