htypechk.pas 97 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557
  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(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 : longint;
  836. begin
  837. if valid_const in opts then
  838. errmsg:=type_e_variable_id_expected
  839. else
  840. errmsg:=type_e_argument_cant_be_assigned;
  841. result:=false;
  842. gotsubscript:=false;
  843. gotvec:=false;
  844. gotderef:=false;
  845. gotrecord:=false;
  846. gotclass:=false;
  847. gotpointer:=false;
  848. gotdynarray:=false;
  849. gotstring:=false;
  850. hp:=p;
  851. if not(valid_void in opts) and
  852. is_void(hp.resultdef) then
  853. begin
  854. if report_errors then
  855. CGMessagePos(hp.fileinfo,errmsg);
  856. exit;
  857. end;
  858. while assigned(hp) do
  859. begin
  860. { property allowed? calln has a property check itself }
  861. if (nf_isproperty in hp.flags) then
  862. begin
  863. if (hp.nodetype=calln) then
  864. begin
  865. { check return type }
  866. case hp.resultdef.typ of
  867. pointerdef :
  868. gotpointer:=true;
  869. objectdef :
  870. gotclass:=is_class_or_interface(hp.resultdef);
  871. recorddef :
  872. gotrecord:=true;
  873. classrefdef :
  874. gotclass:=true;
  875. stringdef :
  876. gotstring:=true;
  877. end;
  878. if (valid_property in opts) then
  879. begin
  880. { don't allow writing to calls that will create
  881. temps like calls that return a structure and we
  882. are assigning to a member }
  883. if (valid_const in opts) or
  884. not(
  885. (gotsubscript and gotrecord) or
  886. (gotstring and gotvec)
  887. ) then
  888. result:=true
  889. else
  890. if report_errors then
  891. CGMessagePos(hp.fileinfo,errmsg);
  892. end
  893. else
  894. begin
  895. { 1. if it returns a pointer and we've found a deref,
  896. 2. if it returns a class or record and a subscription or with is found
  897. 3. if the address is needed of a field (subscriptn) }
  898. if (gotpointer and gotderef) or
  899. (gotstring and gotvec) or
  900. (
  901. (gotclass or gotrecord) and
  902. (gotsubscript)
  903. ) or
  904. (
  905. (gotvec and gotdynarray)
  906. ) or
  907. (
  908. (Valid_Addr in opts) and
  909. (hp.nodetype=subscriptn)
  910. ) then
  911. result:=true
  912. else
  913. if report_errors then
  914. CGMessagePos(hp.fileinfo,errmsg);
  915. end;
  916. end
  917. else
  918. result:=true;
  919. exit;
  920. end;
  921. if (Valid_Const in opts) and is_constnode(hp) then
  922. begin
  923. result:=true;
  924. exit;
  925. end;
  926. case hp.nodetype of
  927. temprefn :
  928. begin
  929. valid_for_assign := true;
  930. exit;
  931. end;
  932. derefn :
  933. begin
  934. gotderef:=true;
  935. hp:=tderefnode(hp).left;
  936. end;
  937. typeconvn :
  938. begin
  939. { typecast sizes must match, exceptions:
  940. - implicit typecast made by absolute
  941. - from formaldef
  942. - from void
  943. - from/to open array
  944. - typecast from pointer to array }
  945. fromdef:=ttypeconvnode(hp).left.resultdef;
  946. todef:=hp.resultdef;
  947. if not((nf_absolute in ttypeconvnode(hp).flags) or
  948. (fromdef.typ=formaldef) or
  949. is_void(fromdef) or
  950. is_open_array(fromdef) or
  951. is_open_array(todef) or
  952. ((fromdef.typ=pointerdef) and (todef.typ=arraydef)) or
  953. ((fromdef.typ = objectdef) and (todef.typ = objectdef) and
  954. (tobjectdef(fromdef).is_related(tobjectdef(todef))))) and
  955. (fromdef.size<>todef.size) then
  956. begin
  957. { in TP it is allowed to typecast to smaller types. But the variable can't
  958. be in a register }
  959. if (m_tp7 in current_settings.modeswitches) or
  960. (todef.size<fromdef.size) then
  961. make_not_regable(hp,vr_addr)
  962. else
  963. if report_errors then
  964. CGMessagePos2(hp.fileinfo,type_e_typecast_wrong_size_for_assignment,tostr(fromdef.size),tostr(todef.size));
  965. end;
  966. { don't allow assignments to typeconvs that need special code }
  967. if not(gotsubscript or gotvec or gotderef) and
  968. not(ttypeconvnode(hp).assign_allowed) then
  969. begin
  970. if report_errors then
  971. CGMessagePos(hp.fileinfo,errmsg);
  972. exit;
  973. end;
  974. case hp.resultdef.typ of
  975. pointerdef :
  976. gotpointer:=true;
  977. objectdef :
  978. gotclass:=is_class_or_interface(hp.resultdef);
  979. classrefdef :
  980. gotclass:=true;
  981. arraydef :
  982. begin
  983. { pointer -> array conversion is done then we need to see it
  984. as a deref, because a ^ is then not required anymore }
  985. if (ttypeconvnode(hp).left.resultdef.typ=pointerdef) then
  986. gotderef:=true;
  987. end;
  988. end;
  989. hp:=ttypeconvnode(hp).left;
  990. end;
  991. vecn :
  992. begin
  993. if { only check for first (= outermost) vec node }
  994. not gotvec and
  995. not(valid_packed in opts) and
  996. (tvecnode(hp).left.resultdef.typ = arraydef) and
  997. (ado_IsBitPacked in tarraydef(tvecnode(hp).left.resultdef).arrayoptions) and
  998. (tarraydef(tvecnode(hp).left.resultdef).elepackedbitsize mod 8 <> 0) then
  999. begin
  1000. if report_errors then
  1001. if (valid_property in opts) then
  1002. CGMessagePos(hp.fileinfo,parser_e_packed_element_no_loop)
  1003. else
  1004. CGMessagePos(hp.fileinfo,parser_e_packed_element_no_var_addr);
  1005. exit;
  1006. end;
  1007. gotvec:=true;
  1008. { accesses to dyn. arrays override read only access in delphi }
  1009. if (m_delphi in current_settings.modeswitches) and is_dynamic_array(tunarynode(hp).left.resultdef) then
  1010. gotdynarray:=true;
  1011. hp:=tunarynode(hp).left;
  1012. end;
  1013. blockn :
  1014. begin
  1015. hp2:=tblocknode(hp).statements;
  1016. if assigned(hp2) then
  1017. begin
  1018. if hp2.nodetype<>statementn then
  1019. internalerror(2006110801);
  1020. while assigned(tstatementnode(hp2).next) do
  1021. hp2:=tstatementnode(hp2).next;
  1022. hp:=tstatementnode(hp2).statement;
  1023. end
  1024. else
  1025. begin
  1026. if report_errors then
  1027. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  1028. exit;
  1029. end;
  1030. end;
  1031. asn :
  1032. begin
  1033. { asn can't be assigned directly, it returns the value in a register instead
  1034. of reference. }
  1035. if not(gotsubscript or gotderef or gotvec) then
  1036. begin
  1037. if report_errors then
  1038. CGMessagePos(hp.fileinfo,errmsg);
  1039. exit;
  1040. end;
  1041. hp:=tunarynode(hp).left;
  1042. end;
  1043. subscriptn :
  1044. begin
  1045. { only check first (= outermost) subscriptn }
  1046. if not gotsubscript and
  1047. not(valid_packed in opts) and
  1048. is_packed_record_or_object(tsubscriptnode(hp).left.resultdef) then
  1049. begin
  1050. if report_errors then
  1051. if (valid_property in opts) then
  1052. CGMessagePos(hp.fileinfo,parser_e_packed_element_no_loop)
  1053. else
  1054. CGMessagePos(hp.fileinfo,parser_e_packed_element_no_var_addr);
  1055. exit;
  1056. end;
  1057. gotsubscript:=true;
  1058. { loop counter? }
  1059. if not(Valid_Const in opts) and
  1060. (vo_is_loop_counter in tsubscriptnode(hp).vs.varoptions) then
  1061. begin
  1062. if report_errors then
  1063. CGMessage1(parser_e_illegal_assignment_to_count_var,tsubscriptnode(hp).vs.realname)
  1064. else
  1065. exit;
  1066. end;
  1067. { a class/interface access is an implicit }
  1068. { dereferencing }
  1069. hp:=tsubscriptnode(hp).left;
  1070. if is_class_or_interface(hp.resultdef) then
  1071. gotderef:=true;
  1072. end;
  1073. muln,
  1074. divn,
  1075. andn,
  1076. xorn,
  1077. orn,
  1078. notn,
  1079. subn,
  1080. addn :
  1081. begin
  1082. { Allow operators on a pointer, or an integer
  1083. and a pointer typecast and deref has been found }
  1084. if ((hp.resultdef.typ=pointerdef) or
  1085. (is_integer(hp.resultdef) and gotpointer)) and
  1086. gotderef then
  1087. result:=true
  1088. else
  1089. { Temp strings are stored in memory, for compatibility with
  1090. delphi only }
  1091. if (m_delphi in current_settings.modeswitches) and
  1092. ((valid_addr in opts) or
  1093. (valid_const in opts)) and
  1094. (hp.resultdef.typ=stringdef) then
  1095. result:=true
  1096. else
  1097. if report_errors then
  1098. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  1099. exit;
  1100. end;
  1101. niln,
  1102. pointerconstn :
  1103. begin
  1104. { to support e.g. @tmypointer(0)^.data; see tests/tbs/tb0481 }
  1105. if gotderef then
  1106. result:=true
  1107. else
  1108. if report_errors then
  1109. CGMessagePos(hp.fileinfo,type_e_no_assign_to_addr);
  1110. exit;
  1111. end;
  1112. addrn :
  1113. begin
  1114. if gotderef then
  1115. result:=true
  1116. else
  1117. if report_errors then
  1118. CGMessagePos(hp.fileinfo,type_e_no_assign_to_addr);
  1119. exit;
  1120. end;
  1121. calln :
  1122. begin
  1123. { check return type }
  1124. case hp.resultdef.typ of
  1125. arraydef :
  1126. begin
  1127. { dynamic arrays are allowed when there is also a
  1128. vec node }
  1129. if is_dynamic_array(hp.resultdef) and
  1130. gotvec then
  1131. begin
  1132. gotderef:=true;
  1133. gotpointer:=true;
  1134. end;
  1135. end;
  1136. pointerdef :
  1137. gotpointer:=true;
  1138. objectdef :
  1139. gotclass:=is_class_or_interface(hp.resultdef);
  1140. recorddef, { handle record like class it needs a subscription }
  1141. classrefdef :
  1142. gotclass:=true;
  1143. stringdef :
  1144. gotstring:=true;
  1145. end;
  1146. { 1. if it returns a pointer and we've found a deref,
  1147. 2. if it returns a class or record and a subscription or with is found
  1148. 3. string is returned }
  1149. if (gotstring and gotvec) or
  1150. (gotpointer and gotderef) or
  1151. (gotclass and gotsubscript) then
  1152. result:=true
  1153. else
  1154. { Temp strings are stored in memory, for compatibility with
  1155. delphi only }
  1156. if (m_delphi in current_settings.modeswitches) and
  1157. (valid_addr in opts) and
  1158. (hp.resultdef.typ=stringdef) then
  1159. result:=true
  1160. else
  1161. if ([valid_const,valid_addr] * opts = [valid_const]) then
  1162. result:=true
  1163. else
  1164. if report_errors then
  1165. CGMessagePos(hp.fileinfo,errmsg);
  1166. exit;
  1167. end;
  1168. inlinen :
  1169. begin
  1170. if ((valid_const in opts) and
  1171. (tinlinenode(hp).inlinenumber in [in_typeof_x]))
  1172. {$ifdef SUPPORT_UNALIGNED}
  1173. or (tinlinenode(hp).inlinenumber in [in_unaligned_x])
  1174. {$endif SUPPORT_UNALIGNED}
  1175. then
  1176. result:=true
  1177. else
  1178. if report_errors then
  1179. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  1180. exit;
  1181. end;
  1182. dataconstn:
  1183. begin
  1184. { only created internally, so no additional checks necessary }
  1185. result:=true;
  1186. exit;
  1187. end;
  1188. loadn :
  1189. begin
  1190. case tloadnode(hp).symtableentry.typ of
  1191. absolutevarsym,
  1192. staticvarsym,
  1193. localvarsym,
  1194. paravarsym :
  1195. begin
  1196. { loop counter? }
  1197. if not(Valid_Const in opts) and
  1198. not gotderef and
  1199. (vo_is_loop_counter in tabstractvarsym(tloadnode(hp).symtableentry).varoptions) then
  1200. if report_errors then
  1201. CGMessage1(parser_e_illegal_assignment_to_count_var,tloadnode(hp).symtableentry.realname)
  1202. else
  1203. exit;
  1204. { read-only variable? }
  1205. if (tabstractvarsym(tloadnode(hp).symtableentry).varspez=vs_const) then
  1206. begin
  1207. { allow p^:= constructions with p is const parameter }
  1208. if gotderef or gotdynarray or (Valid_Const in opts) or
  1209. (nf_isinternal_ignoreconst in tloadnode(hp).flags) then
  1210. result:=true
  1211. else
  1212. if report_errors then
  1213. CGMessagePos(tloadnode(hp).fileinfo,type_e_no_assign_to_const);
  1214. exit;
  1215. end;
  1216. result:=true;
  1217. exit;
  1218. end;
  1219. procsym :
  1220. begin
  1221. if (Valid_Const in opts) then
  1222. result:=true
  1223. else
  1224. if report_errors then
  1225. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  1226. exit;
  1227. end;
  1228. labelsym :
  1229. begin
  1230. if (Valid_Addr in opts) then
  1231. result:=true
  1232. else
  1233. if report_errors then
  1234. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  1235. exit;
  1236. end;
  1237. constsym:
  1238. begin
  1239. if (tconstsym(tloadnode(hp).symtableentry).consttyp=constresourcestring) and
  1240. (valid_addr in opts) then
  1241. result:=true
  1242. else
  1243. if report_errors then
  1244. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  1245. exit;
  1246. end;
  1247. else
  1248. begin
  1249. if report_errors then
  1250. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  1251. exit;
  1252. end;
  1253. end;
  1254. end;
  1255. else
  1256. begin
  1257. if report_errors then
  1258. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  1259. exit;
  1260. end;
  1261. end;
  1262. end;
  1263. end;
  1264. function valid_for_var(p:tnode; report_errors: boolean):boolean;
  1265. begin
  1266. valid_for_var:=valid_for_assign(p,[],report_errors);
  1267. end;
  1268. function valid_for_formal_var(p : tnode; report_errors: boolean) : boolean;
  1269. begin
  1270. valid_for_formal_var:=valid_for_assign(p,[valid_void],report_errors);
  1271. end;
  1272. function valid_for_formal_const(p : tnode; report_errors: boolean) : boolean;
  1273. begin
  1274. valid_for_formal_const:=(p.resultdef.typ=formaldef) or
  1275. valid_for_assign(p,[valid_void,valid_const,valid_property],report_errors);
  1276. end;
  1277. function valid_for_assignment(p:tnode; report_errors: boolean):boolean;
  1278. begin
  1279. valid_for_assignment:=valid_for_assign(p,[valid_property,valid_packed],report_errors);
  1280. end;
  1281. function valid_for_loopvar(p:tnode; report_errors: boolean):boolean;
  1282. begin
  1283. valid_for_loopvar:=valid_for_assign(p,[valid_property],report_errors);
  1284. end;
  1285. function valid_for_addr(p : tnode; report_errors: boolean) : boolean;
  1286. begin
  1287. result:=valid_for_assign(p,[valid_const,valid_addr,valid_void],report_errors);
  1288. end;
  1289. procedure var_para_allowed(var eq:tequaltype;def_from,def_to:Tdef);
  1290. begin
  1291. { Note: eq must be already valid, it will only be updated! }
  1292. case def_to.typ of
  1293. formaldef :
  1294. begin
  1295. { all types can be passed to a formaldef,
  1296. but it is not the prefered way }
  1297. eq:=te_convert_l2;
  1298. end;
  1299. orddef :
  1300. begin
  1301. { allows conversion from word to integer and
  1302. byte to shortint, but only for TP7 compatibility }
  1303. if (m_tp7 in current_settings.modeswitches) and
  1304. (def_from.typ=orddef) and
  1305. (def_from.size=def_to.size) then
  1306. eq:=te_convert_l1;
  1307. end;
  1308. arraydef :
  1309. begin
  1310. if is_open_array(def_to) then
  1311. begin
  1312. if is_dynamic_array(def_from) and
  1313. equal_defs(tarraydef(def_from).elementdef,tarraydef(def_to).elementdef) then
  1314. eq:=te_convert_l2
  1315. else
  1316. if equal_defs(def_from,tarraydef(def_to).elementdef) then
  1317. eq:=te_convert_l2;
  1318. end;
  1319. end;
  1320. pointerdef :
  1321. begin
  1322. { an implicit pointer conversion is allowed }
  1323. if (def_from.typ=pointerdef) then
  1324. eq:=te_convert_l1;
  1325. end;
  1326. stringdef :
  1327. begin
  1328. { all shortstrings are allowed, size is not important }
  1329. if is_shortstring(def_from) and
  1330. is_shortstring(def_to) then
  1331. eq:=te_equal;
  1332. end;
  1333. objectdef :
  1334. begin
  1335. { child objects can be also passed }
  1336. { in non-delphi mode, otherwise }
  1337. { they must match exactly, except }
  1338. { if they are objects }
  1339. if (def_from.typ=objectdef) and
  1340. (
  1341. not(m_delphi in current_settings.modeswitches) or
  1342. (
  1343. (tobjectdef(def_from).objecttype=odt_object) and
  1344. (tobjectdef(def_to).objecttype=odt_object)
  1345. )
  1346. ) and
  1347. (tobjectdef(def_from).is_related(tobjectdef(def_to))) then
  1348. eq:=te_convert_l1;
  1349. end;
  1350. filedef :
  1351. begin
  1352. { an implicit file conversion is also allowed }
  1353. { from a typed file to an untyped one }
  1354. if (def_from.typ=filedef) and
  1355. (tfiledef(def_from).filetyp = ft_typed) and
  1356. (tfiledef(def_to).filetyp = ft_untyped) then
  1357. eq:=te_convert_l1;
  1358. end;
  1359. end;
  1360. end;
  1361. procedure para_allowed(var eq:tequaltype;p:tcallparanode;def_to:tdef);
  1362. begin
  1363. { Note: eq must be already valid, it will only be updated! }
  1364. case def_to.typ of
  1365. formaldef :
  1366. begin
  1367. { all types can be passed to a formaldef }
  1368. eq:=te_equal;
  1369. end;
  1370. stringdef :
  1371. begin
  1372. { to support ansi/long/wide strings in a proper way }
  1373. { string and string[10] are assumed as equal }
  1374. { when searching the correct overloaded procedure }
  1375. if (p.resultdef.typ=stringdef) and
  1376. (tstringdef(def_to).stringtype=tstringdef(p.resultdef).stringtype) then
  1377. eq:=te_equal
  1378. else
  1379. { Passing a constant char to ansistring or shortstring or
  1380. a widechar to widestring then handle it as equal. }
  1381. if (p.left.nodetype=ordconstn) and
  1382. (
  1383. is_char(p.resultdef) and
  1384. (is_shortstring(def_to) or is_ansistring(def_to))
  1385. ) or
  1386. (
  1387. is_widechar(p.resultdef) and
  1388. is_widestring(def_to)
  1389. ) then
  1390. eq:=te_equal
  1391. end;
  1392. setdef :
  1393. begin
  1394. { set can also be a not yet converted array constructor }
  1395. if (p.resultdef.typ=arraydef) and
  1396. is_array_constructor(p.resultdef) and
  1397. not is_variant_array(p.resultdef) then
  1398. eq:=te_equal;
  1399. end;
  1400. procvardef :
  1401. begin
  1402. { in tp7 mode proc -> procvar is allowed }
  1403. if ((m_tp_procvar in current_settings.modeswitches) or
  1404. (m_mac_procvar in current_settings.modeswitches)) and
  1405. (p.left.nodetype=calln) and
  1406. (proc_to_procvar_equal(tprocdef(tcallnode(p.left).procdefinition),tprocvardef(def_to))>=te_equal) then
  1407. eq:=te_equal
  1408. else
  1409. if (m_mac_procvar in current_settings.modeswitches) and
  1410. is_procvar_load(p.left) then
  1411. eq:=te_convert_l2;
  1412. end;
  1413. end;
  1414. end;
  1415. function allowenumop(nt:tnodetype):boolean;
  1416. begin
  1417. result:=(nt in [equaln,unequaln,ltn,lten,gtn,gten]) or
  1418. ((cs_allow_enum_calc in current_settings.localswitches) and
  1419. (nt in [addn,subn]));
  1420. end;
  1421. {****************************************************************************
  1422. TCallCandidates
  1423. ****************************************************************************}
  1424. constructor tcallcandidates.create(sym:tprocsym;st:TSymtable;ppn:tnode;isprop,ignorevis : boolean);
  1425. var
  1426. j : integer;
  1427. pd : tprocdef;
  1428. hp : pcandidate;
  1429. found,
  1430. has_overload_directive : boolean;
  1431. topclassh : tobjectdef;
  1432. srsymtable : TSymtable;
  1433. srprocsym : tprocsym;
  1434. pt : tcallparanode;
  1435. checkstack : psymtablestackitem;
  1436. hashedid : THashedIDString;
  1437. begin
  1438. if not assigned(sym) then
  1439. internalerror(200411015);
  1440. FProcSym:=sym;
  1441. FProcs:=nil;
  1442. FProccnt:=0;
  1443. FProcvisiblecnt:=0;
  1444. FParanode:=ppn;
  1445. FAllowVariant:=true;
  1446. { determine length of parameter list }
  1447. pt:=tcallparanode(ppn);
  1448. FParalength:=0;
  1449. while assigned(pt) do
  1450. begin
  1451. inc(FParalength);
  1452. pt:=tcallparanode(pt.right);
  1453. end;
  1454. { when the definition has overload directive set, we search for
  1455. overloaded definitions in the class, this only needs to be done once
  1456. for class entries as the tree keeps always the same }
  1457. if (not sym.overloadchecked) and
  1458. (sym.owner.symtabletype=ObjectSymtable) and
  1459. (po_overload in tprocdef(sym.ProcdefList[0]).procoptions) then
  1460. search_class_overloads(sym);
  1461. { when the class passed is defined in this unit we
  1462. need to use the scope of that class. This is a trick
  1463. that can be used to access protected members in other
  1464. units. At least kylix supports it this way (PFV) }
  1465. if assigned(st) and
  1466. (
  1467. (st.symtabletype=ObjectSymtable) or
  1468. ((st.symtabletype=withsymtable) and
  1469. (st.defowner.typ=objectdef))
  1470. ) and
  1471. (st.defowner.owner.symtabletype in [globalsymtable,staticsymtable]) and
  1472. st.defowner.owner.iscurrentunit then
  1473. topclassh:=tobjectdef(st.defowner)
  1474. else
  1475. begin
  1476. if assigned(current_procinfo) then
  1477. topclassh:=current_procinfo.procdef._class
  1478. else
  1479. topclassh:=nil;
  1480. end;
  1481. { link all procedures which have the same # of parameters }
  1482. for j:=0 to sym.ProcdefList.Count-1 do
  1483. begin
  1484. pd:=tprocdef(sym.ProcdefList[j]);
  1485. { Is the procdef visible? This needs to be checked on
  1486. procdef level since a symbol can contain both private and
  1487. public declarations. But the check should not be done
  1488. when the callnode is generated by a property
  1489. inherited overrides invisible anonymous inherited (FK) }
  1490. if isprop or ignorevis or
  1491. (pd.owner.symtabletype<>ObjectSymtable) or
  1492. pd.is_visible_for_object(topclassh,nil) then
  1493. begin
  1494. { we have at least one procedure that is visible }
  1495. inc(FProcvisiblecnt);
  1496. { only when the # of parameter are supported by the
  1497. procedure }
  1498. if (FParalength>=pd.minparacount) and
  1499. ((po_varargs in pd.procoptions) or { varargs }
  1500. (FParalength<=pd.maxparacount)) then
  1501. proc_add(pd);
  1502. end;
  1503. end;
  1504. { remember if the procedure is declared with the overload directive,
  1505. it's information is still needed also after all procs are removed }
  1506. has_overload_directive:=(po_overload in tprocdef(sym.ProcdefList[0]).procoptions);
  1507. { when the definition has overload directive set, we search for
  1508. overloaded definitions in the symtablestack. The found
  1509. entries are only added to the procs list and not the procsym, because
  1510. the list can change in every situation }
  1511. if has_overload_directive and
  1512. (sym.owner.symtabletype<>ObjectSymtable) then
  1513. begin
  1514. srsymtable:=sym.owner;
  1515. checkstack:=symtablestack.stack;
  1516. while assigned(checkstack) and
  1517. (checkstack^.symtable<>srsymtable) do
  1518. checkstack:=checkstack^.next;
  1519. { we've already processed the current symtable, start with
  1520. the next symtable in the stack }
  1521. if assigned(checkstack) then
  1522. checkstack:=checkstack^.next;
  1523. hashedid.id:=sym.name;
  1524. while assigned(checkstack) do
  1525. begin
  1526. srsymtable:=checkstack^.symtable;
  1527. if srsymtable.symtabletype in [localsymtable,staticsymtable,globalsymtable] then
  1528. begin
  1529. srprocsym:=tprocsym(srsymtable.FindWithHash(hashedid));
  1530. if assigned(srprocsym) and
  1531. (srprocsym.typ=procsym) then
  1532. begin
  1533. { if this visible procedure doesn't have overload we can stop
  1534. searching }
  1535. if not(po_overload in tprocdef(srprocsym.ProcdefList[0]).procoptions) and
  1536. tprocdef(srprocsym.ProcdefList[0]).is_visible_for_object(topclassh,nil) then
  1537. break;
  1538. { process all overloaded definitions }
  1539. for j:=0 to srprocsym.ProcdefList.Count-1 do
  1540. begin
  1541. pd:=tprocdef(srprocsym.ProcdefList[j]);
  1542. { only visible procedures need to be added }
  1543. if pd.is_visible_for_object(topclassh,nil) then
  1544. begin
  1545. { only when the # of parameter are supported by the
  1546. procedure }
  1547. if (FParalength>=pd.minparacount) and
  1548. ((po_varargs in pd.procoptions) or { varargs }
  1549. (FParalength<=pd.maxparacount)) then
  1550. begin
  1551. found:=false;
  1552. hp:=FProcs;
  1553. while assigned(hp) do
  1554. begin
  1555. { Only compare visible parameters for the user }
  1556. if compare_paras(hp^.data.paras,pd.paras,cp_value_equal_const,[cpo_ignorehidden])>=te_equal then
  1557. begin
  1558. found:=true;
  1559. break;
  1560. end;
  1561. hp:=hp^.next;
  1562. end;
  1563. if not found then
  1564. proc_add(pd);
  1565. end;
  1566. end;
  1567. end;
  1568. end;
  1569. end;
  1570. checkstack:=checkstack^.next;
  1571. end;
  1572. end;
  1573. end;
  1574. constructor tcallcandidates.create_operator(op:ttoken;ppn:tnode);
  1575. var
  1576. j : integer;
  1577. pd : tprocdef;
  1578. hp : pcandidate;
  1579. found : boolean;
  1580. srsymtable : TSymtable;
  1581. srprocsym : tprocsym;
  1582. pt : tcallparanode;
  1583. checkstack : psymtablestackitem;
  1584. hashedid : THashedIDString;
  1585. begin
  1586. FProcSym:=nil;
  1587. FProcs:=nil;
  1588. FProccnt:=0;
  1589. FProcvisiblecnt:=0;
  1590. FParanode:=ppn;
  1591. FAllowVariant:=false;
  1592. { determine length of parameter list }
  1593. pt:=tcallparanode(ppn);
  1594. FParalength:=0;
  1595. while assigned(pt) do
  1596. begin
  1597. if pt.resultdef.typ=variantdef then
  1598. FAllowVariant:=true;
  1599. inc(FParalength);
  1600. pt:=tcallparanode(pt.right);
  1601. end;
  1602. { we search all overloaded operator definitions in the symtablestack. The found
  1603. entries are only added to the procs list and not the procsym, because
  1604. the list can change in every situation }
  1605. hashedid.id:=overloaded_names[op];
  1606. checkstack:=symtablestack.stack;
  1607. while assigned(checkstack) do
  1608. begin
  1609. srsymtable:=checkstack^.symtable;
  1610. if srsymtable.symtabletype in [localsymtable,staticsymtable,globalsymtable] then
  1611. begin
  1612. srprocsym:=tprocsym(srsymtable.FindWithHash(hashedid));
  1613. if assigned(srprocsym) and
  1614. (srprocsym.typ=procsym) then
  1615. begin
  1616. { Store first procsym found }
  1617. if not assigned(FProcsym) then
  1618. FProcsym:=srprocsym;
  1619. { process all overloaded definitions }
  1620. for j:=0 to srprocsym.ProcdefList.Count-1 do
  1621. begin
  1622. pd:=tprocdef(srprocsym.ProcdefList[j]);
  1623. { only when the # of parameter are supported by the
  1624. procedure }
  1625. if (FParalength>=pd.minparacount) and
  1626. (FParalength<=pd.maxparacount) then
  1627. begin
  1628. found:=false;
  1629. hp:=FProcs;
  1630. while assigned(hp) do
  1631. begin
  1632. { Only compare visible parameters for the user }
  1633. if compare_paras(hp^.data.paras,pd.paras,cp_value_equal_const,[cpo_ignorehidden])>=te_equal then
  1634. begin
  1635. found:=true;
  1636. break;
  1637. end;
  1638. hp:=hp^.next;
  1639. end;
  1640. if not found then
  1641. proc_add(pd);
  1642. end;
  1643. end;
  1644. end;
  1645. end;
  1646. checkstack:=checkstack^.next;
  1647. end;
  1648. end;
  1649. destructor tcallcandidates.destroy;
  1650. var
  1651. hpnext,
  1652. hp : pcandidate;
  1653. begin
  1654. hp:=FProcs;
  1655. while assigned(hp) do
  1656. begin
  1657. hpnext:=hp^.next;
  1658. dispose(hp);
  1659. hp:=hpnext;
  1660. end;
  1661. end;
  1662. function tcallcandidates.proc_add(pd:tprocdef):pcandidate;
  1663. var
  1664. defaultparacnt : integer;
  1665. begin
  1666. { generate new candidate entry }
  1667. new(result);
  1668. fillchar(result^,sizeof(tcandidate),0);
  1669. result^.data:=pd;
  1670. result^.next:=FProcs;
  1671. FProcs:=result;
  1672. inc(FProccnt);
  1673. { Find last parameter, skip all default parameters
  1674. that are not passed. Ignore this skipping for varargs }
  1675. result^.firstparaidx:=pd.paras.count-1;
  1676. if not(po_varargs in pd.procoptions) then
  1677. begin
  1678. { ignore hidden parameters }
  1679. while (result^.firstparaidx>=0) and (vo_is_hidden_para in tparavarsym(pd.paras[result^.firstparaidx]).varoptions) do
  1680. dec(result^.firstparaidx);
  1681. defaultparacnt:=pd.maxparacount-FParalength;
  1682. if defaultparacnt>0 then
  1683. begin
  1684. if defaultparacnt>result^.firstparaidx+1 then
  1685. internalerror(200401141);
  1686. dec(result^.firstparaidx,defaultparacnt);
  1687. end;
  1688. end;
  1689. end;
  1690. procedure tcallcandidates.list(all:boolean);
  1691. var
  1692. hp : pcandidate;
  1693. begin
  1694. hp:=FProcs;
  1695. while assigned(hp) do
  1696. begin
  1697. if all or
  1698. (not hp^.invalid) then
  1699. MessagePos1(hp^.data.fileinfo,sym_h_param_list,hp^.data.fullprocname(false));
  1700. hp:=hp^.next;
  1701. end;
  1702. end;
  1703. {$ifdef EXTDEBUG}
  1704. procedure tcallcandidates.dump_info(lvl:longint);
  1705. function ParaTreeStr(p:tcallparanode):string;
  1706. begin
  1707. result:='';
  1708. while assigned(p) do
  1709. begin
  1710. if result<>'' then
  1711. result:=','+result;
  1712. result:=p.resultdef.typename+result;
  1713. p:=tcallparanode(p.right);
  1714. end;
  1715. end;
  1716. var
  1717. hp : pcandidate;
  1718. i : integer;
  1719. currpara : tparavarsym;
  1720. begin
  1721. if not CheckVerbosity(lvl) then
  1722. exit;
  1723. Comment(lvl+V_LineInfo,'Overloaded callnode: '+FProcSym.name+'('+ParaTreeStr(tcallparanode(FParaNode))+')');
  1724. hp:=FProcs;
  1725. while assigned(hp) do
  1726. begin
  1727. Comment(lvl,' '+hp^.data.fullprocname(false));
  1728. if (hp^.invalid) then
  1729. Comment(lvl,' invalid')
  1730. else
  1731. begin
  1732. Comment(lvl,' ex: '+tostr(hp^.exact_count)+
  1733. ' eq: '+tostr(hp^.equal_count)+
  1734. ' l1: '+tostr(hp^.cl1_count)+
  1735. ' l2: '+tostr(hp^.cl2_count)+
  1736. ' l3: '+tostr(hp^.cl3_count)+
  1737. ' oper: '+tostr(hp^.coper_count)+
  1738. ' ord: '+realtostr(hp^.ordinal_distance));
  1739. { Print parameters in left-right order }
  1740. for i:=0 to hp^.data.paras.count-1 do
  1741. begin
  1742. currpara:=tparavarsym(hp^.data.paras[i]);
  1743. if not(vo_is_hidden_para in currpara.varoptions) then
  1744. Comment(lvl,' - '+currpara.vardef.typename+' : '+EqualTypeName[currpara.eqval]);
  1745. end;
  1746. end;
  1747. hp:=hp^.next;
  1748. end;
  1749. end;
  1750. {$endif EXTDEBUG}
  1751. procedure tcallcandidates.get_information;
  1752. var
  1753. hp : pcandidate;
  1754. currpara : tparavarsym;
  1755. paraidx : integer;
  1756. currparanr : byte;
  1757. rfh,rth : bestreal;
  1758. objdef : tobjectdef;
  1759. def_from,
  1760. def_to : tdef;
  1761. currpt,
  1762. pt : tcallparanode;
  1763. eq : tequaltype;
  1764. convtype : tconverttype;
  1765. pdtemp,
  1766. pdoper : tprocdef;
  1767. releasecurrpt : boolean;
  1768. cdoptions : tcompare_defs_options;
  1769. begin
  1770. cdoptions:=[cdo_check_operator];
  1771. if FAllowVariant then
  1772. include(cdoptions,cdo_allow_variant);
  1773. { process all procs }
  1774. hp:=FProcs;
  1775. while assigned(hp) do
  1776. begin
  1777. { We compare parameters in reverse order (right to left),
  1778. the firstpara is already pointing to the last parameter
  1779. were we need to start comparing }
  1780. currparanr:=FParalength;
  1781. paraidx:=hp^.firstparaidx;
  1782. while (paraidx>=0) and (vo_is_hidden_para in tparavarsym(hp^.data.paras[paraidx]).varoptions) do
  1783. dec(paraidx);
  1784. pt:=tcallparanode(FParaNode);
  1785. while assigned(pt) and (paraidx>=0) do
  1786. begin
  1787. currpara:=tparavarsym(hp^.data.paras[paraidx]);
  1788. { currpt can be changed from loadn to calln when a procvar
  1789. is passed. This is to prevent that the change is permanent }
  1790. currpt:=pt;
  1791. releasecurrpt:=false;
  1792. { retrieve current parameter definitions to compares }
  1793. eq:=te_incompatible;
  1794. def_from:=currpt.resultdef;
  1795. def_to:=currpara.vardef;
  1796. if not(assigned(def_from)) then
  1797. internalerror(200212091);
  1798. if not(
  1799. assigned(def_to) or
  1800. ((po_varargs in hp^.data.procoptions) and
  1801. (currparanr>hp^.data.minparacount))
  1802. ) then
  1803. internalerror(200212092);
  1804. { Convert tp procvars when not expecting a procvar }
  1805. if (def_to.typ<>procvardef) and
  1806. (currpt.left.resultdef.typ=procvardef) and
  1807. { Only convert to call when there is no overload or the return type
  1808. is equal to the expected type. }
  1809. (
  1810. (count=1) or
  1811. equal_defs(tprocvardef(currpt.left.resultdef).returndef,def_to)
  1812. ) then
  1813. begin
  1814. releasecurrpt:=true;
  1815. currpt:=tcallparanode(pt.getcopy);
  1816. if maybe_call_procvar(currpt.left,true) then
  1817. begin
  1818. currpt.resultdef:=currpt.left.resultdef;
  1819. def_from:=currpt.left.resultdef;
  1820. end;
  1821. end;
  1822. { If we expect a procvar and the left is loadnode that
  1823. returns a procdef we need to find the correct overloaded
  1824. procdef that matches the expected procvar. The loadnode
  1825. temporary returned the first procdef (PFV) }
  1826. if (def_to.typ=procvardef) and
  1827. (currpt.left.nodetype=loadn) and
  1828. (currpt.left.resultdef.typ=procdef) then
  1829. begin
  1830. pdtemp:=tprocsym(Tloadnode(currpt.left).symtableentry).Find_procdef_byprocvardef(Tprocvardef(def_to));
  1831. if assigned(pdtemp) then
  1832. begin
  1833. tloadnode(currpt.left).setprocdef(pdtemp);
  1834. currpt.resultdef:=currpt.left.resultdef;
  1835. def_from:=currpt.left.resultdef;
  1836. end;
  1837. end;
  1838. { varargs are always equal, but not exact }
  1839. if (po_varargs in hp^.data.procoptions) and
  1840. (currparanr>hp^.data.minparacount) and
  1841. not is_array_of_const(def_from) and
  1842. not is_array_constructor(def_from) then
  1843. begin
  1844. eq:=te_equal;
  1845. end
  1846. else
  1847. { same definition -> exact }
  1848. if (def_from=def_to) then
  1849. begin
  1850. eq:=te_exact;
  1851. end
  1852. else
  1853. { for value and const parameters check if a integer is constant or
  1854. included in other integer -> equal and calc ordinal_distance }
  1855. if not(currpara.varspez in [vs_var,vs_out]) and
  1856. is_integer(def_from) and
  1857. is_integer(def_to) and
  1858. is_in_limit(def_from,def_to) then
  1859. begin
  1860. eq:=te_equal;
  1861. hp^.ordinal_distance:=hp^.ordinal_distance+
  1862. abs(bestreal(torddef(def_from).low)-bestreal(torddef(def_to).low));
  1863. if (torddef(def_to).ordtype=u64bit) then
  1864. rth:=bestreal(qword(torddef(def_to).high))
  1865. else
  1866. rth:=bestreal(torddef(def_to).high);
  1867. if (torddef(def_from).ordtype=u64bit) then
  1868. rfh:=bestreal(qword(torddef(def_from).high))
  1869. else
  1870. rfh:=bestreal(torddef(def_from).high);
  1871. hp^.ordinal_distance:=hp^.ordinal_distance+abs(rth-rfh);
  1872. { Give wrong sign a small penalty, this is need to get a diffrence
  1873. from word->[longword,longint] }
  1874. if is_signed(def_from)<>is_signed(def_to) then
  1875. hp^.ordinal_distance:=hp^.ordinal_distance+1.0;
  1876. end
  1877. else
  1878. { for value and const parameters check precision of real, give
  1879. penalty for loosing of precision. var and out parameters must match exactly }
  1880. if not(currpara.varspez in [vs_var,vs_out]) and
  1881. is_real(def_from) and
  1882. is_real(def_to) then
  1883. begin
  1884. eq:=te_equal;
  1885. if is_extended(def_to) then
  1886. rth:=bestreal(4)
  1887. else
  1888. if is_double (def_to) then
  1889. rth:=bestreal(2)
  1890. else
  1891. rth:=bestreal(1);
  1892. if is_extended(def_from) then
  1893. rfh:=bestreal(4)
  1894. else
  1895. if is_double (def_from) then
  1896. rfh:=bestreal(2)
  1897. else
  1898. rfh:=bestreal(1);
  1899. { penalty for shrinking of precision }
  1900. if rth<rfh then
  1901. rfh:=(rfh-rth)*16
  1902. else
  1903. rfh:=rth-rfh;
  1904. hp^.ordinal_distance:=hp^.ordinal_distance+rfh;
  1905. end
  1906. else
  1907. { related object parameters also need to determine the distance between the current
  1908. object and the object we are comparing with. var and out parameters must match exactly }
  1909. if not(currpara.varspez in [vs_var,vs_out]) and
  1910. (def_from.typ=objectdef) and
  1911. (def_to.typ=objectdef) and
  1912. (tobjectdef(def_from).objecttype=tobjectdef(def_to).objecttype) and
  1913. tobjectdef(def_from).is_related(tobjectdef(def_to)) then
  1914. begin
  1915. eq:=te_convert_l1;
  1916. objdef:=tobjectdef(def_from);
  1917. while assigned(objdef) do
  1918. begin
  1919. if objdef=def_to then
  1920. break;
  1921. hp^.ordinal_distance:=hp^.ordinal_distance+1;
  1922. objdef:=objdef.childof;
  1923. end;
  1924. end
  1925. else
  1926. { generic type comparision }
  1927. begin
  1928. eq:=compare_defs_ext(def_from,def_to,currpt.left.nodetype,convtype,pdoper,cdoptions);
  1929. { when the types are not equal we need to check
  1930. some special case for parameter passing }
  1931. if (eq<te_equal) then
  1932. begin
  1933. if currpara.varspez in [vs_var,vs_out] then
  1934. begin
  1935. { para requires an equal type so the previous found
  1936. match was not good enough, reset to incompatible }
  1937. eq:=te_incompatible;
  1938. { var_para_allowed will return te_equal and te_convert_l1 to
  1939. make a difference for best matching }
  1940. var_para_allowed(eq,currpt.resultdef,currpara.vardef)
  1941. end
  1942. else
  1943. para_allowed(eq,currpt,def_to);
  1944. end;
  1945. end;
  1946. { when a procvar was changed to a call an exact much is
  1947. downgraded to equal. This way an overload call with the
  1948. procvar is choosen. See tb0471 (PFV) }
  1949. if (pt<>currpt) and (eq=te_exact) then
  1950. eq:=te_equal;
  1951. { increase correct counter }
  1952. case eq of
  1953. te_exact :
  1954. inc(hp^.exact_count);
  1955. te_equal :
  1956. inc(hp^.equal_count);
  1957. te_convert_l1 :
  1958. inc(hp^.cl1_count);
  1959. te_convert_l2 :
  1960. inc(hp^.cl2_count);
  1961. te_convert_l3 :
  1962. inc(hp^.cl3_count);
  1963. te_convert_operator :
  1964. inc(hp^.coper_count);
  1965. te_incompatible :
  1966. hp^.invalid:=true;
  1967. else
  1968. internalerror(200212072);
  1969. end;
  1970. { stop checking when an incompatible parameter is found }
  1971. if hp^.invalid then
  1972. begin
  1973. { store the current parameter info for
  1974. a nice error message when no procedure is found }
  1975. hp^.wrongparaidx:=paraidx;
  1976. hp^.wrongparanr:=currparanr;
  1977. break;
  1978. end;
  1979. {$ifdef EXTDEBUG}
  1980. { store equal in node tree for dump }
  1981. currpara.eqval:=eq;
  1982. {$endif EXTDEBUG}
  1983. { maybe release temp currpt }
  1984. if releasecurrpt then
  1985. currpt.free;
  1986. { next parameter in the call tree }
  1987. pt:=tcallparanode(pt.right);
  1988. { next parameter for definition, only goto next para
  1989. if we're out of the varargs }
  1990. if not(po_varargs in hp^.data.procoptions) or
  1991. (currparanr<=hp^.data.maxparacount) then
  1992. begin
  1993. { Ignore vs_hidden parameters }
  1994. repeat
  1995. dec(paraidx);
  1996. until (paraidx<0) or not(vo_is_hidden_para in tparavarsym(hp^.data.paras[paraidx]).varoptions);
  1997. end;
  1998. dec(currparanr);
  1999. end;
  2000. if not(hp^.invalid) and
  2001. (assigned(pt) or (paraidx>=0) or (currparanr<>0)) then
  2002. internalerror(200212141);
  2003. { next candidate }
  2004. hp:=hp^.next;
  2005. end;
  2006. end;
  2007. function get_variantequaltype(def: tdef): tvariantequaltype;
  2008. const
  2009. variantorddef_cl: array[tordtype] of tvariantequaltype =
  2010. (tve_incompatible,tve_byte,tve_word,tve_cardinal,tve_chari64,
  2011. tve_shortint,tve_smallint,tve_longint,tve_chari64,
  2012. tve_boolformal,tve_boolformal,tve_boolformal,tve_boolformal,
  2013. tve_chari64,tve_chari64,tve_dblcurrency);
  2014. {$warning fixme for 128 bit floats }
  2015. variantfloatdef_cl: array[tfloattype] of tvariantequaltype =
  2016. (tve_single,tve_dblcurrency,tve_extended,
  2017. tve_dblcurrency,tve_dblcurrency,tve_extended);
  2018. variantstringdef_cl: array[tstringtype] of tvariantequaltype =
  2019. (tve_sstring,tve_astring,tve_astring,tve_wstring);
  2020. begin
  2021. result:=tve_incompatible;
  2022. case def.typ of
  2023. orddef:
  2024. begin
  2025. result:=variantorddef_cl[torddef(def).ordtype];
  2026. end;
  2027. floatdef:
  2028. begin
  2029. result:=variantfloatdef_cl[tfloatdef(def).floattype];
  2030. end;
  2031. stringdef:
  2032. begin
  2033. result:=variantstringdef_cl[tstringdef(def).stringtype];
  2034. end;
  2035. formaldef:
  2036. begin
  2037. result:=tve_boolformal;
  2038. end;
  2039. else
  2040. internalerror(2006122804);
  2041. end
  2042. end;
  2043. function is_better_candidate(currpd,bestpd:pcandidate):integer;
  2044. var
  2045. res : integer;
  2046. begin
  2047. {
  2048. Return values:
  2049. > 0 when currpd is better than bestpd
  2050. < 0 when bestpd is better than currpd
  2051. = 0 when both are equal
  2052. To choose the best candidate we use the following order:
  2053. - Incompatible flag
  2054. - (Smaller) Number of convert operator parameters.
  2055. - (Smaller) Number of convertlevel 2 parameters.
  2056. - (Smaller) Number of convertlevel 1 parameters.
  2057. - (Bigger) Number of exact parameters.
  2058. - (Smaller) Number of equal parameters.
  2059. - (Smaller) Total of ordinal distance. For example, the distance of a word
  2060. to a byte is 65535-255=65280.
  2061. }
  2062. if bestpd^.invalid then
  2063. begin
  2064. if currpd^.invalid then
  2065. res:=0
  2066. else
  2067. res:=1;
  2068. end
  2069. else
  2070. if currpd^.invalid then
  2071. res:=-1
  2072. else
  2073. begin
  2074. { less operator parameters? }
  2075. res:=(bestpd^.coper_count-currpd^.coper_count);
  2076. if (res=0) then
  2077. begin
  2078. { less cl3 parameters? }
  2079. res:=(bestpd^.cl3_count-currpd^.cl3_count);
  2080. if (res=0) then
  2081. begin
  2082. { less cl2 parameters? }
  2083. res:=(bestpd^.cl2_count-currpd^.cl2_count);
  2084. if (res=0) then
  2085. begin
  2086. { less cl1 parameters? }
  2087. res:=(bestpd^.cl1_count-currpd^.cl1_count);
  2088. if (res=0) then
  2089. begin
  2090. { more exact parameters? }
  2091. res:=(currpd^.exact_count-bestpd^.exact_count);
  2092. if (res=0) then
  2093. begin
  2094. { less equal parameters? }
  2095. res:=(bestpd^.equal_count-currpd^.equal_count);
  2096. if (res=0) then
  2097. begin
  2098. { smaller ordinal distance? }
  2099. if (currpd^.ordinal_distance<bestpd^.ordinal_distance) then
  2100. res:=1
  2101. else
  2102. if (currpd^.ordinal_distance>bestpd^.ordinal_distance) then
  2103. res:=-1
  2104. else
  2105. res:=0;
  2106. end;
  2107. end;
  2108. end;
  2109. end;
  2110. end;
  2111. end;
  2112. end;
  2113. is_better_candidate:=res;
  2114. end;
  2115. { Delphi precedence rules extracted from test programs. Only valid if passing
  2116. a variant parameter to overloaded procedures expecting exactly one parameter.
  2117. single > (char, currency, int64, shortstring, ansistring, widestring, extended, double)
  2118. double/currency > (char, int64, shortstring, ansistring, widestring, extended)
  2119. extended > (char, int64, shortstring, ansistring, widestring)
  2120. longint/cardinal > (int64, shortstring, ansistring, widestring, extended, double, single, char, currency)
  2121. smallint > (longint, int64, shortstring, ansistring, widestring, extended, double single, char, currency);
  2122. word > (longint, cardinal, int64, shortstring, ansistring, widestring, extended, double single, char, currency);
  2123. shortint > (longint, smallint, int64, shortstring, ansistring, widestring, extended, double, single, char, currency)
  2124. byte > (longint, cardinal, word, smallint, int64, shortstring, ansistring, widestring, extended, double, single, char, currency);
  2125. boolean/formal > (char, int64, shortstring, ansistring, widestring)
  2126. shortstring > (char, int64, ansistring, widestring)
  2127. ansistring > (char, int64, widestring)
  2128. widestring > (char, int64)
  2129. Relations not mentioned mean that they conflict: no decision possible }
  2130. function is_better_candidate_single_variant(currpd,bestpd:pcandidate):integer;
  2131. function calculate_relation(const currvcl, bestvcl, testvcl:
  2132. tvariantequaltype; const conflictvcls: tvariantequaltypes):integer;
  2133. begin
  2134. { if (bestvcl=conflictvcl) or
  2135. (currvcl=conflictvcl) then
  2136. result:=0
  2137. else if (bestvcl=testvcl) then
  2138. result:=-1
  2139. else result:=1 }
  2140. result:=1-2*ord(bestvcl=testvcl)+
  2141. ord(currvcl in conflictvcls)-ord(bestvcl in conflictvcls);
  2142. end;
  2143. var
  2144. paraidx,
  2145. res: integer;
  2146. currpara, bestpara: tparavarsym;
  2147. currvcl, bestvcl: tvariantequaltype;
  2148. begin
  2149. {
  2150. Return values:
  2151. > 0 when currpd is better than bestpd
  2152. < 0 when bestpd is better than currpd
  2153. = 0 when both are equal
  2154. }
  2155. if (currpd^.firstparaidx<>bestpd^.firstparaidx) then
  2156. internalerror(2006122801);
  2157. paraidx:=currpd^.firstparaidx;
  2158. while (paraidx>=0) and (vo_is_hidden_para in tparavarsym(currpd^.data.paras[paraidx]).varoptions) do
  2159. if (vo_is_hidden_para in tparavarsym(bestpd^.data.paras[paraidx]).varoptions) then
  2160. dec(paraidx)
  2161. else
  2162. internalerror(2006122802);
  2163. if (vo_is_hidden_para in tparavarsym(currpd^.data.paras[paraidx]).varoptions) then
  2164. internalerror(2006122803);
  2165. currpara:=tparavarsym(currpd^.data.paras[paraidx]);
  2166. bestpara:=tparavarsym(bestpd^.data.paras[paraidx]);
  2167. { if one of the parameters is a regular variant, fall back to the }
  2168. { default algorithm }
  2169. if (currpara.vardef.typ = variantdef) or
  2170. (bestpara.vardef.typ = variantdef) then
  2171. begin
  2172. result:=is_better_candidate(currpd,bestpd);
  2173. exit;
  2174. end;
  2175. currvcl:=get_variantequaltype(currpara.vardef);
  2176. bestvcl:=get_variantequaltype(bestpara.vardef);
  2177. { sanity check }
  2178. result:=-5;
  2179. { if both are the same, there is a conflict }
  2180. if (currvcl=bestvcl) then
  2181. result:=0
  2182. { boolean and formal are better than chari64str, but conflict with }
  2183. { everything else }
  2184. else if (currvcl=tve_boolformal) or
  2185. (bestvcl=tve_boolformal) then
  2186. if (currvcl=tve_boolformal) then
  2187. result:=ord(bestvcl in [tve_chari64,tve_sstring,tve_astring,tve_wstring])
  2188. else
  2189. result:=-ord(currvcl in [tve_chari64,tve_sstring,tve_astring,tve_wstring])
  2190. { byte is better than everything else (we assume both aren't byte, }
  2191. { since there's only one parameter and that one can't be the same) }
  2192. else if (currvcl=tve_byte) or
  2193. (bestvcl=tve_byte) then
  2194. result:=calculate_relation(currvcl,bestvcl,tve_byte,[tve_shortint])
  2195. { shortint conflicts with word and cardinal, but is better than }
  2196. { everything else but byte (which has already been handled) }
  2197. else if (currvcl=tve_shortint) or
  2198. (bestvcl=tve_shortint) then
  2199. result:=calculate_relation(currvcl,bestvcl,tve_shortint,[tve_word, tve_cardinal])
  2200. { word conflicts with smallint, but is better than everything else }
  2201. { but shortint and byte (which has already been handled) }
  2202. else if (currvcl=tve_word) or
  2203. (bestvcl=tve_word) then
  2204. result:=calculate_relation(currvcl,bestvcl,tve_word,[tve_smallint])
  2205. { smallint conflicts with cardinal, but is better than everything }
  2206. { which has not yet been tested }
  2207. else if (currvcl=tve_smallint) or
  2208. (bestvcl=tve_smallint) then
  2209. result:=calculate_relation(currvcl,bestvcl,tve_smallint,[tve_cardinal])
  2210. { cardinal conflicts with each longint and is better than everything }
  2211. { which has not yet been tested }
  2212. else if (currvcl = tve_cardinal) or
  2213. (bestvcl=tve_cardinal) then
  2214. result:=calculate_relation(currvcl,bestvcl,tve_cardinal,[tve_longint])
  2215. { longint is better than everything which has not yet been tested }
  2216. else if (currvcl=tve_longint) or
  2217. (bestvcl=tve_longint) then
  2218. { if bestvcl=tve_longint then
  2219. result:=-1
  2220. else
  2221. result:=1 }
  2222. result:=1-2*ord(bestvcl=tve_longint)
  2223. { single is better than everything left }
  2224. else if (currvcl=tve_single) or
  2225. (bestvcl=tve_single) then
  2226. result:=1-2*ord(bestvcl=tve_single)
  2227. { double/comp/currency are better than everything left, and conflict }
  2228. { with each other (but that's already tested) }
  2229. else if (currvcl=tve_dblcurrency) or
  2230. (bestvcl=tve_dblcurrency) then
  2231. result:=1-2*ord(bestvcl=tve_dblcurrency)
  2232. { extended is better than everything left }
  2233. else if (currvcl=tve_extended) or
  2234. (bestvcl=tve_extended) then
  2235. result:=1-2*ord(bestvcl=tve_extended)
  2236. { shortstring is better than everything left }
  2237. else if (currvcl=tve_sstring) or
  2238. (bestvcl=tve_sstring) then
  2239. result:=1-2*ord(bestvcl=tve_sstring)
  2240. { ansistring is better than everything left }
  2241. else if (currvcl=tve_astring) or
  2242. (bestvcl=tve_astring) then
  2243. result:=1-2*ord(bestvcl=tve_astring)
  2244. { widestring is better than everything left }
  2245. else if (currvcl=tve_wstring) or
  2246. (bestvcl=tve_wstring) then
  2247. result:=1-2*ord(bestvcl=tve_wstring);
  2248. { all possibilities should have been checked now }
  2249. if (result=-5) then
  2250. internalerror(2006122805);
  2251. end;
  2252. function tcallcandidates.choose_best(var bestpd:tabstractprocdef; singlevariant: boolean):integer;
  2253. var
  2254. besthpstart,
  2255. hp : pcandidate;
  2256. cntpd,
  2257. res : integer;
  2258. begin
  2259. {
  2260. Returns the number of candidates left and the
  2261. first candidate is returned in pdbest
  2262. }
  2263. { Setup the first procdef as best, only count it as a result
  2264. when it is valid }
  2265. bestpd:=FProcs^.data;
  2266. if FProcs^.invalid then
  2267. cntpd:=0
  2268. else
  2269. cntpd:=1;
  2270. if assigned(FProcs^.next) then
  2271. begin
  2272. besthpstart:=FProcs;
  2273. hp:=FProcs^.next;
  2274. while assigned(hp) do
  2275. begin
  2276. if not singlevariant then
  2277. res:=is_better_candidate(hp,besthpstart)
  2278. else
  2279. res:=is_better_candidate_single_variant(hp,besthpstart);
  2280. if (res>0) then
  2281. begin
  2282. { hp is better, flag all procs to be incompatible }
  2283. while (besthpstart<>hp) do
  2284. begin
  2285. besthpstart^.invalid:=true;
  2286. besthpstart:=besthpstart^.next;
  2287. end;
  2288. { besthpstart is already set to hp }
  2289. bestpd:=besthpstart^.data;
  2290. cntpd:=1;
  2291. end
  2292. else
  2293. if (res<0) then
  2294. begin
  2295. { besthpstart is better, flag current hp to be incompatible }
  2296. hp^.invalid:=true;
  2297. end
  2298. else
  2299. begin
  2300. { res=0, both are valid }
  2301. if not hp^.invalid then
  2302. inc(cntpd);
  2303. end;
  2304. hp:=hp^.next;
  2305. end;
  2306. end;
  2307. result:=cntpd;
  2308. end;
  2309. procedure tcallcandidates.find_wrong_para;
  2310. var
  2311. currparanr : smallint;
  2312. hp : pcandidate;
  2313. pt : tcallparanode;
  2314. wrongpara : tparavarsym;
  2315. begin
  2316. { Only process the first overloaded procdef }
  2317. hp:=FProcs;
  2318. { Find callparanode corresponding to the argument }
  2319. pt:=tcallparanode(FParanode);
  2320. currparanr:=FParalength;
  2321. while assigned(pt) and
  2322. (currparanr>hp^.wrongparanr) do
  2323. begin
  2324. pt:=tcallparanode(pt.right);
  2325. dec(currparanr);
  2326. end;
  2327. if (currparanr<>hp^.wrongparanr) or
  2328. not assigned(pt) then
  2329. internalerror(200212094);
  2330. { Show error message, when it was a var or out parameter
  2331. guess that it is a missing typeconv }
  2332. wrongpara:=tparavarsym(hp^.data.paras[hp^.wrongparaidx]);
  2333. if wrongpara.varspez in [vs_var,vs_out] then
  2334. begin
  2335. { Maybe passing the correct type but passing a const to var parameter }
  2336. if (compare_defs(pt.resultdef,wrongpara.vardef,pt.nodetype)<>te_incompatible) and
  2337. not valid_for_var(pt.left,true) then
  2338. CGMessagePos(pt.left.fileinfo,type_e_variable_id_expected)
  2339. else
  2340. CGMessagePos3(pt.left.fileinfo,parser_e_call_by_ref_without_typeconv,tostr(hp^.wrongparanr),
  2341. FullTypeName(pt.left.resultdef,wrongpara.vardef),
  2342. FullTypeName(wrongpara.vardef,pt.left.resultdef))
  2343. end
  2344. else
  2345. CGMessagePos3(pt.left.fileinfo,type_e_wrong_parameter_type,tostr(hp^.wrongparanr),
  2346. FullTypeName(pt.left.resultdef,wrongpara.vardef),
  2347. FullTypeName(wrongpara.vardef,pt.left.resultdef));
  2348. end;
  2349. procedure check_hints(const srsym: tsym; const symoptions: tsymoptions);
  2350. begin
  2351. if not assigned(srsym) then
  2352. internalerror(200602051);
  2353. if sp_hint_deprecated in symoptions then
  2354. Message1(sym_w_deprecated_symbol,srsym.realname);
  2355. if sp_hint_platform in symoptions then
  2356. Message1(sym_w_non_portable_symbol,srsym.realname);
  2357. if sp_hint_unimplemented in symoptions then
  2358. Message1(sym_w_non_implemented_symbol,srsym.realname);
  2359. end;
  2360. procedure check_ranges(const location: tfileposinfo; source: tnode; destdef: tdef);
  2361. begin
  2362. { check if the assignment may cause a range check error }
  2363. { if its not explicit, and only if the values are }
  2364. { ordinals, enumdef and floatdef }
  2365. if assigned(destdef) and
  2366. (destdef.typ in [enumdef,orddef,floatdef]) and
  2367. not is_boolean(destdef) and
  2368. assigned(source.resultdef) and
  2369. (source.resultdef.typ in [enumdef,orddef,floatdef]) and
  2370. not is_boolean(source.resultdef) and
  2371. not is_constrealnode(source) then
  2372. begin
  2373. if (destdef.size < source.resultdef.size) then
  2374. begin
  2375. if (cs_check_range in current_settings.localswitches) then
  2376. MessagePos(location,type_w_smaller_possible_range_check)
  2377. else
  2378. MessagePos(location,type_h_smaller_possible_range_check);
  2379. end;
  2380. end;
  2381. end;
  2382. end.