htypechk.pas 141 KB

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