htypechk.pas 137 KB

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