htypechk.pas 98 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595
  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. tokens,cpuinfo,
  22. node,globals,
  23. symconst,symtype,symdef,symsym,symbase;
  24. type
  25. Ttok2nodeRec=record
  26. tok : ttoken;
  27. nod : tnodetype;
  28. op_overloading_supported : boolean;
  29. end;
  30. pcandidate = ^tcandidate;
  31. tcandidate = record
  32. next : pcandidate;
  33. data : tprocdef;
  34. wrongparaidx,
  35. firstparaidx : integer;
  36. exact_count,
  37. equal_count,
  38. cl1_count,
  39. cl2_count,
  40. cl3_count,
  41. coper_count : integer; { should be signed }
  42. ordinal_distance : bestreal;
  43. invalid : boolean;
  44. wrongparanr : byte;
  45. end;
  46. tcallcandidates = class
  47. private
  48. FProcSym : tprocsym;
  49. FProcs : pcandidate;
  50. FProcVisibleCnt,
  51. FProcCnt : integer;
  52. FParaNode : tnode;
  53. FParaLength : smallint;
  54. FAllowVariant : boolean;
  55. function proc_add(ps:tprocsym;pd:tprocdef):pcandidate;
  56. public
  57. constructor create(sym:tprocsym;st:TSymtable;ppn:tnode;isprop,ignorevis : boolean);
  58. constructor create_operator(op:ttoken;ppn:tnode);
  59. destructor destroy;override;
  60. procedure list(all:boolean);
  61. {$ifdef EXTDEBUG}
  62. procedure dump_info(lvl:longint);
  63. {$endif EXTDEBUG}
  64. procedure get_information;
  65. function choose_best(var bestpd:tabstractprocdef; singlevariant: boolean):integer;
  66. procedure find_wrong_para;
  67. property Count:integer read FProcCnt;
  68. property VisibleCount:integer read FProcVisibleCnt;
  69. end;
  70. const
  71. tok2nodes=24;
  72. tok2node:array[1..tok2nodes] of ttok2noderec=(
  73. (tok:_PLUS ;nod:addn;op_overloading_supported:true), { binary overloading supported }
  74. (tok:_MINUS ;nod:subn;op_overloading_supported:true), { binary and unary overloading supported }
  75. (tok:_STAR ;nod:muln;op_overloading_supported:true), { binary overloading supported }
  76. (tok:_SLASH ;nod:slashn;op_overloading_supported:true), { binary overloading supported }
  77. (tok:_EQUAL ;nod:equaln;op_overloading_supported:true), { binary overloading supported }
  78. (tok:_GT ;nod:gtn;op_overloading_supported:true), { binary overloading supported }
  79. (tok:_LT ;nod:ltn;op_overloading_supported:true), { binary overloading supported }
  80. (tok:_GTE ;nod:gten;op_overloading_supported:true), { binary overloading supported }
  81. (tok:_LTE ;nod:lten;op_overloading_supported:true), { binary overloading supported }
  82. (tok:_SYMDIF ;nod:symdifn;op_overloading_supported:true), { binary overloading supported }
  83. (tok:_STARSTAR;nod:starstarn;op_overloading_supported:true), { binary overloading supported }
  84. (tok:_OP_AS ;nod:asn;op_overloading_supported:false), { binary overloading NOT supported }
  85. (tok:_OP_IN ;nod:inn;op_overloading_supported:false), { binary overloading NOT supported }
  86. (tok:_OP_IS ;nod:isn;op_overloading_supported:false), { binary overloading NOT supported }
  87. (tok:_OP_OR ;nod:orn;op_overloading_supported:true), { binary overloading supported }
  88. (tok:_OP_AND ;nod:andn;op_overloading_supported:true), { binary overloading supported }
  89. (tok:_OP_DIV ;nod:divn;op_overloading_supported:true), { binary overloading supported }
  90. (tok:_OP_NOT ;nod:notn;op_overloading_supported:true), { unary overloading supported }
  91. (tok:_OP_MOD ;nod:modn;op_overloading_supported:true), { binary overloading supported }
  92. (tok:_OP_SHL ;nod:shln;op_overloading_supported:true), { binary overloading supported }
  93. (tok:_OP_SHR ;nod:shrn;op_overloading_supported:true), { binary overloading supported }
  94. (tok:_OP_XOR ;nod:xorn;op_overloading_supported:true), { binary overloading supported }
  95. (tok:_ASSIGNMENT;nod:assignn;op_overloading_supported:true), { unary overloading supported }
  96. (tok:_UNEQUAL ;nod:unequaln;op_overloading_supported:false) { binary overloading NOT supported overload = instead }
  97. );
  98. const
  99. { firstcallparan without varspez we don't count the ref }
  100. {$ifdef extdebug}
  101. count_ref : boolean = true;
  102. {$endif def extdebug}
  103. allow_array_constructor : boolean = false;
  104. function node2opstr(nt:tnodetype):string;
  105. { check operator args and result type }
  106. function isbinaryoperatoroverloadable(treetyp:tnodetype;ld:tdef;lt:tnodetype;rd:tdef;rt:tnodetype) : boolean;
  107. function isoperatoracceptable(pf : tprocdef; optoken : ttoken) : boolean;
  108. function isunaryoverloaded(var t : tnode) : boolean;
  109. function isbinaryoverloaded(var t : tnode) : boolean;
  110. { Register Allocation }
  111. procedure make_not_regable(p : tnode; how: tvarregable);
  112. procedure calcregisters(p : tbinarynode;r32,fpu,mmx : word);
  113. { procvar handling }
  114. function is_procvar_load(p:tnode):boolean;
  115. procedure test_local_to_procvar(from_def:tprocvardef;to_def:tdef);
  116. { sets varsym varstate field correctly }
  117. type
  118. tvarstateflag = (vsf_must_be_valid,vsf_use_hints);
  119. tvarstateflags = set of tvarstateflag;
  120. procedure set_varstate(p:tnode;newstate:tvarstate;varstateflags:tvarstateflags);
  121. { sets the callunique flag, if the node is a vecn, }
  122. { takes care of type casts etc. }
  123. procedure set_unique(p : tnode);
  124. function valid_for_formal_var(p : tnode; report_errors: boolean) : boolean;
  125. function valid_for_formal_const(p : tnode; report_errors: boolean) : boolean;
  126. function valid_for_var(p:tnode; report_errors: boolean):boolean;
  127. function valid_for_assignment(p:tnode; report_errors: boolean):boolean;
  128. function valid_for_loopvar(p:tnode; report_errors: boolean):boolean;
  129. function valid_for_addr(p : tnode; report_errors: boolean) : boolean;
  130. function allowenumop(nt:tnodetype):boolean;
  131. procedure check_hints(const srsym: tsym; const symoptions: tsymoptions);
  132. procedure check_ranges(const location: tfileposinfo; source: tnode; destdef: tdef);
  133. implementation
  134. uses
  135. sysutils,
  136. globtype,systems,
  137. cutils,cclasses,verbose,
  138. symtable,
  139. defutil,defcmp,
  140. nbas,ncnv,nld,nmem,ncal,nmat,ninl,nutils,ncon,
  141. cgbase,procinfo
  142. ;
  143. type
  144. TValidAssign=(Valid_Property,Valid_Void,Valid_Const,Valid_Addr,Valid_Packed);
  145. TValidAssigns=set of TValidAssign;
  146. function node2opstr(nt:tnodetype):string;
  147. var
  148. i : integer;
  149. begin
  150. result:='<unknown>';
  151. for i:=1 to tok2nodes do
  152. if tok2node[i].nod=nt then
  153. begin
  154. result:=tokeninfo^[tok2node[i].tok].str;
  155. break;
  156. end;
  157. end;
  158. function isbinaryoperatoroverloadable(treetyp:tnodetype;ld:tdef;lt:tnodetype;rd:tdef;rt:tnodetype) : boolean;
  159. function internal_check(treetyp:tnodetype;ld:tdef;lt:tnodetype;rd:tdef;rt:tnodetype;var allowed:boolean):boolean;
  160. begin
  161. internal_check:=true;
  162. case ld.typ of
  163. formaldef,
  164. recorddef,
  165. variantdef :
  166. begin
  167. allowed:=true;
  168. end;
  169. procvardef :
  170. begin
  171. if (rd.typ in [pointerdef,procdef,procvardef]) then
  172. begin
  173. allowed:=false;
  174. exit;
  175. end;
  176. allowed:=true;
  177. end;
  178. pointerdef :
  179. begin
  180. if ((rd.typ in [orddef,enumdef,pointerdef,classrefdef,procvardef]) or
  181. is_class_or_interface(rd)) then
  182. begin
  183. allowed:=false;
  184. exit;
  185. end;
  186. { don't allow pchar+string }
  187. if (is_pchar(ld) or is_pwidechar(ld)) and
  188. ((rd.typ=stringdef) or
  189. is_pchar(rd) or
  190. is_pwidechar(rd) or
  191. is_chararray(rd) or
  192. is_widechararray(rd)) then
  193. begin
  194. allowed:=false;
  195. exit;
  196. end;
  197. allowed:=true;
  198. end;
  199. arraydef :
  200. begin
  201. { not vector/mmx }
  202. if ((cs_mmx in current_settings.localswitches) and
  203. is_mmx_able_array(ld)) or
  204. ((cs_support_vectors in current_settings.globalswitches) and
  205. is_vector(ld)) then
  206. begin
  207. allowed:=false;
  208. exit;
  209. end;
  210. { not chararray+[(wide)char,(wide)string,(wide)chararray] }
  211. if (is_chararray(ld) or is_widechararray(ld) or
  212. is_open_chararray(ld) or is_open_widechararray(ld))
  213. and
  214. ((rd.typ in [stringdef,orddef,enumdef]) or
  215. is_pchar(rd) or
  216. is_pwidechar(rd) or
  217. is_chararray(rd) or
  218. is_widechararray(rd) or
  219. is_open_chararray(rd) or
  220. is_open_widechararray(rd) or
  221. (rt=niln)) then
  222. begin
  223. allowed:=false;
  224. exit;
  225. end;
  226. { dynamic array compare with niln }
  227. if ((is_dynamic_array(ld) and
  228. (rt=niln)) or
  229. (is_dynamic_array(ld) and is_dynamic_array(rd)))
  230. and
  231. (treetyp in [equaln,unequaln]) then
  232. begin
  233. allowed:=false;
  234. exit;
  235. end;
  236. allowed:=true;
  237. end;
  238. objectdef :
  239. begin
  240. { <> and = are defined for classes }
  241. if (treetyp in [equaln,unequaln]) and
  242. is_class_or_interface(ld) then
  243. begin
  244. allowed:=false;
  245. exit;
  246. end;
  247. allowed:=true;
  248. end;
  249. stringdef :
  250. begin
  251. if (rd.typ in [orddef,enumdef,stringdef]) or
  252. is_pchar(rd) or
  253. is_pwidechar(rd) or
  254. is_chararray(rd) or
  255. is_widechararray(rd) or
  256. is_open_chararray(rd) or
  257. is_open_widechararray(rd) then
  258. begin
  259. allowed:=false;
  260. exit;
  261. end;
  262. allowed:=true;
  263. end;
  264. else
  265. internal_check:=false;
  266. end;
  267. end;
  268. var
  269. allowed : boolean;
  270. begin
  271. { power ** is always possible }
  272. if (treetyp=starstarn) then
  273. begin
  274. isbinaryoperatoroverloadable:=true;
  275. exit;
  276. end;
  277. { order of arguments does not matter so we have to check also
  278. the reversed order }
  279. allowed:=false;
  280. if not internal_check(treetyp,ld,lt,rd,rt,allowed) then
  281. internal_check(treetyp,rd,rt,ld,lt,allowed);
  282. isbinaryoperatoroverloadable:=allowed;
  283. end;
  284. function isunaryoperatoroverloadable(treetyp : tnodetype;ld : tdef) : boolean;
  285. begin
  286. result:=false;
  287. case treetyp of
  288. subn,
  289. unaryminusn :
  290. begin
  291. if (ld.typ in [orddef,enumdef,floatdef]) then
  292. exit;
  293. {$ifdef SUPPORT_MMX}
  294. if (cs_mmx in current_settings.localswitches) and
  295. is_mmx_able_array(ld) then
  296. exit;
  297. {$endif SUPPORT_MMX}
  298. result:=true;
  299. end;
  300. notn :
  301. begin
  302. if (ld.typ in [orddef,enumdef,floatdef]) then
  303. exit;
  304. {$ifdef SUPPORT_MMX}
  305. if (cs_mmx in current_settings.localswitches) and
  306. is_mmx_able_array(ld) then
  307. exit;
  308. {$endif SUPPORT_MMX}
  309. result:=true;
  310. end;
  311. end;
  312. end;
  313. function isoperatoracceptable(pf : tprocdef; optoken : ttoken) : boolean;
  314. var
  315. ld,rd : tdef;
  316. i : longint;
  317. eq : tequaltype;
  318. conv : tconverttype;
  319. pd : tprocdef;
  320. oldcount,
  321. count: longint;
  322. parasym : tparavarsym;
  323. begin
  324. result:=false;
  325. count := pf.parast.SymList.count;
  326. oldcount:=count;
  327. while count > 0 do
  328. begin
  329. parasym:=tparavarsym(pf.parast.SymList[count-1]);
  330. if is_boolean(parasym.vardef) then
  331. begin
  332. if parasym.name='RANGECHECK' then
  333. begin
  334. Include(parasym.varoptions, vo_is_hidden_para);
  335. Include(parasym.varoptions, vo_is_range_check);
  336. Dec(count);
  337. end
  338. else if parasym.name='OVERFLOWCHECK' then
  339. begin
  340. Include(parasym.varoptions, vo_is_hidden_para);
  341. Include(parasym.varoptions, vo_is_overflow_check);
  342. Dec(count);
  343. end
  344. else
  345. break;
  346. end
  347. else
  348. break;
  349. end;
  350. if count<>oldcount then
  351. pf.calcparas;
  352. case count of
  353. 1 : begin
  354. ld:=tparavarsym(pf.parast.SymList[0]).vardef;
  355. { assignment is a special case }
  356. if optoken=_ASSIGNMENT then
  357. begin
  358. eq:=compare_defs_ext(ld,pf.returndef,nothingn,conv,pd,[cdo_explicit]);
  359. result:=(eq=te_incompatible);
  360. end
  361. else
  362. begin
  363. for i:=1 to tok2nodes do
  364. if tok2node[i].tok=optoken then
  365. begin
  366. result:=
  367. tok2node[i].op_overloading_supported and
  368. isunaryoperatoroverloadable(tok2node[i].nod,ld);
  369. break;
  370. end;
  371. end;
  372. end;
  373. 2 : begin
  374. for i:=1 to tok2nodes do
  375. if tok2node[i].tok=optoken then
  376. begin
  377. ld:=tparavarsym(pf.parast.SymList[0]).vardef;
  378. rd:=tparavarsym(pf.parast.SymList[1]).vardef;
  379. result:=
  380. tok2node[i].op_overloading_supported and
  381. isbinaryoperatoroverloadable(tok2node[i].nod,ld,nothingn,rd,nothingn);
  382. break;
  383. end;
  384. end;
  385. end;
  386. end;
  387. function isunaryoverloaded(var t : tnode) : boolean;
  388. var
  389. ld : tdef;
  390. optoken : ttoken;
  391. operpd : tprocdef;
  392. ppn : tcallparanode;
  393. candidates : tcallcandidates;
  394. cand_cnt : integer;
  395. begin
  396. result:=false;
  397. operpd:=nil;
  398. { load easier access variables }
  399. ld:=tunarynode(t).left.resultdef;
  400. if not isunaryoperatoroverloadable(t.nodetype,ld) then
  401. exit;
  402. { operator overload is possible }
  403. result:=true;
  404. case t.nodetype of
  405. notn:
  406. optoken:=_OP_NOT;
  407. unaryminusn:
  408. optoken:=_MINUS;
  409. else
  410. begin
  411. CGMessage(parser_e_operator_not_overloaded);
  412. t:=cnothingnode.create;
  413. exit;
  414. end;
  415. end;
  416. { generate parameter nodes }
  417. ppn:=ccallparanode.create(tunarynode(t).left.getcopy,nil);
  418. ppn.get_paratype;
  419. candidates:=tcallcandidates.create_operator(optoken,ppn);
  420. { stop when there are no operators found }
  421. if candidates.count=0 then
  422. begin
  423. CGMessage(parser_e_operator_not_overloaded);
  424. candidates.free;
  425. ppn.free;
  426. t:=cnothingnode.create;
  427. exit;
  428. end;
  429. { Retrieve information about the candidates }
  430. candidates.get_information;
  431. {$ifdef EXTDEBUG}
  432. { Display info when multiple candidates are found }
  433. candidates.dump_info(V_Debug);
  434. {$endif EXTDEBUG}
  435. cand_cnt:=candidates.choose_best(operpd,false);
  436. { exit when no overloads are found }
  437. if cand_cnt=0 then
  438. begin
  439. CGMessage(parser_e_operator_not_overloaded);
  440. candidates.free;
  441. ppn.free;
  442. t:=cnothingnode.create;
  443. exit;
  444. end;
  445. { Multiple candidates left? }
  446. if cand_cnt>1 then
  447. begin
  448. CGMessage(type_e_cant_choose_overload_function);
  449. {$ifdef EXTDEBUG}
  450. candidates.dump_info(V_Hint);
  451. {$else EXTDEBUG}
  452. candidates.list(false);
  453. {$endif EXTDEBUG}
  454. { we'll just use the first candidate to make the
  455. call }
  456. end;
  457. candidates.free;
  458. addsymref(operpd.procsym);
  459. { the nil as symtable signs firstcalln that this is
  460. an overloaded operator }
  461. t:=ccallnode.create(ppn,Tprocsym(operpd.procsym),nil,nil,[]);
  462. { we already know the procdef to use, so it can
  463. skip the overload choosing in callnode.pass_typecheck }
  464. tcallnode(t).procdefinition:=operpd;
  465. end;
  466. function isbinaryoverloaded(var t : tnode) : boolean;
  467. var
  468. rd,ld : tdef;
  469. optoken : ttoken;
  470. operpd : tprocdef;
  471. ht : tnode;
  472. ppn : tcallparanode;
  473. candidates : tcallcandidates;
  474. cand_cnt : integer;
  475. begin
  476. isbinaryoverloaded:=false;
  477. operpd:=nil;
  478. { load easier access variables }
  479. ld:=tbinarynode(t).left.resultdef;
  480. rd:=tbinarynode(t).right.resultdef;
  481. if not isbinaryoperatoroverloadable(t.nodetype,ld,tbinarynode(t).left.nodetype,rd,tbinarynode(t).right.nodetype) then
  482. exit;
  483. { operator overload is possible }
  484. result:=true;
  485. case t.nodetype of
  486. equaln,
  487. unequaln :
  488. optoken:=_EQUAL;
  489. addn:
  490. optoken:=_PLUS;
  491. subn:
  492. optoken:=_MINUS;
  493. muln:
  494. optoken:=_STAR;
  495. starstarn:
  496. optoken:=_STARSTAR;
  497. slashn:
  498. optoken:=_SLASH;
  499. ltn:
  500. optoken:=_LT;
  501. gtn:
  502. optoken:=_GT;
  503. lten:
  504. optoken:=_LTE;
  505. gten:
  506. optoken:=_GTE;
  507. symdifn :
  508. optoken:=_SYMDIF;
  509. modn :
  510. optoken:=_OP_MOD;
  511. orn :
  512. optoken:=_OP_OR;
  513. xorn :
  514. optoken:=_OP_XOR;
  515. andn :
  516. optoken:=_OP_AND;
  517. divn :
  518. optoken:=_OP_DIV;
  519. shln :
  520. optoken:=_OP_SHL;
  521. shrn :
  522. optoken:=_OP_SHR;
  523. else
  524. begin
  525. CGMessage(parser_e_operator_not_overloaded);
  526. t:=cnothingnode.create;
  527. exit;
  528. end;
  529. end;
  530. { generate parameter nodes }
  531. ppn:=ccallparanode.create(tbinarynode(t).right.getcopy,ccallparanode.create(tbinarynode(t).left.getcopy,nil));
  532. ppn.get_paratype;
  533. candidates:=tcallcandidates.create_operator(optoken,ppn);
  534. { for commutative operators we can swap arguments and try again }
  535. if (candidates.count=0) and
  536. not(optoken in [_OP_SHL,_OP_SHR,_OP_DIV,_OP_MOD,_STARSTAR,_SLASH,_MINUS]) then
  537. begin
  538. candidates.free;
  539. reverseparameters(ppn);
  540. { reverse compare operators }
  541. case optoken of
  542. _LT:
  543. optoken:=_GTE;
  544. _GT:
  545. optoken:=_LTE;
  546. _LTE:
  547. optoken:=_GT;
  548. _GTE:
  549. optoken:=_LT;
  550. end;
  551. candidates:=tcallcandidates.create_operator(optoken,ppn);
  552. end;
  553. { stop when there are no operators found }
  554. if candidates.count=0 then
  555. begin
  556. CGMessage(parser_e_operator_not_overloaded);
  557. candidates.free;
  558. ppn.free;
  559. t:=cnothingnode.create;
  560. exit;
  561. end;
  562. { Retrieve information about the candidates }
  563. candidates.get_information;
  564. {$ifdef EXTDEBUG}
  565. { Display info when multiple candidates are found }
  566. candidates.dump_info(V_Debug);
  567. {$endif EXTDEBUG}
  568. cand_cnt:=candidates.choose_best(operpd,false);
  569. { exit when no overloads are found }
  570. if cand_cnt=0 then
  571. begin
  572. CGMessage(parser_e_operator_not_overloaded);
  573. candidates.free;
  574. ppn.free;
  575. t:=cnothingnode.create;
  576. exit;
  577. end;
  578. { Multiple candidates left? }
  579. if cand_cnt>1 then
  580. begin
  581. CGMessage(type_e_cant_choose_overload_function);
  582. {$ifdef EXTDEBUG}
  583. candidates.dump_info(V_Hint);
  584. {$else EXTDEBUG}
  585. candidates.list(false);
  586. {$endif EXTDEBUG}
  587. { we'll just use the first candidate to make the
  588. call }
  589. end;
  590. candidates.free;
  591. addsymref(operpd.procsym);
  592. { the nil as symtable signs firstcalln that this is
  593. an overloaded operator }
  594. ht:=ccallnode.create(ppn,Tprocsym(operpd.procsym),nil,nil,[]);
  595. { we already know the procdef to use, so it can
  596. skip the overload choosing in callnode.pass_typecheck }
  597. tcallnode(ht).procdefinition:=operpd;
  598. if t.nodetype=unequaln then
  599. ht:=cnotnode.create(ht);
  600. t:=ht;
  601. end;
  602. {****************************************************************************
  603. Register Calculation
  604. ****************************************************************************}
  605. { marks an lvalue as "unregable" }
  606. procedure make_not_regable_intern(p : tnode; how: tvarregable; records_only: boolean);
  607. begin
  608. case p.nodetype of
  609. subscriptn:
  610. make_not_regable_intern(tsubscriptnode(p).left,how,true);
  611. typeconvn :
  612. if (ttypeconvnode(p).resultdef.typ = recorddef) then
  613. make_not_regable_intern(ttypeconvnode(p).left,how,false)
  614. else
  615. make_not_regable_intern(ttypeconvnode(p).left,how,records_only);
  616. loadn :
  617. if (tloadnode(p).symtableentry.typ in [staticvarsym,localvarsym,paravarsym]) and
  618. (tabstractvarsym(tloadnode(p).symtableentry).varregable <> vr_none) and
  619. ((not records_only) or
  620. (tabstractvarsym(tloadnode(p).symtableentry).vardef.typ = recorddef)) then
  621. if (tloadnode(p).symtableentry.typ = paravarsym) then
  622. tabstractvarsym(tloadnode(p).symtableentry).varregable:=how
  623. else
  624. tabstractvarsym(tloadnode(p).symtableentry).varregable:=vr_none;
  625. temprefn :
  626. if (ttemprefnode(p).tempinfo^.may_be_in_reg) and
  627. ((not records_only) or
  628. (ttemprefnode(p).tempinfo^.typedef.typ = recorddef)) then
  629. ttemprefnode(p).tempinfo^.may_be_in_reg:=false;
  630. end;
  631. end;
  632. procedure make_not_regable(p : tnode; how: tvarregable);
  633. begin
  634. make_not_regable_intern(p,how,false);
  635. end;
  636. { calculates the needed registers for a binary operator }
  637. procedure calcregisters(p : tbinarynode;r32,fpu,mmx : word);
  638. begin
  639. p.left_right_max;
  640. { Only when the difference between the left and right registers < the
  641. wanted registers allocate the amount of registers }
  642. if assigned(p.left) then
  643. begin
  644. if assigned(p.right) then
  645. begin
  646. { the location must be already filled in because we need it to }
  647. { calculate the necessary number of registers (JM) }
  648. if p.expectloc = LOC_INVALID then
  649. internalerror(200110101);
  650. if (abs(p.left.registersint-p.right.registersint)<r32) or
  651. ((p.expectloc = LOC_FPUREGISTER) and
  652. (p.right.registersfpu <= p.left.registersfpu) and
  653. ((p.right.registersfpu <> 0) or (p.left.registersfpu <> 0)) and
  654. (p.left.registersint < p.right.registersint)) then
  655. inc(p.registersint,r32);
  656. if (abs(p.left.registersfpu-p.right.registersfpu)<fpu) then
  657. inc(p.registersfpu,fpu);
  658. {$ifdef SUPPORT_MMX}
  659. if (abs(p.left.registersmmx-p.right.registersmmx)<mmx) then
  660. inc(p.registersmmx,mmx);
  661. {$endif SUPPORT_MMX}
  662. { the following is a little bit guessing but I think }
  663. { it's the only way to solve same internalerrors: }
  664. { if the left and right node both uses registers }
  665. { and return a mem location, but the current node }
  666. { doesn't use an integer register we get probably }
  667. { trouble when restoring a node }
  668. if (p.left.registersint=p.right.registersint) and
  669. (p.registersint=p.left.registersint) and
  670. (p.registersint>0) and
  671. (p.left.expectloc in [LOC_REFERENCE,LOC_CREFERENCE]) and
  672. (p.right.expectloc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  673. inc(p.registersint);
  674. end
  675. else
  676. begin
  677. if (p.left.registersint<r32) then
  678. inc(p.registersint,r32);
  679. if (p.left.registersfpu<fpu) then
  680. inc(p.registersfpu,fpu);
  681. {$ifdef SUPPORT_MMX}
  682. if (p.left.registersmmx<mmx) then
  683. inc(p.registersmmx,mmx);
  684. {$endif SUPPORT_MMX}
  685. end;
  686. end;
  687. end;
  688. {****************************************************************************
  689. Subroutine Handling
  690. ****************************************************************************}
  691. function is_procvar_load(p:tnode):boolean;
  692. begin
  693. result:=false;
  694. { remove voidpointer typecast for tp procvars }
  695. if ((m_tp_procvar in current_settings.modeswitches) or
  696. (m_mac_procvar in current_settings.modeswitches)) and
  697. (p.nodetype=typeconvn) and
  698. is_voidpointer(p.resultdef) then
  699. p:=tunarynode(p).left;
  700. result:=(p.nodetype=typeconvn) and
  701. (ttypeconvnode(p).convtype=tc_proc_2_procvar);
  702. end;
  703. { local routines can't be assigned to procvars }
  704. procedure test_local_to_procvar(from_def:tprocvardef;to_def:tdef);
  705. begin
  706. if (from_def.parast.symtablelevel>normal_function_level) and
  707. (to_def.typ=procvardef) then
  708. CGMessage(type_e_cannot_local_proc_to_procvar);
  709. end;
  710. procedure set_varstate(p:tnode;newstate:tvarstate;varstateflags:tvarstateflags);
  711. const
  712. vstrans: array[tvarstate,tvarstate] of tvarstate = (
  713. { vs_none -> ... }
  714. (vs_none,vs_declared,vs_initialised,vs_read,vs_read_not_warned,vs_referred_not_inited,vs_written,vs_readwritten),
  715. { vs_declared -> ... }
  716. (vs_none,vs_declared,vs_initialised,vs_read,vs_read_not_warned,vs_referred_not_inited,vs_written,vs_readwritten),
  717. { vs_initialised -> ... }
  718. (vs_none,vs_initialised,vs_initialised,vs_read,vs_read,vs_read,vs_written,vs_readwritten),
  719. { vs_read -> ... }
  720. (vs_none,vs_read,vs_read,vs_read,vs_read,vs_read,vs_readwritten,vs_readwritten),
  721. { vs_read_not_warned -> ... }
  722. (vs_none,vs_read_not_warned,vs_read,vs_read,vs_read_not_warned,vs_read_not_warned,vs_readwritten,vs_readwritten),
  723. { vs_referred_not_inited }
  724. (vs_none,vs_referred_not_inited,vs_read,vs_read,vs_read_not_warned,vs_referred_not_inited,vs_written,vs_readwritten),
  725. { vs_written -> ... }
  726. (vs_none,vs_written,vs_written,vs_readwritten,vs_readwritten,vs_written,vs_written,vs_readwritten),
  727. { vs_readwritten -> ... }
  728. (vs_none,vs_readwritten,vs_readwritten,vs_readwritten,vs_readwritten,vs_readwritten,vs_readwritten,vs_readwritten));
  729. var
  730. hsym : tabstractvarsym;
  731. begin
  732. { make sure we can still warn about uninitialised use after high(v), @v etc }
  733. if (newstate = vs_read) and
  734. not(vsf_must_be_valid in varstateflags) then
  735. newstate := vs_referred_not_inited;
  736. while assigned(p) do
  737. begin
  738. case p.nodetype of
  739. derefn:
  740. begin
  741. if (tderefnode(p).left.nodetype=temprefn) and
  742. assigned(ttemprefnode(tderefnode(p).left).tempinfo^.withnode) then
  743. p:=ttemprefnode(tderefnode(p).left).tempinfo^.withnode
  744. else
  745. break;
  746. end;
  747. typeconvn :
  748. begin
  749. case ttypeconvnode(p).convtype of
  750. tc_cchar_2_pchar,
  751. tc_cstring_2_pchar,
  752. tc_array_2_pointer :
  753. exclude(varstateflags,vsf_must_be_valid);
  754. tc_pchar_2_string,
  755. tc_pointer_2_array :
  756. include(varstateflags,vsf_must_be_valid);
  757. end;
  758. p:=tunarynode(p).left;
  759. end;
  760. subscriptn :
  761. begin
  762. if is_class_or_interface(tunarynode(p).left.resultdef) then
  763. newstate := vs_read;
  764. p:=tunarynode(p).left;
  765. end;
  766. vecn:
  767. begin
  768. set_varstate(tbinarynode(p).right,vs_read,[vsf_must_be_valid]);
  769. if (newstate in [vs_read,vs_readwritten]) or
  770. not(tunarynode(p).left.resultdef.typ in [stringdef,arraydef]) then
  771. include(varstateflags,vsf_must_be_valid)
  772. else if (newstate = vs_written) then
  773. exclude(varstateflags,vsf_must_be_valid);
  774. p:=tunarynode(p).left;
  775. end;
  776. { do not parse calln }
  777. calln :
  778. break;
  779. loadn :
  780. begin
  781. if (tloadnode(p).symtableentry.typ in [localvarsym,paravarsym,staticvarsym]) then
  782. begin
  783. hsym:=tabstractvarsym(tloadnode(p).symtableentry);
  784. if (vsf_must_be_valid in varstateflags) and
  785. (hsym.varstate in [vs_declared,vs_read_not_warned,vs_referred_not_inited]) then
  786. begin
  787. { Give warning/note for uninitialized locals }
  788. if assigned(hsym.owner) and
  789. not(vo_is_external in hsym.varoptions) and
  790. (hsym.owner.symtabletype in [parasymtable,localsymtable,staticsymtable]) and
  791. ((hsym.owner=current_procinfo.procdef.localst) or
  792. (hsym.owner=current_procinfo.procdef.parast)) then
  793. begin
  794. if (vo_is_funcret in hsym.varoptions) then
  795. begin
  796. if (vsf_use_hints in varstateflags) then
  797. CGMessage(sym_h_function_result_uninitialized)
  798. else
  799. CGMessage(sym_w_function_result_uninitialized)
  800. end
  801. else
  802. begin
  803. if tloadnode(p).symtable.symtabletype=localsymtable then
  804. begin
  805. if (vsf_use_hints in varstateflags) then
  806. CGMessage1(sym_h_uninitialized_local_variable,hsym.realname)
  807. else
  808. CGMessage1(sym_w_uninitialized_local_variable,hsym.realname);
  809. end
  810. else
  811. begin
  812. if (vsf_use_hints in varstateflags) then
  813. CGMessage1(sym_h_uninitialized_variable,hsym.realname)
  814. else
  815. CGMessage1(sym_w_uninitialized_variable,hsym.realname);
  816. end;
  817. end;
  818. end
  819. else if (newstate = vs_read) then
  820. newstate := vs_read_not_warned;
  821. end;
  822. hsym.varstate := vstrans[hsym.varstate,newstate];
  823. end;
  824. break;
  825. end;
  826. callparan :
  827. internalerror(200310081);
  828. else
  829. break;
  830. end;{case }
  831. end;
  832. end;
  833. procedure set_unique(p : tnode);
  834. begin
  835. while assigned(p) do
  836. begin
  837. case p.nodetype of
  838. vecn:
  839. begin
  840. include(p.flags,nf_callunique);
  841. break;
  842. end;
  843. typeconvn,
  844. subscriptn,
  845. derefn:
  846. p:=tunarynode(p).left;
  847. else
  848. break;
  849. end;
  850. end;
  851. end;
  852. function valid_for_assign(p:tnode;opts:TValidAssigns; report_errors: boolean):boolean;
  853. var
  854. hp2,
  855. hp : tnode;
  856. gotstring,
  857. gotsubscript,
  858. gotrecord,
  859. gotpointer,
  860. gotvec,
  861. gotclass,
  862. gotdynarray,
  863. gotderef : boolean;
  864. fromdef,
  865. todef : tdef;
  866. errmsg,
  867. temp : longint;
  868. begin
  869. if valid_const in opts then
  870. errmsg:=type_e_variable_id_expected
  871. else if valid_property in opts then
  872. errmsg:=type_e_argument_cant_be_assigned
  873. else
  874. errmsg:=type_e_no_addr_of_constant;
  875. result:=false;
  876. gotsubscript:=false;
  877. gotvec:=false;
  878. gotderef:=false;
  879. gotrecord:=false;
  880. gotclass:=false;
  881. gotpointer:=false;
  882. gotdynarray:=false;
  883. gotstring:=false;
  884. hp:=p;
  885. if not(valid_void in opts) and
  886. is_void(hp.resultdef) then
  887. begin
  888. if report_errors then
  889. CGMessagePos(hp.fileinfo,errmsg);
  890. exit;
  891. end;
  892. while assigned(hp) do
  893. begin
  894. { property allowed? calln has a property check itself }
  895. if (nf_isproperty in hp.flags) then
  896. begin
  897. { check return type }
  898. case hp.resultdef.typ of
  899. pointerdef :
  900. gotpointer:=true;
  901. objectdef :
  902. gotclass:=is_class_or_interface(hp.resultdef);
  903. recorddef :
  904. gotrecord:=true;
  905. classrefdef :
  906. gotclass:=true;
  907. stringdef :
  908. gotstring:=true;
  909. end;
  910. if (valid_property in opts) then
  911. begin
  912. { don't allow writing to calls that will create
  913. temps like calls that return a structure and we
  914. are assigning to a member }
  915. if (valid_const in opts) or
  916. not(
  917. (gotsubscript and gotrecord) or
  918. (gotstring and gotvec)
  919. ) then
  920. result:=true
  921. else
  922. if report_errors then
  923. CGMessagePos(hp.fileinfo,errmsg);
  924. end
  925. else
  926. begin
  927. { 1. if it returns a pointer and we've found a deref,
  928. 2. if it returns a class or record and a subscription or with is found
  929. 3. if the address is needed of a field (subscriptn, vecn) }
  930. if (gotpointer and gotderef) or
  931. (gotstring and gotvec) or
  932. (
  933. (gotclass or gotrecord) and
  934. (gotsubscript)
  935. ) or
  936. (
  937. (gotvec and gotdynarray)
  938. ) or
  939. (
  940. (Valid_Addr in opts) and
  941. (hp.nodetype in [subscriptn,vecn])
  942. ) then
  943. result:=true
  944. else
  945. if report_errors then
  946. CGMessagePos(hp.fileinfo,errmsg);
  947. end;
  948. exit;
  949. end;
  950. if (Valid_Const in opts) and is_constnode(hp) then
  951. begin
  952. result:=true;
  953. exit;
  954. end;
  955. case hp.nodetype of
  956. temprefn :
  957. begin
  958. valid_for_assign := true;
  959. exit;
  960. end;
  961. derefn :
  962. begin
  963. gotderef:=true;
  964. hp:=tderefnode(hp).left;
  965. end;
  966. typeconvn :
  967. begin
  968. { typecast sizes must match, exceptions:
  969. - implicit typecast made by absolute
  970. - from formaldef
  971. - from void
  972. - from/to open array
  973. - typecast from pointer to array }
  974. fromdef:=ttypeconvnode(hp).left.resultdef;
  975. todef:=hp.resultdef;
  976. if not((nf_absolute in ttypeconvnode(hp).flags) or
  977. (fromdef.typ=formaldef) or
  978. is_void(fromdef) or
  979. is_open_array(fromdef) or
  980. is_open_array(todef) or
  981. ((fromdef.typ=pointerdef) and (todef.typ=arraydef)) or
  982. ((fromdef.typ = objectdef) and (todef.typ = objectdef) and
  983. (tobjectdef(fromdef).is_related(tobjectdef(todef))))) and
  984. (fromdef.size<>todef.size) then
  985. begin
  986. { in TP it is allowed to typecast to smaller types. But the variable can't
  987. be in a register }
  988. if (m_tp7 in current_settings.modeswitches) or
  989. (todef.size<fromdef.size) then
  990. make_not_regable(hp,vr_addr)
  991. else
  992. if report_errors then
  993. CGMessagePos2(hp.fileinfo,type_e_typecast_wrong_size_for_assignment,tostr(fromdef.size),tostr(todef.size));
  994. end;
  995. { don't allow assignments to typeconvs that need special code }
  996. if not(gotsubscript or gotvec or gotderef) and
  997. not(ttypeconvnode(hp).assign_allowed) then
  998. begin
  999. if report_errors then
  1000. CGMessagePos(hp.fileinfo,errmsg);
  1001. exit;
  1002. end;
  1003. case hp.resultdef.typ of
  1004. pointerdef :
  1005. gotpointer:=true;
  1006. objectdef :
  1007. gotclass:=is_class_or_interface(hp.resultdef);
  1008. classrefdef :
  1009. gotclass:=true;
  1010. arraydef :
  1011. begin
  1012. { pointer -> array conversion is done then we need to see it
  1013. as a deref, because a ^ is then not required anymore }
  1014. if (ttypeconvnode(hp).left.resultdef.typ=pointerdef) then
  1015. gotderef:=true;
  1016. end;
  1017. end;
  1018. hp:=ttypeconvnode(hp).left;
  1019. end;
  1020. vecn :
  1021. begin
  1022. if { only check for first (= outermost) vec node }
  1023. not gotvec and
  1024. not(valid_packed in opts) and
  1025. (tvecnode(hp).left.resultdef.typ = arraydef) and
  1026. (ado_IsBitPacked in tarraydef(tvecnode(hp).left.resultdef).arrayoptions) and
  1027. (tarraydef(tvecnode(hp).left.resultdef).elepackedbitsize mod 8 <> 0) then
  1028. begin
  1029. if report_errors then
  1030. if (valid_property in opts) then
  1031. CGMessagePos(hp.fileinfo,parser_e_packed_element_no_loop)
  1032. else
  1033. CGMessagePos(hp.fileinfo,parser_e_packed_element_no_var_addr);
  1034. exit;
  1035. end;
  1036. gotvec:=true;
  1037. { accesses to dyn. arrays override read only access in delphi }
  1038. if (m_delphi in current_settings.modeswitches) and is_dynamic_array(tunarynode(hp).left.resultdef) then
  1039. gotdynarray:=true;
  1040. hp:=tunarynode(hp).left;
  1041. end;
  1042. blockn :
  1043. begin
  1044. hp2:=tblocknode(hp).statements;
  1045. if assigned(hp2) then
  1046. begin
  1047. if hp2.nodetype<>statementn then
  1048. internalerror(2006110801);
  1049. while assigned(tstatementnode(hp2).next) do
  1050. hp2:=tstatementnode(hp2).next;
  1051. hp:=tstatementnode(hp2).statement;
  1052. end
  1053. else
  1054. begin
  1055. if report_errors then
  1056. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  1057. exit;
  1058. end;
  1059. end;
  1060. asn :
  1061. begin
  1062. { asn can't be assigned directly, it returns the value in a register instead
  1063. of reference. }
  1064. if not(gotsubscript or gotderef or gotvec) then
  1065. begin
  1066. if report_errors then
  1067. CGMessagePos(hp.fileinfo,errmsg);
  1068. exit;
  1069. end;
  1070. hp:=tunarynode(hp).left;
  1071. end;
  1072. subscriptn :
  1073. begin
  1074. { only check first (= outermost) subscriptn }
  1075. if not gotsubscript and
  1076. not(valid_packed in opts) and
  1077. is_packed_record_or_object(tsubscriptnode(hp).left.resultdef) and
  1078. ((tsubscriptnode(hp).vs.fieldoffset mod 8 <> 0) or
  1079. (is_ordinal(tsubscriptnode(hp).resultdef) and
  1080. not ispowerof2(tsubscriptnode(hp).resultdef.packedbitsize div 8,temp))) then
  1081. begin
  1082. if report_errors then
  1083. if (valid_property in opts) then
  1084. CGMessagePos(hp.fileinfo,parser_e_packed_element_no_loop)
  1085. else
  1086. CGMessagePos(hp.fileinfo,parser_e_packed_element_no_var_addr);
  1087. exit;
  1088. end;
  1089. gotsubscript:=true;
  1090. { loop counter? }
  1091. if not(Valid_Const in opts) and
  1092. (vo_is_loop_counter in tsubscriptnode(hp).vs.varoptions) then
  1093. begin
  1094. if report_errors then
  1095. CGMessage1(parser_e_illegal_assignment_to_count_var,tsubscriptnode(hp).vs.realname)
  1096. else
  1097. exit;
  1098. end;
  1099. { a class/interface access is an implicit }
  1100. { dereferencing }
  1101. hp:=tsubscriptnode(hp).left;
  1102. if is_class_or_interface(hp.resultdef) then
  1103. gotderef:=true;
  1104. end;
  1105. muln,
  1106. divn,
  1107. andn,
  1108. xorn,
  1109. orn,
  1110. notn,
  1111. subn,
  1112. addn :
  1113. begin
  1114. { Allow operators on a pointer, or an integer
  1115. and a pointer typecast and deref has been found }
  1116. if ((hp.resultdef.typ=pointerdef) or
  1117. (is_integer(hp.resultdef) and gotpointer)) and
  1118. gotderef then
  1119. result:=true
  1120. else
  1121. { Temp strings are stored in memory, for compatibility with
  1122. delphi only }
  1123. if (m_delphi in current_settings.modeswitches) and
  1124. ((valid_addr in opts) or
  1125. (valid_const in opts)) and
  1126. (hp.resultdef.typ=stringdef) then
  1127. result:=true
  1128. else
  1129. if report_errors then
  1130. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  1131. exit;
  1132. end;
  1133. niln,
  1134. pointerconstn :
  1135. begin
  1136. { to support e.g. @tmypointer(0)^.data; see tests/tbs/tb0481 }
  1137. if gotderef then
  1138. result:=true
  1139. else
  1140. if report_errors then
  1141. CGMessagePos(hp.fileinfo,type_e_no_assign_to_addr);
  1142. exit;
  1143. end;
  1144. addrn :
  1145. begin
  1146. if gotderef then
  1147. result:=true
  1148. else
  1149. if report_errors then
  1150. CGMessagePos(hp.fileinfo,type_e_no_assign_to_addr);
  1151. exit;
  1152. end;
  1153. calln :
  1154. begin
  1155. { check return type }
  1156. case hp.resultdef.typ of
  1157. arraydef :
  1158. begin
  1159. { dynamic arrays are allowed when there is also a
  1160. vec node }
  1161. if is_dynamic_array(hp.resultdef) and
  1162. gotvec then
  1163. begin
  1164. gotderef:=true;
  1165. gotpointer:=true;
  1166. end;
  1167. end;
  1168. pointerdef :
  1169. gotpointer:=true;
  1170. objectdef :
  1171. gotclass:=is_class_or_interface(hp.resultdef);
  1172. recorddef, { handle record like class it needs a subscription }
  1173. classrefdef :
  1174. gotclass:=true;
  1175. stringdef :
  1176. gotstring:=true;
  1177. end;
  1178. { 1. if it returns a pointer and we've found a deref,
  1179. 2. if it returns a class or record and a subscription or with is found
  1180. 3. string is returned }
  1181. if (gotstring and gotvec) or
  1182. (gotpointer and gotderef) or
  1183. (gotclass and gotsubscript) then
  1184. result:=true
  1185. else
  1186. { Temp strings are stored in memory, for compatibility with
  1187. delphi only }
  1188. if (m_delphi in current_settings.modeswitches) and
  1189. (valid_addr in opts) and
  1190. (hp.resultdef.typ=stringdef) then
  1191. result:=true
  1192. else
  1193. if ([valid_const,valid_addr] * opts = [valid_const]) then
  1194. result:=true
  1195. else
  1196. if report_errors then
  1197. CGMessagePos(hp.fileinfo,errmsg);
  1198. exit;
  1199. end;
  1200. inlinen :
  1201. begin
  1202. if ((valid_const in opts) and
  1203. (tinlinenode(hp).inlinenumber in [in_typeof_x]))
  1204. {$ifdef SUPPORT_UNALIGNED}
  1205. or (tinlinenode(hp).inlinenumber in [in_unaligned_x])
  1206. {$endif SUPPORT_UNALIGNED}
  1207. then
  1208. result:=true
  1209. else
  1210. if report_errors then
  1211. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  1212. exit;
  1213. end;
  1214. dataconstn:
  1215. begin
  1216. { only created internally, so no additional checks necessary }
  1217. result:=true;
  1218. exit;
  1219. end;
  1220. loadn :
  1221. begin
  1222. case tloadnode(hp).symtableentry.typ of
  1223. absolutevarsym,
  1224. staticvarsym,
  1225. localvarsym,
  1226. paravarsym :
  1227. begin
  1228. { loop counter? }
  1229. if not(Valid_Const in opts) and
  1230. not gotderef and
  1231. (vo_is_loop_counter in tabstractvarsym(tloadnode(hp).symtableentry).varoptions) then
  1232. if report_errors then
  1233. CGMessage1(parser_e_illegal_assignment_to_count_var,tloadnode(hp).symtableentry.realname)
  1234. else
  1235. exit;
  1236. { read-only variable? }
  1237. if (tabstractvarsym(tloadnode(hp).symtableentry).varspez=vs_const) then
  1238. begin
  1239. { allow p^:= constructions with p is const parameter }
  1240. if gotderef or gotdynarray or (Valid_Const in opts) or
  1241. (nf_isinternal_ignoreconst in tloadnode(hp).flags) then
  1242. result:=true
  1243. else
  1244. if report_errors then
  1245. CGMessagePos(tloadnode(hp).fileinfo,type_e_no_assign_to_const);
  1246. exit;
  1247. end;
  1248. result:=true;
  1249. exit;
  1250. end;
  1251. procsym :
  1252. begin
  1253. if (Valid_Const in opts) then
  1254. result:=true
  1255. else
  1256. if report_errors then
  1257. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  1258. exit;
  1259. end;
  1260. labelsym :
  1261. begin
  1262. if (Valid_Addr in opts) then
  1263. result:=true
  1264. else
  1265. if report_errors then
  1266. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  1267. exit;
  1268. end;
  1269. constsym:
  1270. begin
  1271. if (tconstsym(tloadnode(hp).symtableentry).consttyp=constresourcestring) and
  1272. (valid_addr in opts) then
  1273. result:=true
  1274. else
  1275. if report_errors then
  1276. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  1277. exit;
  1278. end;
  1279. else
  1280. begin
  1281. if report_errors then
  1282. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  1283. exit;
  1284. end;
  1285. end;
  1286. end;
  1287. else
  1288. begin
  1289. if report_errors then
  1290. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  1291. exit;
  1292. end;
  1293. end;
  1294. end;
  1295. end;
  1296. function valid_for_var(p:tnode; report_errors: boolean):boolean;
  1297. begin
  1298. valid_for_var:=valid_for_assign(p,[],report_errors);
  1299. end;
  1300. function valid_for_formal_var(p : tnode; report_errors: boolean) : boolean;
  1301. begin
  1302. valid_for_formal_var:=valid_for_assign(p,[valid_void],report_errors);
  1303. end;
  1304. function valid_for_formal_const(p : tnode; report_errors: boolean) : boolean;
  1305. begin
  1306. valid_for_formal_const:=(p.resultdef.typ=formaldef) or
  1307. valid_for_assign(p,[valid_void,valid_const,valid_property],report_errors);
  1308. end;
  1309. function valid_for_assignment(p:tnode; report_errors: boolean):boolean;
  1310. begin
  1311. valid_for_assignment:=valid_for_assign(p,[valid_property,valid_packed],report_errors);
  1312. end;
  1313. function valid_for_loopvar(p:tnode; report_errors: boolean):boolean;
  1314. begin
  1315. valid_for_loopvar:=valid_for_assign(p,[valid_property],report_errors);
  1316. end;
  1317. function valid_for_addr(p : tnode; report_errors: boolean) : boolean;
  1318. begin
  1319. result:=valid_for_assign(p,[valid_const,valid_addr,valid_void],report_errors);
  1320. end;
  1321. procedure var_para_allowed(var eq:tequaltype;def_from,def_to:Tdef);
  1322. begin
  1323. { Note: eq must be already valid, it will only be updated! }
  1324. case def_to.typ of
  1325. formaldef :
  1326. begin
  1327. { all types can be passed to a formaldef,
  1328. but it is not the prefered way }
  1329. eq:=te_convert_l2;
  1330. end;
  1331. orddef :
  1332. begin
  1333. { allows conversion from word to integer and
  1334. byte to shortint, but only for TP7 compatibility }
  1335. if (m_tp7 in current_settings.modeswitches) and
  1336. (def_from.typ=orddef) and
  1337. (def_from.size=def_to.size) then
  1338. eq:=te_convert_l1;
  1339. end;
  1340. arraydef :
  1341. begin
  1342. if is_open_array(def_to) then
  1343. begin
  1344. if is_dynamic_array(def_from) and
  1345. equal_defs(tarraydef(def_from).elementdef,tarraydef(def_to).elementdef) then
  1346. eq:=te_convert_l2
  1347. else
  1348. if equal_defs(def_from,tarraydef(def_to).elementdef) then
  1349. eq:=te_convert_l2;
  1350. end;
  1351. end;
  1352. pointerdef :
  1353. begin
  1354. { an implicit pointer conversion is allowed }
  1355. if (def_from.typ=pointerdef) then
  1356. eq:=te_convert_l1;
  1357. end;
  1358. stringdef :
  1359. begin
  1360. { all shortstrings are allowed, size is not important }
  1361. if is_shortstring(def_from) and
  1362. is_shortstring(def_to) then
  1363. eq:=te_equal;
  1364. end;
  1365. objectdef :
  1366. begin
  1367. { child objects can be also passed }
  1368. { in non-delphi mode, otherwise }
  1369. { they must match exactly, except }
  1370. { if they are objects }
  1371. if (def_from.typ=objectdef) and
  1372. (
  1373. not(m_delphi in current_settings.modeswitches) or
  1374. (
  1375. (tobjectdef(def_from).objecttype=odt_object) and
  1376. (tobjectdef(def_to).objecttype=odt_object)
  1377. )
  1378. ) and
  1379. (tobjectdef(def_from).is_related(tobjectdef(def_to))) then
  1380. eq:=te_convert_l1;
  1381. end;
  1382. filedef :
  1383. begin
  1384. { an implicit file conversion is also allowed }
  1385. { from a typed file to an untyped one }
  1386. if (def_from.typ=filedef) and
  1387. (tfiledef(def_from).filetyp = ft_typed) and
  1388. (tfiledef(def_to).filetyp = ft_untyped) then
  1389. eq:=te_convert_l1;
  1390. end;
  1391. end;
  1392. end;
  1393. procedure para_allowed(var eq:tequaltype;p:tcallparanode;def_to:tdef);
  1394. begin
  1395. { Note: eq must be already valid, it will only be updated! }
  1396. case def_to.typ of
  1397. formaldef :
  1398. begin
  1399. { all types can be passed to a formaldef }
  1400. eq:=te_equal;
  1401. end;
  1402. stringdef :
  1403. begin
  1404. { to support ansi/long/wide strings in a proper way }
  1405. { string and string[10] are assumed as equal }
  1406. { when searching the correct overloaded procedure }
  1407. if (p.resultdef.typ=stringdef) and
  1408. (tstringdef(def_to).stringtype=tstringdef(p.resultdef).stringtype) then
  1409. eq:=te_equal
  1410. else
  1411. { Passing a constant char to ansistring or shortstring or
  1412. a widechar to widestring then handle it as equal. }
  1413. if (p.left.nodetype=ordconstn) and
  1414. (
  1415. is_char(p.resultdef) and
  1416. (is_shortstring(def_to) or is_ansistring(def_to))
  1417. ) or
  1418. (
  1419. is_widechar(p.resultdef) and
  1420. is_widestring(def_to)
  1421. ) then
  1422. eq:=te_equal
  1423. end;
  1424. setdef :
  1425. begin
  1426. { set can also be a not yet converted array constructor }
  1427. if (p.resultdef.typ=arraydef) and
  1428. is_array_constructor(p.resultdef) and
  1429. not is_variant_array(p.resultdef) then
  1430. eq:=te_equal;
  1431. end;
  1432. procvardef :
  1433. begin
  1434. { in tp7 mode proc -> procvar is allowed }
  1435. if ((m_tp_procvar in current_settings.modeswitches) or
  1436. (m_mac_procvar in current_settings.modeswitches)) and
  1437. (p.left.nodetype=calln) and
  1438. (proc_to_procvar_equal(tprocdef(tcallnode(p.left).procdefinition),tprocvardef(def_to))>=te_equal) then
  1439. eq:=te_equal
  1440. else
  1441. if (m_mac_procvar in current_settings.modeswitches) and
  1442. is_procvar_load(p.left) then
  1443. eq:=te_convert_l2;
  1444. end;
  1445. end;
  1446. end;
  1447. function allowenumop(nt:tnodetype):boolean;
  1448. begin
  1449. result:=(nt in [equaln,unequaln,ltn,lten,gtn,gten]) or
  1450. ((cs_allow_enum_calc in current_settings.localswitches) and
  1451. (nt in [addn,subn]));
  1452. end;
  1453. {****************************************************************************
  1454. TCallCandidates
  1455. ****************************************************************************}
  1456. constructor tcallcandidates.create(sym:tprocsym;st:TSymtable;ppn:tnode;isprop,ignorevis : boolean);
  1457. var
  1458. j : integer;
  1459. pd : tprocdef;
  1460. hp : pcandidate;
  1461. found,
  1462. has_overload_directive : boolean;
  1463. topclassh : tobjectdef;
  1464. srsymtable : TSymtable;
  1465. srprocsym : tprocsym;
  1466. pt : tcallparanode;
  1467. checkstack : psymtablestackitem;
  1468. hashedid : THashedIDString;
  1469. begin
  1470. if not assigned(sym) then
  1471. internalerror(200411015);
  1472. FProcSym:=sym;
  1473. FProcs:=nil;
  1474. FProccnt:=0;
  1475. FProcvisiblecnt:=0;
  1476. FParanode:=ppn;
  1477. FAllowVariant:=true;
  1478. { determine length of parameter list }
  1479. pt:=tcallparanode(ppn);
  1480. FParalength:=0;
  1481. while assigned(pt) do
  1482. begin
  1483. inc(FParalength);
  1484. pt:=tcallparanode(pt.right);
  1485. end;
  1486. { when the definition has overload directive set, we search for
  1487. overloaded definitions in the class, this only needs to be done once
  1488. for class entries as the tree keeps always the same }
  1489. if (not sym.overloadchecked) and
  1490. (sym.owner.symtabletype=ObjectSymtable) and
  1491. (po_overload in tprocdef(sym.ProcdefList[0]).procoptions) then
  1492. search_class_overloads(sym);
  1493. { when the class passed is defined in this unit we
  1494. need to use the scope of that class. This is a trick
  1495. that can be used to access protected members in other
  1496. units. At least kylix supports it this way (PFV) }
  1497. if assigned(st) and
  1498. (
  1499. (st.symtabletype=ObjectSymtable) or
  1500. ((st.symtabletype=withsymtable) and
  1501. (st.defowner.typ=objectdef))
  1502. ) and
  1503. (st.defowner.owner.symtabletype in [globalsymtable,staticsymtable]) and
  1504. st.defowner.owner.iscurrentunit then
  1505. topclassh:=tobjectdef(st.defowner)
  1506. else
  1507. begin
  1508. if assigned(current_procinfo) then
  1509. topclassh:=current_procinfo.procdef._class
  1510. else
  1511. topclassh:=nil;
  1512. end;
  1513. { link all procedures which have the same # of parameters }
  1514. for j:=0 to sym.ProcdefList.Count-1 do
  1515. begin
  1516. pd:=tprocdef(sym.ProcdefList[j]);
  1517. { Is the procdef visible? This needs to be checked on
  1518. procdef level since a symbol can contain both private and
  1519. public declarations. But the check should not be done
  1520. when the callnode is generated by a property
  1521. inherited overrides invisible anonymous inherited (FK) }
  1522. if isprop or ignorevis or
  1523. (pd.owner.symtabletype<>ObjectSymtable) or
  1524. pd.is_visible_for_object(topclassh,nil) then
  1525. begin
  1526. { we have at least one procedure that is visible }
  1527. inc(FProcvisiblecnt);
  1528. { only when the # of parameter are supported by the
  1529. procedure }
  1530. if (FParalength>=pd.minparacount) and
  1531. ((po_varargs in pd.procoptions) or { varargs }
  1532. (FParalength<=pd.maxparacount)) then
  1533. proc_add(sym,pd);
  1534. end;
  1535. end;
  1536. { remember if the procedure is declared with the overload directive,
  1537. it's information is still needed also after all procs are removed }
  1538. has_overload_directive:=(po_overload in tprocdef(sym.ProcdefList[0]).procoptions);
  1539. { when the definition has overload directive set, we search for
  1540. overloaded definitions in the symtablestack. The found
  1541. entries are only added to the procs list and not the procsym, because
  1542. the list can change in every situation }
  1543. if has_overload_directive and
  1544. (sym.owner.symtabletype<>ObjectSymtable) then
  1545. begin
  1546. srsymtable:=sym.owner;
  1547. checkstack:=symtablestack.stack;
  1548. while assigned(checkstack) and
  1549. (checkstack^.symtable<>srsymtable) do
  1550. checkstack:=checkstack^.next;
  1551. { we've already processed the current symtable, start with
  1552. the next symtable in the stack }
  1553. if assigned(checkstack) then
  1554. checkstack:=checkstack^.next;
  1555. hashedid.id:=sym.name;
  1556. while assigned(checkstack) do
  1557. begin
  1558. srsymtable:=checkstack^.symtable;
  1559. if srsymtable.symtabletype in [localsymtable,staticsymtable,globalsymtable] then
  1560. begin
  1561. srprocsym:=tprocsym(srsymtable.FindWithHash(hashedid));
  1562. if assigned(srprocsym) and
  1563. (srprocsym.typ=procsym) then
  1564. begin
  1565. { if this visible procedure doesn't have overload we can stop
  1566. searching }
  1567. if not(po_overload in tprocdef(srprocsym.ProcdefList[0]).procoptions) and
  1568. tprocdef(srprocsym.ProcdefList[0]).is_visible_for_object(topclassh,nil) then
  1569. break;
  1570. { process all overloaded definitions }
  1571. for j:=0 to srprocsym.ProcdefList.Count-1 do
  1572. begin
  1573. pd:=tprocdef(srprocsym.ProcdefList[j]);
  1574. { only visible procedures need to be added }
  1575. if pd.is_visible_for_object(topclassh,nil) then
  1576. begin
  1577. { only when the # of parameter are supported by the
  1578. procedure }
  1579. if (FParalength>=pd.minparacount) and
  1580. ((po_varargs in pd.procoptions) or { varargs }
  1581. (FParalength<=pd.maxparacount)) then
  1582. begin
  1583. found:=false;
  1584. hp:=FProcs;
  1585. while assigned(hp) do
  1586. begin
  1587. { Only compare visible parameters for the user }
  1588. if compare_paras(hp^.data.paras,pd.paras,cp_value_equal_const,[cpo_ignorehidden])>=te_equal then
  1589. begin
  1590. found:=true;
  1591. break;
  1592. end;
  1593. hp:=hp^.next;
  1594. end;
  1595. if not found then
  1596. proc_add(srprocsym,pd);
  1597. end;
  1598. end;
  1599. end;
  1600. end;
  1601. end;
  1602. checkstack:=checkstack^.next;
  1603. end;
  1604. end;
  1605. end;
  1606. constructor tcallcandidates.create_operator(op:ttoken;ppn:tnode);
  1607. var
  1608. j : integer;
  1609. pd : tprocdef;
  1610. hp : pcandidate;
  1611. found : boolean;
  1612. srsymtable : TSymtable;
  1613. srprocsym : tprocsym;
  1614. pt : tcallparanode;
  1615. checkstack : psymtablestackitem;
  1616. hashedid : THashedIDString;
  1617. begin
  1618. FProcSym:=nil;
  1619. FProcs:=nil;
  1620. FProccnt:=0;
  1621. FProcvisiblecnt:=0;
  1622. FParanode:=ppn;
  1623. FAllowVariant:=false;
  1624. { determine length of parameter list }
  1625. pt:=tcallparanode(ppn);
  1626. FParalength:=0;
  1627. while assigned(pt) do
  1628. begin
  1629. if pt.resultdef.typ=variantdef then
  1630. FAllowVariant:=true;
  1631. inc(FParalength);
  1632. pt:=tcallparanode(pt.right);
  1633. end;
  1634. { we search all overloaded operator definitions in the symtablestack. The found
  1635. entries are only added to the procs list and not the procsym, because
  1636. the list can change in every situation }
  1637. hashedid.id:=overloaded_names[op];
  1638. checkstack:=symtablestack.stack;
  1639. while assigned(checkstack) do
  1640. begin
  1641. srsymtable:=checkstack^.symtable;
  1642. if srsymtable.symtabletype in [localsymtable,staticsymtable,globalsymtable] then
  1643. begin
  1644. srprocsym:=tprocsym(srsymtable.FindWithHash(hashedid));
  1645. if assigned(srprocsym) and
  1646. (srprocsym.typ=procsym) then
  1647. begin
  1648. { Store first procsym found }
  1649. if not assigned(FProcsym) then
  1650. FProcsym:=srprocsym;
  1651. { process all overloaded definitions }
  1652. for j:=0 to srprocsym.ProcdefList.Count-1 do
  1653. begin
  1654. pd:=tprocdef(srprocsym.ProcdefList[j]);
  1655. { only when the # of parameter are supported by the
  1656. procedure }
  1657. if (FParalength>=pd.minparacount) and
  1658. (FParalength<=pd.maxparacount) then
  1659. begin
  1660. found:=false;
  1661. hp:=FProcs;
  1662. while assigned(hp) do
  1663. begin
  1664. { Only compare visible parameters for the user }
  1665. if compare_paras(hp^.data.paras,pd.paras,cp_value_equal_const,[cpo_ignorehidden])>=te_equal then
  1666. begin
  1667. found:=true;
  1668. break;
  1669. end;
  1670. hp:=hp^.next;
  1671. end;
  1672. if not found then
  1673. proc_add(srprocsym,pd);
  1674. end;
  1675. end;
  1676. end;
  1677. end;
  1678. checkstack:=checkstack^.next;
  1679. end;
  1680. end;
  1681. destructor tcallcandidates.destroy;
  1682. var
  1683. hpnext,
  1684. hp : pcandidate;
  1685. begin
  1686. hp:=FProcs;
  1687. while assigned(hp) do
  1688. begin
  1689. hpnext:=hp^.next;
  1690. dispose(hp);
  1691. hp:=hpnext;
  1692. end;
  1693. end;
  1694. function tcallcandidates.proc_add(ps:tprocsym;pd:tprocdef):pcandidate;
  1695. var
  1696. defaultparacnt : integer;
  1697. begin
  1698. { generate new candidate entry }
  1699. new(result);
  1700. fillchar(result^,sizeof(tcandidate),0);
  1701. result^.data:=pd;
  1702. result^.next:=FProcs;
  1703. FProcs:=result;
  1704. inc(FProccnt);
  1705. { Find last parameter, skip all default parameters
  1706. that are not passed. Ignore this skipping for varargs }
  1707. result^.firstparaidx:=pd.paras.count-1;
  1708. if not(po_varargs in pd.procoptions) then
  1709. begin
  1710. { ignore hidden parameters }
  1711. while (result^.firstparaidx>=0) and (vo_is_hidden_para in tparavarsym(pd.paras[result^.firstparaidx]).varoptions) do
  1712. dec(result^.firstparaidx);
  1713. defaultparacnt:=pd.maxparacount-FParalength;
  1714. if defaultparacnt>0 then
  1715. begin
  1716. if defaultparacnt>result^.firstparaidx+1 then
  1717. internalerror(200401141);
  1718. dec(result^.firstparaidx,defaultparacnt);
  1719. end;
  1720. end;
  1721. { Give a small penalty for overloaded methods not in
  1722. defined the current class/unit }
  1723. if ps.owner<>pd.owner then
  1724. result^.ordinal_distance:=result^.ordinal_distance+1.0;
  1725. end;
  1726. procedure tcallcandidates.list(all:boolean);
  1727. var
  1728. hp : pcandidate;
  1729. begin
  1730. hp:=FProcs;
  1731. while assigned(hp) do
  1732. begin
  1733. if all or
  1734. (not hp^.invalid) then
  1735. MessagePos1(hp^.data.fileinfo,sym_h_param_list,hp^.data.fullprocname(false));
  1736. hp:=hp^.next;
  1737. end;
  1738. end;
  1739. {$ifdef EXTDEBUG}
  1740. procedure tcallcandidates.dump_info(lvl:longint);
  1741. function ParaTreeStr(p:tcallparanode):string;
  1742. begin
  1743. result:='';
  1744. while assigned(p) do
  1745. begin
  1746. if result<>'' then
  1747. result:=','+result;
  1748. result:=p.resultdef.typename+result;
  1749. p:=tcallparanode(p.right);
  1750. end;
  1751. end;
  1752. var
  1753. hp : pcandidate;
  1754. i : integer;
  1755. currpara : tparavarsym;
  1756. begin
  1757. if not CheckVerbosity(lvl) then
  1758. exit;
  1759. Comment(lvl+V_LineInfo,'Overloaded callnode: '+FProcSym.name+'('+ParaTreeStr(tcallparanode(FParaNode))+')');
  1760. hp:=FProcs;
  1761. while assigned(hp) do
  1762. begin
  1763. Comment(lvl,' '+hp^.data.fullprocname(false));
  1764. if (hp^.invalid) then
  1765. Comment(lvl,' invalid')
  1766. else
  1767. begin
  1768. Comment(lvl,' ex: '+tostr(hp^.exact_count)+
  1769. ' eq: '+tostr(hp^.equal_count)+
  1770. ' l1: '+tostr(hp^.cl1_count)+
  1771. ' l2: '+tostr(hp^.cl2_count)+
  1772. ' l3: '+tostr(hp^.cl3_count)+
  1773. ' oper: '+tostr(hp^.coper_count)+
  1774. ' ord: '+realtostr(hp^.ordinal_distance));
  1775. { Print parameters in left-right order }
  1776. for i:=0 to hp^.data.paras.count-1 do
  1777. begin
  1778. currpara:=tparavarsym(hp^.data.paras[i]);
  1779. if not(vo_is_hidden_para in currpara.varoptions) then
  1780. Comment(lvl,' - '+currpara.vardef.typename+' : '+EqualTypeName[currpara.eqval]);
  1781. end;
  1782. end;
  1783. hp:=hp^.next;
  1784. end;
  1785. end;
  1786. {$endif EXTDEBUG}
  1787. procedure tcallcandidates.get_information;
  1788. var
  1789. hp : pcandidate;
  1790. currpara : tparavarsym;
  1791. paraidx : integer;
  1792. currparanr : byte;
  1793. rfh,rth : bestreal;
  1794. objdef : tobjectdef;
  1795. def_from,
  1796. def_to : tdef;
  1797. currpt,
  1798. pt : tcallparanode;
  1799. eq : tequaltype;
  1800. convtype : tconverttype;
  1801. pdtemp,
  1802. pdoper : tprocdef;
  1803. releasecurrpt : boolean;
  1804. cdoptions : tcompare_defs_options;
  1805. begin
  1806. cdoptions:=[cdo_check_operator];
  1807. if FAllowVariant then
  1808. include(cdoptions,cdo_allow_variant);
  1809. { process all procs }
  1810. hp:=FProcs;
  1811. while assigned(hp) do
  1812. begin
  1813. { We compare parameters in reverse order (right to left),
  1814. the firstpara is already pointing to the last parameter
  1815. were we need to start comparing }
  1816. currparanr:=FParalength;
  1817. paraidx:=hp^.firstparaidx;
  1818. while (paraidx>=0) and (vo_is_hidden_para in tparavarsym(hp^.data.paras[paraidx]).varoptions) do
  1819. dec(paraidx);
  1820. pt:=tcallparanode(FParaNode);
  1821. while assigned(pt) and (paraidx>=0) do
  1822. begin
  1823. currpara:=tparavarsym(hp^.data.paras[paraidx]);
  1824. { currpt can be changed from loadn to calln when a procvar
  1825. is passed. This is to prevent that the change is permanent }
  1826. currpt:=pt;
  1827. releasecurrpt:=false;
  1828. { retrieve current parameter definitions to compares }
  1829. eq:=te_incompatible;
  1830. def_from:=currpt.resultdef;
  1831. def_to:=currpara.vardef;
  1832. if not(assigned(def_from)) then
  1833. internalerror(200212091);
  1834. if not(
  1835. assigned(def_to) or
  1836. ((po_varargs in hp^.data.procoptions) and
  1837. (currparanr>hp^.data.minparacount))
  1838. ) then
  1839. internalerror(200212092);
  1840. { Convert tp procvars when not expecting a procvar }
  1841. if (def_to.typ<>procvardef) and
  1842. (currpt.left.resultdef.typ=procvardef) and
  1843. { Only convert to call when there is no overload or the return type
  1844. is equal to the expected type. }
  1845. (
  1846. (count=1) or
  1847. equal_defs(tprocvardef(currpt.left.resultdef).returndef,def_to)
  1848. ) then
  1849. begin
  1850. releasecurrpt:=true;
  1851. currpt:=tcallparanode(pt.getcopy);
  1852. if maybe_call_procvar(currpt.left,true) then
  1853. begin
  1854. currpt.resultdef:=currpt.left.resultdef;
  1855. def_from:=currpt.left.resultdef;
  1856. end;
  1857. end;
  1858. { If we expect a procvar and the left is loadnode that
  1859. returns a procdef we need to find the correct overloaded
  1860. procdef that matches the expected procvar. The loadnode
  1861. temporary returned the first procdef (PFV) }
  1862. if (def_to.typ=procvardef) and
  1863. (currpt.left.nodetype=loadn) and
  1864. (currpt.left.resultdef.typ=procdef) then
  1865. begin
  1866. pdtemp:=tprocsym(Tloadnode(currpt.left).symtableentry).Find_procdef_byprocvardef(Tprocvardef(def_to));
  1867. if assigned(pdtemp) then
  1868. begin
  1869. tloadnode(currpt.left).setprocdef(pdtemp);
  1870. currpt.resultdef:=currpt.left.resultdef;
  1871. def_from:=currpt.left.resultdef;
  1872. end;
  1873. end;
  1874. { varargs are always equal, but not exact }
  1875. if (po_varargs in hp^.data.procoptions) and
  1876. (currparanr>hp^.data.minparacount) and
  1877. not is_array_of_const(def_from) and
  1878. not is_array_constructor(def_from) then
  1879. begin
  1880. eq:=te_equal;
  1881. end
  1882. else
  1883. { same definition -> exact }
  1884. if (def_from=def_to) then
  1885. begin
  1886. eq:=te_exact;
  1887. end
  1888. else
  1889. { for value and const parameters check if a integer is constant or
  1890. included in other integer -> equal and calc ordinal_distance }
  1891. if not(currpara.varspez in [vs_var,vs_out]) and
  1892. is_integer(def_from) and
  1893. is_integer(def_to) and
  1894. is_in_limit(def_from,def_to) then
  1895. begin
  1896. eq:=te_equal;
  1897. hp^.ordinal_distance:=hp^.ordinal_distance+
  1898. abs(bestreal(torddef(def_from).low)-bestreal(torddef(def_to).low));
  1899. if (torddef(def_to).ordtype=u64bit) then
  1900. rth:=bestreal(qword(torddef(def_to).high))
  1901. else
  1902. rth:=bestreal(torddef(def_to).high);
  1903. if (torddef(def_from).ordtype=u64bit) then
  1904. rfh:=bestreal(qword(torddef(def_from).high))
  1905. else
  1906. rfh:=bestreal(torddef(def_from).high);
  1907. hp^.ordinal_distance:=hp^.ordinal_distance+abs(rth-rfh);
  1908. { Give wrong sign a small penalty, this is need to get a diffrence
  1909. from word->[longword,longint] }
  1910. if is_signed(def_from)<>is_signed(def_to) then
  1911. hp^.ordinal_distance:=hp^.ordinal_distance+1.0;
  1912. end
  1913. else
  1914. { for value and const parameters check precision of real, give
  1915. penalty for loosing of precision. var and out parameters must match exactly }
  1916. if not(currpara.varspez in [vs_var,vs_out]) and
  1917. is_real(def_from) and
  1918. is_real(def_to) then
  1919. begin
  1920. eq:=te_equal;
  1921. if is_extended(def_to) then
  1922. rth:=bestreal(4)
  1923. else
  1924. if is_double (def_to) then
  1925. rth:=bestreal(2)
  1926. else
  1927. rth:=bestreal(1);
  1928. if is_extended(def_from) then
  1929. rfh:=bestreal(4)
  1930. else
  1931. if is_double (def_from) then
  1932. rfh:=bestreal(2)
  1933. else
  1934. rfh:=bestreal(1);
  1935. { penalty for shrinking of precision }
  1936. if rth<rfh then
  1937. rfh:=(rfh-rth)*16
  1938. else
  1939. rfh:=rth-rfh;
  1940. hp^.ordinal_distance:=hp^.ordinal_distance+rfh;
  1941. end
  1942. else
  1943. { related object parameters also need to determine the distance between the current
  1944. object and the object we are comparing with. var and out parameters must match exactly }
  1945. if not(currpara.varspez in [vs_var,vs_out]) and
  1946. (def_from.typ=objectdef) and
  1947. (def_to.typ=objectdef) and
  1948. (tobjectdef(def_from).objecttype=tobjectdef(def_to).objecttype) and
  1949. tobjectdef(def_from).is_related(tobjectdef(def_to)) then
  1950. begin
  1951. eq:=te_convert_l1;
  1952. objdef:=tobjectdef(def_from);
  1953. while assigned(objdef) do
  1954. begin
  1955. if objdef=def_to then
  1956. break;
  1957. hp^.ordinal_distance:=hp^.ordinal_distance+1;
  1958. objdef:=objdef.childof;
  1959. end;
  1960. end
  1961. else
  1962. { generic type comparision }
  1963. begin
  1964. eq:=compare_defs_ext(def_from,def_to,currpt.left.nodetype,convtype,pdoper,cdoptions);
  1965. { when the types are not equal we need to check
  1966. some special case for parameter passing }
  1967. if (eq<te_equal) then
  1968. begin
  1969. if currpara.varspez in [vs_var,vs_out] then
  1970. begin
  1971. { para requires an equal type so the previous found
  1972. match was not good enough, reset to incompatible }
  1973. eq:=te_incompatible;
  1974. { var_para_allowed will return te_equal and te_convert_l1 to
  1975. make a difference for best matching }
  1976. var_para_allowed(eq,currpt.resultdef,currpara.vardef)
  1977. end
  1978. else
  1979. para_allowed(eq,currpt,def_to);
  1980. end;
  1981. end;
  1982. { when a procvar was changed to a call an exact much is
  1983. downgraded to equal. This way an overload call with the
  1984. procvar is choosen. See tb0471 (PFV) }
  1985. if (pt<>currpt) and (eq=te_exact) then
  1986. eq:=te_equal;
  1987. { increase correct counter }
  1988. case eq of
  1989. te_exact :
  1990. inc(hp^.exact_count);
  1991. te_equal :
  1992. inc(hp^.equal_count);
  1993. te_convert_l1 :
  1994. inc(hp^.cl1_count);
  1995. te_convert_l2 :
  1996. inc(hp^.cl2_count);
  1997. te_convert_l3 :
  1998. inc(hp^.cl3_count);
  1999. te_convert_operator :
  2000. inc(hp^.coper_count);
  2001. te_incompatible :
  2002. hp^.invalid:=true;
  2003. else
  2004. internalerror(200212072);
  2005. end;
  2006. { stop checking when an incompatible parameter is found }
  2007. if hp^.invalid then
  2008. begin
  2009. { store the current parameter info for
  2010. a nice error message when no procedure is found }
  2011. hp^.wrongparaidx:=paraidx;
  2012. hp^.wrongparanr:=currparanr;
  2013. break;
  2014. end;
  2015. {$ifdef EXTDEBUG}
  2016. { store equal in node tree for dump }
  2017. currpara.eqval:=eq;
  2018. {$endif EXTDEBUG}
  2019. { maybe release temp currpt }
  2020. if releasecurrpt then
  2021. currpt.free;
  2022. { next parameter in the call tree }
  2023. pt:=tcallparanode(pt.right);
  2024. { next parameter for definition, only goto next para
  2025. if we're out of the varargs }
  2026. if not(po_varargs in hp^.data.procoptions) or
  2027. (currparanr<=hp^.data.maxparacount) then
  2028. begin
  2029. { Ignore vs_hidden parameters }
  2030. repeat
  2031. dec(paraidx);
  2032. until (paraidx<0) or not(vo_is_hidden_para in tparavarsym(hp^.data.paras[paraidx]).varoptions);
  2033. end;
  2034. dec(currparanr);
  2035. end;
  2036. if not(hp^.invalid) and
  2037. (assigned(pt) or (paraidx>=0) or (currparanr<>0)) then
  2038. internalerror(200212141);
  2039. { next candidate }
  2040. hp:=hp^.next;
  2041. end;
  2042. end;
  2043. function get_variantequaltype(def: tdef): tvariantequaltype;
  2044. const
  2045. variantorddef_cl: array[tordtype] of tvariantequaltype =
  2046. (tve_incompatible,tve_byte,tve_word,tve_cardinal,tve_chari64,
  2047. tve_shortint,tve_smallint,tve_longint,tve_chari64,
  2048. tve_boolformal,tve_boolformal,tve_boolformal,tve_boolformal,
  2049. tve_chari64,tve_chari64,tve_dblcurrency);
  2050. {$warning fixme for 128 bit floats }
  2051. variantfloatdef_cl: array[tfloattype] of tvariantequaltype =
  2052. (tve_single,tve_dblcurrency,tve_extended,
  2053. tve_dblcurrency,tve_dblcurrency,tve_extended);
  2054. variantstringdef_cl: array[tstringtype] of tvariantequaltype =
  2055. (tve_sstring,tve_astring,tve_astring,tve_wstring);
  2056. begin
  2057. result:=tve_incompatible;
  2058. case def.typ of
  2059. orddef:
  2060. begin
  2061. result:=variantorddef_cl[torddef(def).ordtype];
  2062. end;
  2063. floatdef:
  2064. begin
  2065. result:=variantfloatdef_cl[tfloatdef(def).floattype];
  2066. end;
  2067. stringdef:
  2068. begin
  2069. result:=variantstringdef_cl[tstringdef(def).stringtype];
  2070. end;
  2071. formaldef:
  2072. begin
  2073. result:=tve_boolformal;
  2074. end;
  2075. else
  2076. internalerror(2006122804);
  2077. end
  2078. end;
  2079. function is_better_candidate(currpd,bestpd:pcandidate):integer;
  2080. var
  2081. res : integer;
  2082. begin
  2083. {
  2084. Return values:
  2085. > 0 when currpd is better than bestpd
  2086. < 0 when bestpd is better than currpd
  2087. = 0 when both are equal
  2088. To choose the best candidate we use the following order:
  2089. - Incompatible flag
  2090. - (Smaller) Number of convert operator parameters.
  2091. - (Smaller) Number of convertlevel 2 parameters.
  2092. - (Smaller) Number of convertlevel 1 parameters.
  2093. - (Bigger) Number of exact parameters.
  2094. - (Smaller) Number of equal parameters.
  2095. - (Smaller) Total of ordinal distance. For example, the distance of a word
  2096. to a byte is 65535-255=65280.
  2097. }
  2098. if bestpd^.invalid then
  2099. begin
  2100. if currpd^.invalid then
  2101. res:=0
  2102. else
  2103. res:=1;
  2104. end
  2105. else
  2106. if currpd^.invalid then
  2107. res:=-1
  2108. else
  2109. begin
  2110. { less operator parameters? }
  2111. res:=(bestpd^.coper_count-currpd^.coper_count);
  2112. if (res=0) then
  2113. begin
  2114. { less cl3 parameters? }
  2115. res:=(bestpd^.cl3_count-currpd^.cl3_count);
  2116. if (res=0) then
  2117. begin
  2118. { less cl2 parameters? }
  2119. res:=(bestpd^.cl2_count-currpd^.cl2_count);
  2120. if (res=0) then
  2121. begin
  2122. { less cl1 parameters? }
  2123. res:=(bestpd^.cl1_count-currpd^.cl1_count);
  2124. if (res=0) then
  2125. begin
  2126. { more exact parameters? }
  2127. res:=(currpd^.exact_count-bestpd^.exact_count);
  2128. if (res=0) then
  2129. begin
  2130. { less equal parameters? }
  2131. res:=(bestpd^.equal_count-currpd^.equal_count);
  2132. if (res=0) then
  2133. begin
  2134. { smaller ordinal distance? }
  2135. if (currpd^.ordinal_distance<bestpd^.ordinal_distance) then
  2136. res:=1
  2137. else
  2138. if (currpd^.ordinal_distance>bestpd^.ordinal_distance) then
  2139. res:=-1
  2140. else
  2141. res:=0;
  2142. end;
  2143. end;
  2144. end;
  2145. end;
  2146. end;
  2147. end;
  2148. end;
  2149. is_better_candidate:=res;
  2150. end;
  2151. { Delphi precedence rules extracted from test programs. Only valid if passing
  2152. a variant parameter to overloaded procedures expecting exactly one parameter.
  2153. single > (char, currency, int64, shortstring, ansistring, widestring, extended, double)
  2154. double/currency > (char, int64, shortstring, ansistring, widestring, extended)
  2155. extended > (char, int64, shortstring, ansistring, widestring)
  2156. longint/cardinal > (int64, shortstring, ansistring, widestring, extended, double, single, char, currency)
  2157. smallint > (longint, int64, shortstring, ansistring, widestring, extended, double single, char, currency);
  2158. word > (longint, cardinal, int64, shortstring, ansistring, widestring, extended, double single, char, currency);
  2159. shortint > (longint, smallint, int64, shortstring, ansistring, widestring, extended, double, single, char, currency)
  2160. byte > (longint, cardinal, word, smallint, int64, shortstring, ansistring, widestring, extended, double, single, char, currency);
  2161. boolean/formal > (char, int64, shortstring, ansistring, widestring)
  2162. shortstring > (char, int64, ansistring, widestring)
  2163. ansistring > (char, int64, widestring)
  2164. widestring > (char, int64)
  2165. Relations not mentioned mean that they conflict: no decision possible }
  2166. function is_better_candidate_single_variant(currpd,bestpd:pcandidate):integer;
  2167. function calculate_relation(const currvcl, bestvcl, testvcl:
  2168. tvariantequaltype; const conflictvcls: tvariantequaltypes):integer;
  2169. begin
  2170. { if (bestvcl=conflictvcl) or
  2171. (currvcl=conflictvcl) then
  2172. result:=0
  2173. else if (bestvcl=testvcl) then
  2174. result:=-1
  2175. else result:=1 }
  2176. result:=1-2*ord(bestvcl=testvcl)+
  2177. ord(currvcl in conflictvcls)-ord(bestvcl in conflictvcls);
  2178. end;
  2179. var
  2180. paraidx,
  2181. res: integer;
  2182. currpara, bestpara: tparavarsym;
  2183. currvcl, bestvcl: tvariantequaltype;
  2184. begin
  2185. {
  2186. Return values:
  2187. > 0 when currpd is better than bestpd
  2188. < 0 when bestpd is better than currpd
  2189. = 0 when both are equal
  2190. }
  2191. if (currpd^.firstparaidx<>bestpd^.firstparaidx) then
  2192. internalerror(2006122801);
  2193. paraidx:=currpd^.firstparaidx;
  2194. while (paraidx>=0) and (vo_is_hidden_para in tparavarsym(currpd^.data.paras[paraidx]).varoptions) do
  2195. if (vo_is_hidden_para in tparavarsym(bestpd^.data.paras[paraidx]).varoptions) then
  2196. dec(paraidx)
  2197. else
  2198. internalerror(2006122802);
  2199. if (vo_is_hidden_para in tparavarsym(currpd^.data.paras[paraidx]).varoptions) then
  2200. internalerror(2006122803);
  2201. currpara:=tparavarsym(currpd^.data.paras[paraidx]);
  2202. bestpara:=tparavarsym(bestpd^.data.paras[paraidx]);
  2203. { if one of the parameters is a regular variant, fall back to the }
  2204. { default algorithm }
  2205. if (currpara.vardef.typ = variantdef) or
  2206. (bestpara.vardef.typ = variantdef) then
  2207. begin
  2208. result:=is_better_candidate(currpd,bestpd);
  2209. exit;
  2210. end;
  2211. currvcl:=get_variantequaltype(currpara.vardef);
  2212. bestvcl:=get_variantequaltype(bestpara.vardef);
  2213. { sanity check }
  2214. result:=-5;
  2215. { if both are the same, there is a conflict }
  2216. if (currvcl=bestvcl) then
  2217. result:=0
  2218. { boolean and formal are better than chari64str, but conflict with }
  2219. { everything else }
  2220. else if (currvcl=tve_boolformal) or
  2221. (bestvcl=tve_boolformal) then
  2222. if (currvcl=tve_boolformal) then
  2223. result:=ord(bestvcl in [tve_chari64,tve_sstring,tve_astring,tve_wstring])
  2224. else
  2225. result:=-ord(currvcl in [tve_chari64,tve_sstring,tve_astring,tve_wstring])
  2226. { byte is better than everything else (we assume both aren't byte, }
  2227. { since there's only one parameter and that one can't be the same) }
  2228. else if (currvcl=tve_byte) or
  2229. (bestvcl=tve_byte) then
  2230. result:=calculate_relation(currvcl,bestvcl,tve_byte,[tve_shortint])
  2231. { shortint conflicts with word and cardinal, but is better than }
  2232. { everything else but byte (which has already been handled) }
  2233. else if (currvcl=tve_shortint) or
  2234. (bestvcl=tve_shortint) then
  2235. result:=calculate_relation(currvcl,bestvcl,tve_shortint,[tve_word, tve_cardinal])
  2236. { word conflicts with smallint, but is better than everything else }
  2237. { but shortint and byte (which has already been handled) }
  2238. else if (currvcl=tve_word) or
  2239. (bestvcl=tve_word) then
  2240. result:=calculate_relation(currvcl,bestvcl,tve_word,[tve_smallint])
  2241. { smallint conflicts with cardinal, but is better than everything }
  2242. { which has not yet been tested }
  2243. else if (currvcl=tve_smallint) or
  2244. (bestvcl=tve_smallint) then
  2245. result:=calculate_relation(currvcl,bestvcl,tve_smallint,[tve_cardinal])
  2246. { cardinal conflicts with each longint and is better than everything }
  2247. { which has not yet been tested }
  2248. else if (currvcl = tve_cardinal) or
  2249. (bestvcl=tve_cardinal) then
  2250. result:=calculate_relation(currvcl,bestvcl,tve_cardinal,[tve_longint])
  2251. { longint is better than everything which has not yet been tested }
  2252. else if (currvcl=tve_longint) or
  2253. (bestvcl=tve_longint) then
  2254. { if bestvcl=tve_longint then
  2255. result:=-1
  2256. else
  2257. result:=1 }
  2258. result:=1-2*ord(bestvcl=tve_longint)
  2259. { single is better than everything left }
  2260. else if (currvcl=tve_single) or
  2261. (bestvcl=tve_single) then
  2262. result:=1-2*ord(bestvcl=tve_single)
  2263. { double/comp/currency are better than everything left, and conflict }
  2264. { with each other (but that's already tested) }
  2265. else if (currvcl=tve_dblcurrency) or
  2266. (bestvcl=tve_dblcurrency) then
  2267. result:=1-2*ord(bestvcl=tve_dblcurrency)
  2268. { extended is better than everything left }
  2269. else if (currvcl=tve_extended) or
  2270. (bestvcl=tve_extended) then
  2271. result:=1-2*ord(bestvcl=tve_extended)
  2272. { shortstring is better than everything left }
  2273. else if (currvcl=tve_sstring) or
  2274. (bestvcl=tve_sstring) then
  2275. result:=1-2*ord(bestvcl=tve_sstring)
  2276. { ansistring is better than everything left }
  2277. else if (currvcl=tve_astring) or
  2278. (bestvcl=tve_astring) then
  2279. result:=1-2*ord(bestvcl=tve_astring)
  2280. { widestring is better than everything left }
  2281. else if (currvcl=tve_wstring) or
  2282. (bestvcl=tve_wstring) then
  2283. result:=1-2*ord(bestvcl=tve_wstring);
  2284. { all possibilities should have been checked now }
  2285. if (result=-5) then
  2286. internalerror(2006122805);
  2287. end;
  2288. function tcallcandidates.choose_best(var bestpd:tabstractprocdef; singlevariant: boolean):integer;
  2289. var
  2290. besthpstart,
  2291. hp : pcandidate;
  2292. cntpd,
  2293. res : integer;
  2294. begin
  2295. {
  2296. Returns the number of candidates left and the
  2297. first candidate is returned in pdbest
  2298. }
  2299. { Setup the first procdef as best, only count it as a result
  2300. when it is valid }
  2301. bestpd:=FProcs^.data;
  2302. if FProcs^.invalid then
  2303. cntpd:=0
  2304. else
  2305. cntpd:=1;
  2306. if assigned(FProcs^.next) then
  2307. begin
  2308. besthpstart:=FProcs;
  2309. hp:=FProcs^.next;
  2310. while assigned(hp) do
  2311. begin
  2312. if not singlevariant then
  2313. res:=is_better_candidate(hp,besthpstart)
  2314. else
  2315. res:=is_better_candidate_single_variant(hp,besthpstart);
  2316. if (res>0) then
  2317. begin
  2318. { hp is better, flag all procs to be incompatible }
  2319. while (besthpstart<>hp) do
  2320. begin
  2321. besthpstart^.invalid:=true;
  2322. besthpstart:=besthpstart^.next;
  2323. end;
  2324. { besthpstart is already set to hp }
  2325. bestpd:=besthpstart^.data;
  2326. cntpd:=1;
  2327. end
  2328. else
  2329. if (res<0) then
  2330. begin
  2331. { besthpstart is better, flag current hp to be incompatible }
  2332. hp^.invalid:=true;
  2333. end
  2334. else
  2335. begin
  2336. { res=0, both are valid }
  2337. if not hp^.invalid then
  2338. inc(cntpd);
  2339. end;
  2340. hp:=hp^.next;
  2341. end;
  2342. end;
  2343. result:=cntpd;
  2344. end;
  2345. procedure tcallcandidates.find_wrong_para;
  2346. var
  2347. currparanr : smallint;
  2348. hp : pcandidate;
  2349. pt : tcallparanode;
  2350. wrongpara : tparavarsym;
  2351. begin
  2352. { Only process the first overloaded procdef }
  2353. hp:=FProcs;
  2354. { Find callparanode corresponding to the argument }
  2355. pt:=tcallparanode(FParanode);
  2356. currparanr:=FParalength;
  2357. while assigned(pt) and
  2358. (currparanr>hp^.wrongparanr) do
  2359. begin
  2360. pt:=tcallparanode(pt.right);
  2361. dec(currparanr);
  2362. end;
  2363. if (currparanr<>hp^.wrongparanr) or
  2364. not assigned(pt) then
  2365. internalerror(200212094);
  2366. { Show error message, when it was a var or out parameter
  2367. guess that it is a missing typeconv }
  2368. wrongpara:=tparavarsym(hp^.data.paras[hp^.wrongparaidx]);
  2369. if wrongpara.varspez in [vs_var,vs_out] then
  2370. begin
  2371. { Maybe passing the correct type but passing a const to var parameter }
  2372. if (compare_defs(pt.resultdef,wrongpara.vardef,pt.nodetype)<>te_incompatible) and
  2373. not valid_for_var(pt.left,true) then
  2374. CGMessagePos(pt.left.fileinfo,type_e_variable_id_expected)
  2375. else
  2376. CGMessagePos3(pt.left.fileinfo,parser_e_call_by_ref_without_typeconv,tostr(hp^.wrongparanr),
  2377. FullTypeName(pt.left.resultdef,wrongpara.vardef),
  2378. FullTypeName(wrongpara.vardef,pt.left.resultdef))
  2379. end
  2380. else
  2381. CGMessagePos3(pt.left.fileinfo,type_e_wrong_parameter_type,tostr(hp^.wrongparanr),
  2382. FullTypeName(pt.left.resultdef,wrongpara.vardef),
  2383. FullTypeName(wrongpara.vardef,pt.left.resultdef));
  2384. end;
  2385. procedure check_hints(const srsym: tsym; const symoptions: tsymoptions);
  2386. begin
  2387. if not assigned(srsym) then
  2388. internalerror(200602051);
  2389. if sp_hint_deprecated in symoptions then
  2390. Message1(sym_w_deprecated_symbol,srsym.realname);
  2391. if sp_hint_platform in symoptions then
  2392. Message1(sym_w_non_portable_symbol,srsym.realname);
  2393. if sp_hint_unimplemented in symoptions then
  2394. Message1(sym_w_non_implemented_symbol,srsym.realname);
  2395. end;
  2396. procedure check_ranges(const location: tfileposinfo; source: tnode; destdef: tdef);
  2397. begin
  2398. { check if the assignment may cause a range check error }
  2399. { if its not explicit, and only if the values are }
  2400. { ordinals, enumdef and floatdef }
  2401. if assigned(destdef) and
  2402. (destdef.typ in [enumdef,orddef,floatdef]) and
  2403. not is_boolean(destdef) and
  2404. assigned(source.resultdef) and
  2405. (source.resultdef.typ in [enumdef,orddef,floatdef]) and
  2406. not is_boolean(source.resultdef) and
  2407. not is_constrealnode(source) then
  2408. begin
  2409. if (destdef.size < source.resultdef.size) then
  2410. begin
  2411. if (cs_check_range in current_settings.localswitches) then
  2412. MessagePos(location,type_w_smaller_possible_range_check)
  2413. else
  2414. MessagePos(location,type_h_smaller_possible_range_check);
  2415. end;
  2416. end;
  2417. end;
  2418. end.