htypechk.pas 144 KB

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