htypechk.pas 133 KB

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