htypechk.pas 132 KB

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