htypechk.pas 123 KB

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