htypechk.pas 135 KB

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