htypechk.pas 146 KB

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