htypechk.pas 98 KB

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