htypechk.pas 75 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Florian Klaempfl
  4. This unit exports some help routines for the type checking
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit htypechk;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. tokens,cpuinfo,
  23. node,
  24. symconst,symtype,symdef,symsym,symbase;
  25. type
  26. Ttok2nodeRec=record
  27. tok : ttoken;
  28. nod : tnodetype;
  29. op_overloading_supported : boolean;
  30. end;
  31. pcandidate = ^tcandidate;
  32. tcandidate = record
  33. next : pcandidate;
  34. data : tprocdef;
  35. wrongparaidx,
  36. firstparaidx : integer;
  37. exact_count,
  38. equal_count,
  39. cl1_count,
  40. cl2_count,
  41. cl3_count,
  42. coper_count : integer; { should be signed }
  43. ordinal_distance : bestreal;
  44. invalid : boolean;
  45. wrongparanr : byte;
  46. end;
  47. tcallcandidates = class
  48. private
  49. FProcSym : tprocsym;
  50. FProcs : pcandidate;
  51. FProcVisibleCnt,
  52. FProcCnt : integer;
  53. FParaNode : tnode;
  54. FParaLength : smallint;
  55. FAllowVariant : boolean;
  56. function proc_add(pd:tprocdef):pcandidate;
  57. public
  58. constructor create(sym:tprocsym;st:tsymtable;ppn:tnode;isprop,ignorevis : boolean);
  59. constructor create_operator(op:ttoken;ppn:tnode);
  60. destructor destroy;override;
  61. procedure list(all:boolean);
  62. {$ifdef EXTDEBUG}
  63. procedure dump_info(lvl:longint);
  64. {$endif EXTDEBUG}
  65. procedure get_information;
  66. function choose_best(var bestpd:tabstractprocdef):integer;
  67. procedure find_wrong_para;
  68. property Count:integer read FProcCnt;
  69. property VisibleCount:integer read FProcVisibleCnt;
  70. end;
  71. const
  72. tok2nodes=25;
  73. tok2node:array[1..tok2nodes] of ttok2noderec=(
  74. (tok:_PLUS ;nod:addn;op_overloading_supported:true), { binary overloading supported }
  75. (tok:_MINUS ;nod:subn;op_overloading_supported:true), { binary and unary overloading supported }
  76. (tok:_STAR ;nod:muln;op_overloading_supported:true), { binary overloading supported }
  77. (tok:_SLASH ;nod:slashn;op_overloading_supported:true), { binary overloading supported }
  78. (tok:_EQUAL ;nod:equaln;op_overloading_supported:true), { binary overloading supported }
  79. (tok:_GT ;nod:gtn;op_overloading_supported:true), { binary overloading supported }
  80. (tok:_LT ;nod:ltn;op_overloading_supported:true), { binary overloading supported }
  81. (tok:_GTE ;nod:gten;op_overloading_supported:true), { binary overloading supported }
  82. (tok:_LTE ;nod:lten;op_overloading_supported:true), { binary overloading supported }
  83. (tok:_SYMDIF ;nod:symdifn;op_overloading_supported:true), { binary overloading supported }
  84. (tok:_STARSTAR;nod:starstarn;op_overloading_supported:true), { binary overloading supported }
  85. (tok:_OP_AS ;nod:asn;op_overloading_supported:false), { binary overloading NOT supported }
  86. (tok:_OP_IN ;nod:inn;op_overloading_supported:false), { binary overloading NOT supported }
  87. (tok:_OP_IS ;nod:isn;op_overloading_supported:false), { binary overloading NOT supported }
  88. (tok:_OP_OR ;nod:orn;op_overloading_supported:true), { binary overloading supported }
  89. (tok:_OP_AND ;nod:andn;op_overloading_supported:true), { binary overloading supported }
  90. (tok:_OP_DIV ;nod:divn;op_overloading_supported:true), { binary overloading supported }
  91. (tok:_OP_NOT ;nod:notn;op_overloading_supported:true), { unary overloading supported }
  92. (tok:_OP_MOD ;nod:modn;op_overloading_supported:true), { binary overloading supported }
  93. (tok:_OP_SHL ;nod:shln;op_overloading_supported:true), { binary overloading supported }
  94. (tok:_OP_SHR ;nod:shrn;op_overloading_supported:true), { binary overloading supported }
  95. (tok:_OP_XOR ;nod:xorn;op_overloading_supported:true), { binary overloading supported }
  96. (tok:_ASSIGNMENT;nod:assignn;op_overloading_supported:true), { unary overloading supported }
  97. (tok:_CARET ;nod:caretn;op_overloading_supported:false), { binary overloading NOT supported }
  98. (tok:_UNEQUAL ;nod:unequaln;op_overloading_supported:false) { binary overloading NOT supported overload = instead }
  99. );
  100. const
  101. { firstcallparan without varspez we don't count the ref }
  102. {$ifdef extdebug}
  103. count_ref : boolean = true;
  104. {$endif def extdebug}
  105. allow_array_constructor : boolean = false;
  106. function node2opstr(nt:tnodetype):string;
  107. { check operator args and result type }
  108. function isbinaryoperatoroverloadable(treetyp:tnodetype;ld:tdef;lt:tnodetype;rd:tdef;rt:tnodetype) : boolean;
  109. function isoperatoracceptable(pf : tprocdef; optoken : ttoken) : boolean;
  110. function isunaryoverloaded(var t : tnode) : boolean;
  111. function isbinaryoverloaded(var t : tnode) : boolean;
  112. { Register Allocation }
  113. procedure make_not_regable(p : tnode);
  114. procedure calcregisters(p : tbinarynode;r32,fpu,mmx : word);
  115. { procvar handling }
  116. function is_procvar_load(p:tnode):boolean;
  117. procedure test_local_to_procvar(from_def:tprocvardef;to_def:tdef);
  118. { sets varsym varstate field correctly }
  119. type
  120. tvarstateflag = (vsf_must_be_valid,vsf_use_hints);
  121. tvarstateflags = set of tvarstateflag;
  122. procedure set_varstate(p:tnode;newstate:tvarstate;varstateflags:tvarstateflags);
  123. { sets the callunique flag, if the node is a vecn, }
  124. { takes care of type casts etc. }
  125. procedure set_unique(p : tnode);
  126. function valid_for_formal_var(p : tnode) : boolean;
  127. function valid_for_formal_const(p : tnode) : boolean;
  128. function valid_for_var(p:tnode):boolean;
  129. function valid_for_assignment(p:tnode):boolean;
  130. function valid_for_addr(p : tnode) : boolean;
  131. implementation
  132. uses
  133. globtype,systems,
  134. cutils,verbose,globals,
  135. symtable,
  136. defutil,defcmp,
  137. nbas,ncnv,nld,nmem,ncal,nmat,ninl,nutils,
  138. cgbase,procinfo
  139. ;
  140. type
  141. TValidAssign=(Valid_Property,Valid_Void,Valid_Const,Valid_Addr);
  142. TValidAssigns=set of TValidAssign;
  143. function node2opstr(nt:tnodetype):string;
  144. var
  145. i : integer;
  146. begin
  147. result:='<unknown>';
  148. for i:=1 to tok2nodes do
  149. if tok2node[i].nod=nt then
  150. begin
  151. result:=tokeninfo^[tok2node[i].tok].str;
  152. break;
  153. end;
  154. end;
  155. function isbinaryoperatoroverloadable(treetyp:tnodetype;ld:tdef;lt:tnodetype;rd:tdef;rt:tnodetype) : boolean;
  156. function internal_check(treetyp:tnodetype;ld:tdef;lt:tnodetype;rd:tdef;rt:tnodetype;var allowed:boolean):boolean;
  157. begin
  158. internal_check:=true;
  159. case ld.deftype of
  160. formaldef,
  161. recorddef,
  162. variantdef :
  163. begin
  164. allowed:=true;
  165. end;
  166. procvardef :
  167. begin
  168. if (rd.deftype in [pointerdef,procdef,procvardef]) then
  169. begin
  170. allowed:=false;
  171. exit;
  172. end;
  173. allowed:=true;
  174. end;
  175. pointerdef :
  176. begin
  177. if ((rd.deftype in [orddef,enumdef,pointerdef,classrefdef,procvardef]) or
  178. is_class_or_interface(rd)) then
  179. begin
  180. allowed:=false;
  181. exit;
  182. end;
  183. { don't allow pchar+string }
  184. if (is_pchar(ld) or is_pwidechar(ld)) and
  185. ((rd.deftype=stringdef) or
  186. is_pchar(rd) or
  187. is_pwidechar(rd) or
  188. is_chararray(rd) or
  189. is_widechararray(rd)) then
  190. begin
  191. allowed:=false;
  192. exit;
  193. end;
  194. allowed:=true;
  195. end;
  196. arraydef :
  197. begin
  198. { not mmx }
  199. if (cs_mmx in aktlocalswitches) and
  200. is_mmx_able_array(ld) then
  201. begin
  202. allowed:=false;
  203. exit;
  204. end;
  205. { not chararray+[(wide)char,(wide)string,(wide)chararray] }
  206. if (is_chararray(ld) or is_widechararray(ld) or
  207. is_open_chararray(ld) or is_open_widechararray(ld))
  208. and
  209. ((rd.deftype in [stringdef,orddef,enumdef]) or
  210. is_pchar(rd) or
  211. is_pwidechar(rd) or
  212. is_chararray(rd) or
  213. is_widechararray(rd) or
  214. is_open_chararray(rd) or
  215. is_open_widechararray(rd) or
  216. (rt=niln)) then
  217. begin
  218. allowed:=false;
  219. exit;
  220. end;
  221. { dynamic array compare with niln }
  222. if ((is_dynamic_array(ld) and
  223. (rt=niln)) or
  224. (is_dynamic_array(ld) and is_dynamic_array(rd)))
  225. and
  226. (treetyp in [equaln,unequaln]) then
  227. begin
  228. allowed:=false;
  229. exit;
  230. end;
  231. allowed:=true;
  232. end;
  233. objectdef :
  234. begin
  235. { <> and = are defined for classes }
  236. if (treetyp in [equaln,unequaln]) and
  237. is_class_or_interface(ld) then
  238. begin
  239. allowed:=false;
  240. exit;
  241. end;
  242. allowed:=true;
  243. end;
  244. stringdef :
  245. begin
  246. if (rd.deftype in [orddef,enumdef,stringdef]) or
  247. is_pchar(rd) or
  248. is_pwidechar(rd) or
  249. is_chararray(rd) or
  250. is_widechararray(rd) or
  251. is_open_chararray(rd) or
  252. is_open_widechararray(rd) then
  253. begin
  254. allowed:=false;
  255. exit;
  256. end;
  257. allowed:=true;
  258. end;
  259. else
  260. internal_check:=false;
  261. end;
  262. end;
  263. var
  264. allowed : boolean;
  265. begin
  266. { power ** is always possible }
  267. if (treetyp=starstarn) then
  268. begin
  269. isbinaryoperatoroverloadable:=true;
  270. exit;
  271. end;
  272. { order of arguments does not matter so we have to check also
  273. the reversed order }
  274. allowed:=false;
  275. if not internal_check(treetyp,ld,lt,rd,rt,allowed) then
  276. internal_check(treetyp,rd,rt,ld,lt,allowed);
  277. isbinaryoperatoroverloadable:=allowed;
  278. end;
  279. function isunaryoperatoroverloadable(treetyp : tnodetype;ld : tdef) : boolean;
  280. begin
  281. result:=false;
  282. case treetyp of
  283. subn,
  284. unaryminusn :
  285. begin
  286. if (ld.deftype in [orddef,enumdef,floatdef]) then
  287. exit;
  288. {$ifdef SUPPORT_MMX}
  289. if (cs_mmx in aktlocalswitches) and
  290. is_mmx_able_array(ld) then
  291. exit;
  292. {$endif SUPPORT_MMX}
  293. result:=true;
  294. end;
  295. notn :
  296. begin
  297. if (ld.deftype in [orddef,enumdef,floatdef]) then
  298. exit;
  299. {$ifdef SUPPORT_MMX}
  300. if (cs_mmx in aktlocalswitches) and
  301. is_mmx_able_array(ld) then
  302. exit;
  303. {$endif SUPPORT_MMX}
  304. result:=true;
  305. end;
  306. end;
  307. end;
  308. function isoperatoracceptable(pf : tprocdef; optoken : ttoken) : boolean;
  309. var
  310. ld,rd : tdef;
  311. i : longint;
  312. eq : tequaltype;
  313. conv : tconverttype;
  314. pd : tprocdef;
  315. begin
  316. result:=false;
  317. case pf.parast.symindex.count of
  318. 1 : begin
  319. ld:=tparavarsym(pf.parast.symindex.first).vartype.def;
  320. { assignment is a special case }
  321. if optoken=_ASSIGNMENT then
  322. begin
  323. eq:=compare_defs_ext(ld,pf.rettype.def,nothingn,conv,pd,[cdo_explicit]);
  324. result:=(eq=te_incompatible);
  325. end
  326. else
  327. begin
  328. for i:=1 to tok2nodes do
  329. if tok2node[i].tok=optoken then
  330. begin
  331. result:=
  332. tok2node[i].op_overloading_supported and
  333. isunaryoperatoroverloadable(tok2node[i].nod,ld);
  334. break;
  335. end;
  336. end;
  337. end;
  338. 2 : begin
  339. for i:=1 to tok2nodes do
  340. if tok2node[i].tok=optoken then
  341. begin
  342. ld:=tparavarsym(pf.parast.symindex.first).vartype.def;
  343. rd:=tparavarsym(pf.parast.symindex.first.indexnext).vartype.def;
  344. result:=
  345. tok2node[i].op_overloading_supported and
  346. isbinaryoperatoroverloadable(tok2node[i].nod,ld,nothingn,rd,nothingn);
  347. break;
  348. end;
  349. end;
  350. end;
  351. end;
  352. function isunaryoverloaded(var t : tnode) : boolean;
  353. var
  354. ld : tdef;
  355. optoken : ttoken;
  356. operpd : tprocdef;
  357. ppn : tcallparanode;
  358. candidates : tcallcandidates;
  359. cand_cnt : integer;
  360. begin
  361. result:=false;
  362. operpd:=nil;
  363. { load easier access variables }
  364. ld:=tunarynode(t).left.resulttype.def;
  365. if not isunaryoperatoroverloadable(t.nodetype,ld) then
  366. exit;
  367. { operator overload is possible }
  368. result:=true;
  369. case t.nodetype of
  370. notn:
  371. optoken:=_OP_NOT;
  372. unaryminusn:
  373. optoken:=_MINUS;
  374. else
  375. begin
  376. CGMessage(parser_e_operator_not_overloaded);
  377. t:=cnothingnode.create;
  378. exit;
  379. end;
  380. end;
  381. { generate parameter nodes }
  382. ppn:=ccallparanode.create(tunarynode(t).left.getcopy,nil);
  383. ppn.get_paratype;
  384. candidates:=tcallcandidates.create_operator(optoken,ppn);
  385. { stop when there are no operators found }
  386. if candidates.count=0 then
  387. begin
  388. CGMessage(parser_e_operator_not_overloaded);
  389. candidates.free;
  390. ppn.free;
  391. t:=cnothingnode.create;
  392. exit;
  393. end;
  394. { Retrieve information about the candidates }
  395. candidates.get_information;
  396. {$ifdef EXTDEBUG}
  397. { Display info when multiple candidates are found }
  398. candidates.dump_info(V_Debug);
  399. {$endif EXTDEBUG}
  400. cand_cnt:=candidates.choose_best(operpd);
  401. { exit when no overloads are found }
  402. if cand_cnt=0 then
  403. begin
  404. CGMessage(parser_e_operator_not_overloaded);
  405. candidates.free;
  406. ppn.free;
  407. t:=cnothingnode.create;
  408. exit;
  409. end;
  410. { Multiple candidates left? }
  411. if cand_cnt>1 then
  412. begin
  413. CGMessage(type_e_cant_choose_overload_function);
  414. {$ifdef EXTDEBUG}
  415. candidates.dump_info(V_Hint);
  416. {$else EXTDEBUG}
  417. candidates.list(false);
  418. {$endif EXTDEBUG}
  419. { we'll just use the first candidate to make the
  420. call }
  421. end;
  422. candidates.free;
  423. inc(operpd.procsym.refs);
  424. { the nil as symtable signs firstcalln that this is
  425. an overloaded operator }
  426. t:=ccallnode.create(ppn,Tprocsym(operpd.procsym),nil,nil,[]);
  427. { we already know the procdef to use, so it can
  428. skip the overload choosing in callnode.det_resulttype }
  429. tcallnode(t).procdefinition:=operpd;
  430. end;
  431. function isbinaryoverloaded(var t : tnode) : boolean;
  432. var
  433. rd,ld : tdef;
  434. optoken : ttoken;
  435. operpd : tprocdef;
  436. ht : tnode;
  437. ppn : tcallparanode;
  438. candidates : tcallcandidates;
  439. cand_cnt : integer;
  440. begin
  441. isbinaryoverloaded:=false;
  442. operpd:=nil;
  443. { load easier access variables }
  444. ld:=tbinarynode(t).left.resulttype.def;
  445. rd:=tbinarynode(t).right.resulttype.def;
  446. if not isbinaryoperatoroverloadable(t.nodetype,ld,tbinarynode(t).left.nodetype,rd,tbinarynode(t).right.nodetype) then
  447. exit;
  448. { operator overload is possible }
  449. result:=true;
  450. case t.nodetype of
  451. equaln,
  452. unequaln :
  453. optoken:=_EQUAL;
  454. addn:
  455. optoken:=_PLUS;
  456. subn:
  457. optoken:=_MINUS;
  458. muln:
  459. optoken:=_STAR;
  460. starstarn:
  461. optoken:=_STARSTAR;
  462. slashn:
  463. optoken:=_SLASH;
  464. ltn:
  465. optoken:=_LT;
  466. gtn:
  467. optoken:=_GT;
  468. lten:
  469. optoken:=_LTE;
  470. gten:
  471. optoken:=_GTE;
  472. symdifn :
  473. optoken:=_SYMDIF;
  474. modn :
  475. optoken:=_OP_MOD;
  476. orn :
  477. optoken:=_OP_OR;
  478. xorn :
  479. optoken:=_OP_XOR;
  480. andn :
  481. optoken:=_OP_AND;
  482. divn :
  483. optoken:=_OP_DIV;
  484. shln :
  485. optoken:=_OP_SHL;
  486. shrn :
  487. optoken:=_OP_SHR;
  488. else
  489. begin
  490. CGMessage(parser_e_operator_not_overloaded);
  491. t:=cnothingnode.create;
  492. exit;
  493. end;
  494. end;
  495. { generate parameter nodes }
  496. ppn:=ccallparanode.create(tbinarynode(t).right.getcopy,ccallparanode.create(tbinarynode(t).left.getcopy,nil));
  497. ppn.get_paratype;
  498. candidates:=tcallcandidates.create_operator(optoken,ppn);
  499. { for commutative operators we can swap arguments and try again }
  500. if (candidates.count=0) and
  501. not(optoken in [_OP_SHL,_OP_SHR,_OP_DIV,_OP_MOD,_STARSTAR,_SLASH,_MINUS]) then
  502. begin
  503. candidates.free;
  504. reverseparameters(ppn);
  505. { reverse compare operators }
  506. case optoken of
  507. _LT:
  508. optoken:=_GTE;
  509. _GT:
  510. optoken:=_LTE;
  511. _LTE:
  512. optoken:=_GT;
  513. _GTE:
  514. optoken:=_LT;
  515. end;
  516. candidates:=tcallcandidates.create_operator(optoken,ppn);
  517. end;
  518. { stop when there are no operators found }
  519. if candidates.count=0 then
  520. begin
  521. CGMessage(parser_e_operator_not_overloaded);
  522. candidates.free;
  523. ppn.free;
  524. t:=cnothingnode.create;
  525. exit;
  526. end;
  527. { Retrieve information about the candidates }
  528. candidates.get_information;
  529. {$ifdef EXTDEBUG}
  530. { Display info when multiple candidates are found }
  531. candidates.dump_info(V_Debug);
  532. {$endif EXTDEBUG}
  533. cand_cnt:=candidates.choose_best(operpd);
  534. { exit when no overloads are found }
  535. if cand_cnt=0 then
  536. begin
  537. CGMessage(parser_e_operator_not_overloaded);
  538. candidates.free;
  539. ppn.free;
  540. t:=cnothingnode.create;
  541. exit;
  542. end;
  543. { Multiple candidates left? }
  544. if cand_cnt>1 then
  545. begin
  546. CGMessage(type_e_cant_choose_overload_function);
  547. {$ifdef EXTDEBUG}
  548. candidates.dump_info(V_Hint);
  549. {$else EXTDEBUG}
  550. candidates.list(false);
  551. {$endif EXTDEBUG}
  552. { we'll just use the first candidate to make the
  553. call }
  554. end;
  555. candidates.free;
  556. inc(operpd.procsym.refs);
  557. { the nil as symtable signs firstcalln that this is
  558. an overloaded operator }
  559. ht:=ccallnode.create(ppn,Tprocsym(operpd.procsym),nil,nil,[]);
  560. { we already know the procdef to use, so it can
  561. skip the overload choosing in callnode.det_resulttype }
  562. tcallnode(ht).procdefinition:=operpd;
  563. if t.nodetype=unequaln then
  564. ht:=cnotnode.create(ht);
  565. t:=ht;
  566. end;
  567. {****************************************************************************
  568. Register Calculation
  569. ****************************************************************************}
  570. { marks an lvalue as "unregable" }
  571. procedure make_not_regable(p : tnode);
  572. begin
  573. case p.nodetype of
  574. typeconvn :
  575. make_not_regable(ttypeconvnode(p).left);
  576. loadn :
  577. if tloadnode(p).symtableentry.typ in [globalvarsym,localvarsym,paravarsym] then
  578. tabstractvarsym(tloadnode(p).symtableentry).varregable:=vr_none;
  579. end;
  580. end;
  581. { calculates the needed registers for a binary operator }
  582. procedure calcregisters(p : tbinarynode;r32,fpu,mmx : word);
  583. begin
  584. p.left_right_max;
  585. { Only when the difference between the left and right registers < the
  586. wanted registers allocate the amount of registers }
  587. if assigned(p.left) then
  588. begin
  589. if assigned(p.right) then
  590. begin
  591. { the location must be already filled in because we need it to }
  592. { calculate the necessary number of registers (JM) }
  593. if p.expectloc = LOC_INVALID then
  594. internalerror(200110101);
  595. if (abs(p.left.registersint-p.right.registersint)<r32) or
  596. ((p.expectloc = LOC_FPUREGISTER) and
  597. (p.right.registersfpu <= p.left.registersfpu) and
  598. ((p.right.registersfpu <> 0) or (p.left.registersfpu <> 0)) and
  599. (p.left.registersint < p.right.registersint)) then
  600. inc(p.registersint,r32);
  601. if (abs(p.left.registersfpu-p.right.registersfpu)<fpu) then
  602. inc(p.registersfpu,fpu);
  603. {$ifdef SUPPORT_MMX}
  604. if (abs(p.left.registersmmx-p.right.registersmmx)<mmx) then
  605. inc(p.registersmmx,mmx);
  606. {$endif SUPPORT_MMX}
  607. { the following is a little bit guessing but I think }
  608. { it's the only way to solve same internalerrors: }
  609. { if the left and right node both uses registers }
  610. { and return a mem location, but the current node }
  611. { doesn't use an integer register we get probably }
  612. { trouble when restoring a node }
  613. if (p.left.registersint=p.right.registersint) and
  614. (p.registersint=p.left.registersint) and
  615. (p.registersint>0) and
  616. (p.left.expectloc in [LOC_REFERENCE,LOC_CREFERENCE]) and
  617. (p.right.expectloc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  618. inc(p.registersint);
  619. end
  620. else
  621. begin
  622. if (p.left.registersint<r32) then
  623. inc(p.registersint,r32);
  624. if (p.left.registersfpu<fpu) then
  625. inc(p.registersfpu,fpu);
  626. {$ifdef SUPPORT_MMX}
  627. if (p.left.registersmmx<mmx) then
  628. inc(p.registersmmx,mmx);
  629. {$endif SUPPORT_MMX}
  630. end;
  631. end;
  632. end;
  633. {****************************************************************************
  634. Subroutine Handling
  635. ****************************************************************************}
  636. function is_procvar_load(p:tnode):boolean;
  637. begin
  638. result:=false;
  639. { remove voidpointer typecast for tp procvars }
  640. if (m_tp_procvar in aktmodeswitches) and
  641. (p.nodetype=typeconvn) and
  642. is_voidpointer(p.resulttype.def) then
  643. p:=tunarynode(p).left;
  644. result:=(p.nodetype=typeconvn) and
  645. (ttypeconvnode(p).convtype=tc_proc_2_procvar);
  646. end;
  647. { local routines can't be assigned to procvars }
  648. procedure test_local_to_procvar(from_def:tprocvardef;to_def:tdef);
  649. begin
  650. if (from_def.parast.symtablelevel>normal_function_level) and
  651. (to_def.deftype=procvardef) then
  652. CGMessage(type_e_cannot_local_proc_to_procvar);
  653. end;
  654. procedure set_varstate(p:tnode;newstate:tvarstate;varstateflags:tvarstateflags);
  655. var
  656. hsym : tabstractvarsym;
  657. begin
  658. while assigned(p) do
  659. begin
  660. case p.nodetype of
  661. typeconvn :
  662. begin
  663. case ttypeconvnode(p).convtype of
  664. tc_cchar_2_pchar,
  665. tc_cstring_2_pchar,
  666. tc_array_2_pointer :
  667. exclude(varstateflags,vsf_must_be_valid);
  668. tc_pchar_2_string,
  669. tc_pointer_2_array :
  670. include(varstateflags,vsf_must_be_valid);
  671. end;
  672. p:=tunarynode(p).left;
  673. end;
  674. subscriptn :
  675. p:=tunarynode(p).left;
  676. vecn:
  677. begin
  678. set_varstate(tbinarynode(p).right,vs_used,[vsf_must_be_valid]);
  679. if not(tunarynode(p).left.resulttype.def.deftype in [stringdef,arraydef]) then
  680. include(varstateflags,vsf_must_be_valid);
  681. p:=tunarynode(p).left;
  682. end;
  683. { do not parse calln }
  684. calln :
  685. break;
  686. loadn :
  687. begin
  688. if (tloadnode(p).symtableentry.typ in [localvarsym,paravarsym,globalvarsym]) then
  689. begin
  690. hsym:=tabstractvarsym(tloadnode(p).symtableentry);
  691. if (vsf_must_be_valid in varstateflags) and (hsym.varstate=vs_declared) then
  692. begin
  693. { Give warning/note for uninitialized locals }
  694. if assigned(hsym.owner) and
  695. not(vo_is_external in hsym.varoptions) and
  696. (hsym.owner.symtabletype in [localsymtable,staticsymtable]) and
  697. (hsym.owner=current_procinfo.procdef.localst) then
  698. begin
  699. if (vo_is_funcret in hsym.varoptions) then
  700. CGMessage(sym_w_function_result_not_set)
  701. else
  702. begin
  703. if tloadnode(p).symtable.symtabletype=localsymtable then
  704. begin
  705. if (vsf_use_hints in varstateflags) then
  706. CGMessage1(sym_h_uninitialized_local_variable,hsym.realname)
  707. else
  708. CGMessage1(sym_w_uninitialized_local_variable,hsym.realname);
  709. end
  710. else
  711. begin
  712. if (vsf_use_hints in varstateflags) then
  713. CGMessage1(sym_h_uninitialized_variable,hsym.realname)
  714. else
  715. CGMessage1(sym_w_uninitialized_variable,hsym.realname);
  716. end;
  717. end;
  718. end;
  719. end;
  720. { don't override vs_used with vs_assigned }
  721. if hsym.varstate<>vs_used then
  722. hsym.varstate:=newstate;
  723. end;
  724. break;
  725. end;
  726. callparan :
  727. internalerror(200310081);
  728. else
  729. break;
  730. end;{case }
  731. end;
  732. end;
  733. procedure set_unique(p : tnode);
  734. begin
  735. while assigned(p) do
  736. begin
  737. case p.nodetype of
  738. vecn:
  739. begin
  740. include(p.flags,nf_callunique);
  741. break;
  742. end;
  743. typeconvn,
  744. subscriptn,
  745. derefn:
  746. p:=tunarynode(p).left;
  747. else
  748. break;
  749. end;
  750. end;
  751. end;
  752. function valid_for_assign(p:tnode;opts:TValidAssigns):boolean;
  753. var
  754. hp : tnode;
  755. gotwith,
  756. gotsubscript,
  757. gotpointer,
  758. gotvec,
  759. gotclass,
  760. gotdynarray,
  761. gotderef : boolean;
  762. fromdef,
  763. todef : tdef;
  764. errmsg : longint;
  765. begin
  766. if valid_const in opts then
  767. errmsg:=type_e_variable_id_expected
  768. else
  769. errmsg:=type_e_argument_cant_be_assigned;
  770. result:=false;
  771. gotsubscript:=false;
  772. gotvec:=false;
  773. gotderef:=false;
  774. gotclass:=false;
  775. gotpointer:=false;
  776. gotwith:=false;
  777. gotdynarray:=false;
  778. hp:=p;
  779. if not(valid_void in opts) and
  780. is_void(hp.resulttype.def) then
  781. begin
  782. CGMessagePos(hp.fileinfo,errmsg);
  783. exit;
  784. end;
  785. while assigned(hp) do
  786. begin
  787. { property allowed? calln has a property check itself }
  788. if (nf_isproperty in hp.flags) then
  789. begin
  790. if (valid_property in opts) then
  791. result:=true
  792. else
  793. begin
  794. { check return type }
  795. case hp.resulttype.def.deftype of
  796. pointerdef :
  797. gotpointer:=true;
  798. objectdef :
  799. gotclass:=is_class_or_interface(hp.resulttype.def);
  800. recorddef, { handle record like class it needs a subscription }
  801. classrefdef :
  802. gotclass:=true;
  803. end;
  804. { 1. if it returns a pointer and we've found a deref,
  805. 2. if it returns a class or record and a subscription or with is found
  806. 3. if the address is needed of a field (subscriptn) }
  807. if (gotpointer and gotderef) or
  808. (
  809. gotclass and
  810. (gotsubscript or gotwith)
  811. ) or
  812. (
  813. (gotvec and gotdynarray)
  814. ) or
  815. (
  816. (Valid_Addr in opts) and
  817. (hp.nodetype=subscriptn)
  818. ) then
  819. result:=true
  820. else
  821. CGMessagePos(hp.fileinfo,errmsg);
  822. end;
  823. exit;
  824. end;
  825. if (Valid_Const in opts) and is_constnode(hp) then
  826. begin
  827. result:=true;
  828. exit;
  829. end;
  830. case hp.nodetype of
  831. temprefn :
  832. begin
  833. valid_for_assign := true;
  834. exit;
  835. end;
  836. derefn :
  837. begin
  838. gotderef:=true;
  839. hp:=tderefnode(hp).left;
  840. end;
  841. typeconvn :
  842. begin
  843. { typecast sizes must match, exceptions:
  844. - implicit typecast made by absolute
  845. - from formaldef
  846. - from void
  847. - from/to open array
  848. - typecast from pointer to array }
  849. fromdef:=ttypeconvnode(hp).left.resulttype.def;
  850. todef:=hp.resulttype.def;
  851. if not((nf_absolute in ttypeconvnode(hp).flags) or
  852. (fromdef.deftype=formaldef) or
  853. is_void(fromdef) or
  854. is_open_array(fromdef) or
  855. is_open_array(todef) or
  856. ((fromdef.deftype=pointerdef) and (todef.deftype=arraydef)) or
  857. ((fromdef.deftype = objectdef) and (todef.deftype = objectdef) and
  858. (tobjectdef(fromdef).is_related(tobjectdef(todef))))) and
  859. (fromdef.size<>todef.size) then
  860. begin
  861. { in TP it is allowed to typecast to smaller types. But the variable can't
  862. be in a register }
  863. if (m_tp7 in aktmodeswitches) or
  864. (todef.size<fromdef.size) then
  865. make_not_regable(hp)
  866. else
  867. CGMessagePos2(hp.fileinfo,type_e_typecast_wrong_size_for_assignment,tostr(fromdef.size),tostr(todef.size));
  868. end;
  869. { don't allow assignments to typeconvs that need special code }
  870. if not(gotsubscript or gotvec or gotderef) and
  871. not(ttypeconvnode(hp).assign_allowed) then
  872. begin
  873. CGMessagePos(hp.fileinfo,errmsg);
  874. exit;
  875. end;
  876. case hp.resulttype.def.deftype of
  877. pointerdef :
  878. gotpointer:=true;
  879. objectdef :
  880. gotclass:=is_class_or_interface(hp.resulttype.def);
  881. classrefdef :
  882. gotclass:=true;
  883. arraydef :
  884. begin
  885. { pointer -> array conversion is done then we need to see it
  886. as a deref, because a ^ is then not required anymore }
  887. if (ttypeconvnode(hp).left.resulttype.def.deftype=pointerdef) then
  888. gotderef:=true;
  889. end;
  890. end;
  891. hp:=ttypeconvnode(hp).left;
  892. end;
  893. vecn :
  894. begin
  895. gotvec:=true;
  896. { accesses to dyn. arrays override read only access in delphi }
  897. if (m_delphi in aktmodeswitches) and is_dynamic_array(tunarynode(hp).left.resulttype.def) then
  898. gotdynarray:=true;
  899. hp:=tunarynode(hp).left;
  900. end;
  901. asn :
  902. begin
  903. { asn can't be assigned directly, it returns the value in a register instead
  904. of reference. }
  905. if not(gotsubscript or gotderef or gotvec) then
  906. begin
  907. CGMessagePos(hp.fileinfo,errmsg);
  908. exit;
  909. end;
  910. hp:=tunarynode(hp).left;
  911. end;
  912. subscriptn :
  913. begin
  914. gotsubscript:=true;
  915. { loop counter? }
  916. if not(Valid_Const in opts) and
  917. (vo_is_loop_counter in tsubscriptnode(hp).vs.varoptions) then
  918. CGMessage1(parser_e_illegal_assignment_to_count_var,tsubscriptnode(hp).vs.realname);
  919. { a class/interface access is an implicit }
  920. { dereferencing }
  921. hp:=tsubscriptnode(hp).left;
  922. if is_class_or_interface(hp.resulttype.def) then
  923. gotderef:=true;
  924. end;
  925. muln,
  926. divn,
  927. andn,
  928. xorn,
  929. orn,
  930. notn,
  931. subn,
  932. addn :
  933. begin
  934. { Allow operators on a pointer, or an integer
  935. and a pointer typecast and deref has been found }
  936. if ((hp.resulttype.def.deftype=pointerdef) or
  937. (is_integer(hp.resulttype.def) and gotpointer)) and
  938. gotderef then
  939. result:=true
  940. else
  941. { Temp strings are stored in memory, for compatibility with
  942. delphi only }
  943. if (m_delphi in aktmodeswitches) and
  944. (valid_addr in opts) and
  945. (hp.resulttype.def.deftype=stringdef) then
  946. result:=true
  947. else
  948. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  949. exit;
  950. end;
  951. niln,
  952. pointerconstn :
  953. begin
  954. { to support e.g. @tmypointer(0)^.data; see tests/tbs/tb0481 }
  955. if gotderef then
  956. result:=true
  957. else
  958. CGMessagePos(hp.fileinfo,type_e_no_assign_to_addr);
  959. exit;
  960. end;
  961. addrn :
  962. begin
  963. if gotderef then
  964. result:=true
  965. else
  966. CGMessagePos(hp.fileinfo,type_e_no_assign_to_addr);
  967. exit;
  968. end;
  969. calln :
  970. begin
  971. { check return type }
  972. case hp.resulttype.def.deftype of
  973. arraydef :
  974. begin
  975. { dynamic arrays are allowed when there is also a
  976. vec node }
  977. if is_dynamic_array(hp.resulttype.def) and
  978. gotvec then
  979. begin
  980. gotderef:=true;
  981. gotpointer:=true;
  982. end;
  983. end;
  984. pointerdef :
  985. gotpointer:=true;
  986. objectdef :
  987. gotclass:=is_class_or_interface(hp.resulttype.def);
  988. recorddef, { handle record like class it needs a subscription }
  989. classrefdef :
  990. gotclass:=true;
  991. end;
  992. { 1. if it returns a pointer and we've found a deref,
  993. 2. if it returns a class or record and a subscription or with is found }
  994. if (gotpointer and gotderef) or
  995. (gotclass and (gotsubscript or gotwith)) then
  996. result:=true
  997. else
  998. { Temp strings are stored in memory, for compatibility with
  999. delphi only }
  1000. if (m_delphi in aktmodeswitches) and
  1001. (valid_addr in opts) and
  1002. (hp.resulttype.def.deftype=stringdef) then
  1003. result:=true
  1004. else
  1005. CGMessagePos(hp.fileinfo,errmsg);
  1006. exit;
  1007. end;
  1008. inlinen :
  1009. begin
  1010. if (valid_const in opts) and
  1011. (tinlinenode(hp).inlinenumber in [in_typeof_x]) then
  1012. result:=true
  1013. else
  1014. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  1015. exit;
  1016. end;
  1017. loadn :
  1018. begin
  1019. case tloadnode(hp).symtableentry.typ of
  1020. absolutevarsym,
  1021. globalvarsym,
  1022. localvarsym,
  1023. paravarsym :
  1024. begin
  1025. { loop counter? }
  1026. if not(Valid_Const in opts) and
  1027. (vo_is_loop_counter in tabstractvarsym(tloadnode(hp).symtableentry).varoptions) then
  1028. CGMessage1(parser_e_illegal_assignment_to_count_var,tloadnode(hp).symtableentry.realname);
  1029. { derefed pointer }
  1030. if (tabstractvarsym(tloadnode(hp).symtableentry).varspez=vs_const) then
  1031. begin
  1032. { allow p^:= constructions with p is const parameter }
  1033. if gotderef or gotdynarray or (Valid_Const in opts) then
  1034. result:=true
  1035. else
  1036. CGMessagePos(tloadnode(hp).fileinfo,type_e_no_assign_to_const);
  1037. exit;
  1038. end;
  1039. { Are we at a with symtable, then we need to process the
  1040. withrefnode also to check for maybe a const load }
  1041. if (tloadnode(hp).symtable.symtabletype=withsymtable) then
  1042. begin
  1043. { continue with processing the withref node }
  1044. hp:=tnode(twithsymtable(tloadnode(hp).symtable).withrefnode);
  1045. gotwith:=true;
  1046. end
  1047. else
  1048. begin
  1049. result:=true;
  1050. exit;
  1051. end;
  1052. end;
  1053. typedconstsym :
  1054. begin
  1055. if ttypedconstsym(tloadnode(hp).symtableentry).is_writable then
  1056. result:=true
  1057. else
  1058. CGMessagePos(hp.fileinfo,type_e_no_assign_to_const);
  1059. exit;
  1060. end;
  1061. procsym :
  1062. begin
  1063. if (Valid_Const in opts) then
  1064. result:=true
  1065. else
  1066. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  1067. exit;
  1068. end;
  1069. labelsym :
  1070. begin
  1071. if (Valid_Addr in opts) then
  1072. result:=true
  1073. else
  1074. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  1075. exit;
  1076. end;
  1077. constsym:
  1078. begin
  1079. if (tconstsym(tloadnode(hp).symtableentry).consttyp=constresourcestring) and
  1080. (valid_addr in opts) then
  1081. result:=true
  1082. else
  1083. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  1084. exit;
  1085. end;
  1086. else
  1087. begin
  1088. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  1089. exit;
  1090. end;
  1091. end;
  1092. end;
  1093. else
  1094. begin
  1095. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  1096. exit;
  1097. end;
  1098. end;
  1099. end;
  1100. end;
  1101. function valid_for_var(p:tnode):boolean;
  1102. begin
  1103. valid_for_var:=valid_for_assign(p,[]);
  1104. end;
  1105. function valid_for_formal_var(p : tnode) : boolean;
  1106. begin
  1107. valid_for_formal_var:=valid_for_assign(p,[valid_void]);
  1108. end;
  1109. function valid_for_formal_const(p : tnode) : boolean;
  1110. begin
  1111. valid_for_formal_const:=(p.resulttype.def.deftype=formaldef) or
  1112. valid_for_assign(p,[valid_void,valid_const,valid_property]);
  1113. end;
  1114. function valid_for_assignment(p:tnode):boolean;
  1115. begin
  1116. valid_for_assignment:=valid_for_assign(p,[valid_property]);
  1117. end;
  1118. function valid_for_addr(p : tnode) : boolean;
  1119. begin
  1120. result:=valid_for_assign(p,[valid_const,valid_addr,valid_void]);
  1121. end;
  1122. procedure var_para_allowed(var eq:tequaltype;def_from,def_to:Tdef);
  1123. begin
  1124. { Note: eq must be already valid, it will only be updated! }
  1125. case def_to.deftype of
  1126. formaldef :
  1127. begin
  1128. { all types can be passed to a formaldef }
  1129. eq:=te_equal;
  1130. end;
  1131. orddef :
  1132. begin
  1133. { allows conversion from word to integer and
  1134. byte to shortint, but only for TP7 compatibility }
  1135. if (m_tp7 in aktmodeswitches) and
  1136. (def_from.deftype=orddef) and
  1137. (def_from.size=def_to.size) then
  1138. eq:=te_convert_l1;
  1139. end;
  1140. arraydef :
  1141. begin
  1142. if is_open_array(def_to) and
  1143. is_dynamic_array(def_from) and
  1144. equal_defs(tarraydef(def_from).elementtype.def,tarraydef(def_to).elementtype.def) then
  1145. eq:=te_convert_l2;
  1146. end;
  1147. pointerdef :
  1148. begin
  1149. { an implicit pointer conversion is allowed }
  1150. if (def_from.deftype=pointerdef) then
  1151. eq:=te_convert_l1;
  1152. end;
  1153. stringdef :
  1154. begin
  1155. { all shortstrings are allowed, size is not important }
  1156. if is_shortstring(def_from) and
  1157. is_shortstring(def_to) then
  1158. eq:=te_equal;
  1159. end;
  1160. objectdef :
  1161. begin
  1162. { child objects can be also passed }
  1163. { in non-delphi mode, otherwise }
  1164. { they must match exactly, except }
  1165. { if they are objects }
  1166. if (def_from.deftype=objectdef) and
  1167. (
  1168. not(m_delphi in aktmodeswitches) or
  1169. (
  1170. (tobjectdef(def_from).objecttype=odt_object) and
  1171. (tobjectdef(def_to).objecttype=odt_object)
  1172. )
  1173. ) and
  1174. (tobjectdef(def_from).is_related(tobjectdef(def_to))) then
  1175. eq:=te_convert_l1;
  1176. end;
  1177. filedef :
  1178. begin
  1179. { an implicit file conversion is also allowed }
  1180. { from a typed file to an untyped one }
  1181. if (def_from.deftype=filedef) and
  1182. (tfiledef(def_from).filetyp = ft_typed) and
  1183. (tfiledef(def_to).filetyp = ft_untyped) then
  1184. eq:=te_convert_l1;
  1185. end;
  1186. end;
  1187. end;
  1188. procedure para_allowed(var eq:tequaltype;p:tcallparanode;def_to:tdef);
  1189. begin
  1190. { Note: eq must be already valid, it will only be updated! }
  1191. case def_to.deftype of
  1192. formaldef :
  1193. begin
  1194. { all types can be passed to a formaldef }
  1195. eq:=te_equal;
  1196. end;
  1197. stringdef :
  1198. begin
  1199. { to support ansi/long/wide strings in a proper way }
  1200. { string and string[10] are assumed as equal }
  1201. { when searching the correct overloaded procedure }
  1202. if (p.resulttype.def.deftype=stringdef) and
  1203. (tstringdef(def_to).string_typ=tstringdef(p.resulttype.def).string_typ) then
  1204. eq:=te_equal
  1205. else
  1206. { Passing a constant char to ansistring or shortstring or
  1207. a widechar to widestring then handle it as equal. }
  1208. if (p.left.nodetype=ordconstn) and
  1209. (
  1210. is_char(p.resulttype.def) and
  1211. (is_shortstring(def_to) or is_ansistring(def_to))
  1212. ) or
  1213. (
  1214. is_widechar(p.resulttype.def) and
  1215. is_widestring(def_to)
  1216. ) then
  1217. eq:=te_equal
  1218. end;
  1219. setdef :
  1220. begin
  1221. { set can also be a not yet converted array constructor }
  1222. if (p.resulttype.def.deftype=arraydef) and
  1223. (tarraydef(p.resulttype.def).IsConstructor) and
  1224. not(tarraydef(p.resulttype.def).IsVariant) then
  1225. eq:=te_equal;
  1226. end;
  1227. procvardef :
  1228. begin
  1229. { in tp7 mode proc -> procvar is allowed }
  1230. if (m_tp_procvar in aktmodeswitches) and
  1231. (p.left.nodetype=calln) and
  1232. (proc_to_procvar_equal(tprocdef(tcallnode(p.left).procdefinition),tprocvardef(def_to),true)>=te_equal) then
  1233. eq:=te_equal;
  1234. end;
  1235. end;
  1236. end;
  1237. {****************************************************************************
  1238. TCallCandidates
  1239. ****************************************************************************}
  1240. constructor tcallcandidates.create(sym:tprocsym;st:tsymtable;ppn:tnode;isprop,ignorevis : boolean);
  1241. var
  1242. j : integer;
  1243. pd : tprocdef;
  1244. hp : pcandidate;
  1245. found,
  1246. has_overload_directive : boolean;
  1247. topclassh : tobjectdef;
  1248. srsymtable : tsymtable;
  1249. srprocsym : tprocsym;
  1250. pt : tcallparanode;
  1251. begin
  1252. if not assigned(sym) then
  1253. internalerror(200411015);
  1254. FProcSym:=sym;
  1255. FProcs:=nil;
  1256. FProccnt:=0;
  1257. FProcvisiblecnt:=0;
  1258. FParanode:=ppn;
  1259. FAllowVariant:=true;
  1260. { determine length of parameter list }
  1261. pt:=tcallparanode(ppn);
  1262. FParalength:=0;
  1263. while assigned(pt) do
  1264. begin
  1265. inc(FParalength);
  1266. pt:=tcallparanode(pt.right);
  1267. end;
  1268. { when the definition has overload directive set, we search for
  1269. overloaded definitions in the class, this only needs to be done once
  1270. for class entries as the tree keeps always the same }
  1271. if (not sym.overloadchecked) and
  1272. (sym.owner.symtabletype=objectsymtable) and
  1273. (po_overload in sym.first_procdef.procoptions) then
  1274. search_class_overloads(sym);
  1275. { when the class passed is defined in this unit we
  1276. need to use the scope of that class. This is a trick
  1277. that can be used to access protected members in other
  1278. units. At least kylix supports it this way (PFV) }
  1279. if assigned(st) and
  1280. (st.symtabletype=objectsymtable) and
  1281. (st.defowner.owner.symtabletype in [globalsymtable,staticsymtable]) and
  1282. st.defowner.owner.iscurrentunit then
  1283. topclassh:=tobjectdef(st.defowner)
  1284. else
  1285. begin
  1286. if assigned(current_procinfo) then
  1287. topclassh:=current_procinfo.procdef._class
  1288. else
  1289. topclassh:=nil;
  1290. end;
  1291. { link all procedures which have the same # of parameters }
  1292. for j:=1 to sym.procdef_count do
  1293. begin
  1294. pd:=sym.procdef[j];
  1295. { Is the procdef visible? This needs to be checked on
  1296. procdef level since a symbol can contain both private and
  1297. public declarations. But the check should not be done
  1298. when the callnode is generated by a property
  1299. inherited overrides invisible anonymous inherited (FK) }
  1300. if isprop or ignorevis or
  1301. (pd.owner.symtabletype<>objectsymtable) or
  1302. pd.is_visible_for_object(topclassh) then
  1303. begin
  1304. { we have at least one procedure that is visible }
  1305. inc(FProcvisiblecnt);
  1306. { only when the # of parameter are supported by the
  1307. procedure }
  1308. if (FParalength>=pd.minparacount) and
  1309. ((po_varargs in pd.procoptions) or { varargs }
  1310. (FParalength<=pd.maxparacount)) then
  1311. proc_add(pd);
  1312. end;
  1313. end;
  1314. { remember if the procedure is declared with the overload directive,
  1315. it's information is still needed also after all procs are removed }
  1316. has_overload_directive:=(po_overload in sym.first_procdef.procoptions);
  1317. { when the definition has overload directive set, we search for
  1318. overloaded definitions in the symtablestack. The found
  1319. entries are only added to the procs list and not the procsym, because
  1320. the list can change in every situation }
  1321. if has_overload_directive and
  1322. (sym.owner.symtabletype<>objectsymtable) then
  1323. begin
  1324. srsymtable:=sym.owner.next;
  1325. while assigned(srsymtable) do
  1326. begin
  1327. if srsymtable.symtabletype in [localsymtable,staticsymtable,globalsymtable] then
  1328. begin
  1329. srprocsym:=tprocsym(srsymtable.speedsearch(sym.name,sym.speedvalue));
  1330. if assigned(srprocsym) and
  1331. (srprocsym.typ=procsym) then
  1332. begin
  1333. { if this visible procedure doesn't have overload we can stop
  1334. searching }
  1335. if not(po_overload in srprocsym.first_procdef.procoptions) and
  1336. srprocsym.first_procdef.is_visible_for_object(topclassh) then
  1337. break;
  1338. { process all overloaded definitions }
  1339. for j:=1 to srprocsym.procdef_count do
  1340. begin
  1341. pd:=srprocsym.procdef[j];
  1342. { only visible procedures need to be added }
  1343. if pd.is_visible_for_object(topclassh) then
  1344. begin
  1345. { only when the # of parameter are supported by the
  1346. procedure }
  1347. if (FParalength>=pd.minparacount) and
  1348. ((po_varargs in pd.procoptions) or { varargs }
  1349. (FParalength<=pd.maxparacount)) then
  1350. begin
  1351. found:=false;
  1352. hp:=FProcs;
  1353. while assigned(hp) do
  1354. begin
  1355. { Only compare visible parameters for the user }
  1356. if compare_paras(hp^.data.paras,pd.paras,cp_value_equal_const,[cpo_ignorehidden])>=te_equal then
  1357. begin
  1358. found:=true;
  1359. break;
  1360. end;
  1361. hp:=hp^.next;
  1362. end;
  1363. if not found then
  1364. proc_add(pd);
  1365. end;
  1366. end;
  1367. end;
  1368. end;
  1369. end;
  1370. srsymtable:=srsymtable.next;
  1371. end;
  1372. end;
  1373. end;
  1374. constructor tcallcandidates.create_operator(op:ttoken;ppn:tnode);
  1375. var
  1376. j : integer;
  1377. pd : tprocdef;
  1378. hp : pcandidate;
  1379. found : boolean;
  1380. srsymtable : tsymtable;
  1381. srprocsym : tprocsym;
  1382. pt : tcallparanode;
  1383. sv : cardinal;
  1384. begin
  1385. FProcSym:=nil;
  1386. FProcs:=nil;
  1387. FProccnt:=0;
  1388. FProcvisiblecnt:=0;
  1389. FParanode:=ppn;
  1390. FAllowVariant:=false;
  1391. { determine length of parameter list }
  1392. pt:=tcallparanode(ppn);
  1393. FParalength:=0;
  1394. while assigned(pt) do
  1395. begin
  1396. if pt.resulttype.def.deftype=variantdef then
  1397. FAllowVariant:=true;
  1398. inc(FParalength);
  1399. pt:=tcallparanode(pt.right);
  1400. end;
  1401. { we search all overloaded operator definitions in the symtablestack. The found
  1402. entries are only added to the procs list and not the procsym, because
  1403. the list can change in every situation }
  1404. sv:=getspeedvalue(overloaded_names[op]);
  1405. srsymtable:=symtablestack;
  1406. while assigned(srsymtable) do
  1407. begin
  1408. if srsymtable.symtabletype in [localsymtable,staticsymtable,globalsymtable] then
  1409. begin
  1410. srprocsym:=tprocsym(srsymtable.speedsearch(overloaded_names[op],sv));
  1411. if assigned(srprocsym) and
  1412. (srprocsym.typ=procsym) then
  1413. begin
  1414. { Store first procsym found }
  1415. if not assigned(FProcsym) then
  1416. FProcsym:=srprocsym;
  1417. { process all overloaded definitions }
  1418. for j:=1 to srprocsym.procdef_count do
  1419. begin
  1420. pd:=srprocsym.procdef[j];
  1421. { only when the # of parameter are supported by the
  1422. procedure }
  1423. if (FParalength>=pd.minparacount) and
  1424. (FParalength<=pd.maxparacount) then
  1425. begin
  1426. found:=false;
  1427. hp:=FProcs;
  1428. while assigned(hp) do
  1429. begin
  1430. { Only compare visible parameters for the user }
  1431. if compare_paras(hp^.data.paras,pd.paras,cp_value_equal_const,[cpo_ignorehidden])>=te_equal then
  1432. begin
  1433. found:=true;
  1434. break;
  1435. end;
  1436. hp:=hp^.next;
  1437. end;
  1438. if not found then
  1439. proc_add(pd);
  1440. end;
  1441. end;
  1442. end;
  1443. end;
  1444. srsymtable:=srsymtable.next;
  1445. end;
  1446. end;
  1447. destructor tcallcandidates.destroy;
  1448. var
  1449. hpnext,
  1450. hp : pcandidate;
  1451. begin
  1452. hp:=FProcs;
  1453. while assigned(hp) do
  1454. begin
  1455. hpnext:=hp^.next;
  1456. dispose(hp);
  1457. hp:=hpnext;
  1458. end;
  1459. end;
  1460. function tcallcandidates.proc_add(pd:tprocdef):pcandidate;
  1461. var
  1462. defaultparacnt : integer;
  1463. begin
  1464. { generate new candidate entry }
  1465. new(result);
  1466. fillchar(result^,sizeof(tcandidate),0);
  1467. result^.data:=pd;
  1468. result^.next:=FProcs;
  1469. FProcs:=result;
  1470. inc(FProccnt);
  1471. { Find last parameter, skip all default parameters
  1472. that are not passed. Ignore this skipping for varargs }
  1473. result^.firstparaidx:=pd.paras.count-1;
  1474. if not(po_varargs in pd.procoptions) then
  1475. begin
  1476. { ignore hidden parameters }
  1477. while (result^.firstparaidx>=0) and (vo_is_hidden_para in tparavarsym(pd.paras[result^.firstparaidx]).varoptions) do
  1478. dec(result^.firstparaidx);
  1479. defaultparacnt:=pd.maxparacount-FParalength;
  1480. if defaultparacnt>0 then
  1481. begin
  1482. if defaultparacnt>result^.firstparaidx+1 then
  1483. internalerror(200401141);
  1484. dec(result^.firstparaidx,defaultparacnt);
  1485. end;
  1486. end;
  1487. end;
  1488. procedure tcallcandidates.list(all:boolean);
  1489. var
  1490. hp : pcandidate;
  1491. begin
  1492. hp:=FProcs;
  1493. while assigned(hp) do
  1494. begin
  1495. if all or
  1496. (not hp^.invalid) then
  1497. MessagePos1(hp^.data.fileinfo,sym_h_param_list,hp^.data.fullprocname(false));
  1498. hp:=hp^.next;
  1499. end;
  1500. end;
  1501. {$ifdef EXTDEBUG}
  1502. procedure tcallcandidates.dump_info(lvl:longint);
  1503. function ParaTreeStr(p:tcallparanode):string;
  1504. begin
  1505. result:='';
  1506. while assigned(p) do
  1507. begin
  1508. if result<>'' then
  1509. result:=result+',';
  1510. result:=result+p.resulttype.def.typename;
  1511. p:=tcallparanode(p.right);
  1512. end;
  1513. end;
  1514. var
  1515. hp : pcandidate;
  1516. i : integer;
  1517. currpara : tparavarsym;
  1518. begin
  1519. if not CheckVerbosity(lvl) then
  1520. exit;
  1521. Comment(lvl+V_LineInfo,'Overloaded callnode: '+FProcSym.name+'('+ParaTreeStr(tcallparanode(FParaNode))+')');
  1522. hp:=FProcs;
  1523. while assigned(hp) do
  1524. begin
  1525. Comment(lvl,' '+hp^.data.fullprocname(false));
  1526. if (hp^.invalid) then
  1527. Comment(lvl,' invalid')
  1528. else
  1529. begin
  1530. Comment(lvl,' ex: '+tostr(hp^.exact_count)+
  1531. ' eq: '+tostr(hp^.equal_count)+
  1532. ' l1: '+tostr(hp^.cl1_count)+
  1533. ' l2: '+tostr(hp^.cl2_count)+
  1534. ' l3: '+tostr(hp^.cl3_count)+
  1535. ' oper: '+tostr(hp^.coper_count)+
  1536. ' ord: '+realtostr(hp^.ordinal_distance));
  1537. { Print parameters in left-right order }
  1538. for i:=0 to hp^.data.paras.count-1 do
  1539. begin
  1540. currpara:=tparavarsym(hp^.data.paras[i]);
  1541. if (vo_is_hidden_para in currpara.varoptions) then
  1542. Comment(lvl,' - '+currpara.vartype.def.typename+' : '+EqualTypeName[currpara.eqval]);
  1543. end;
  1544. end;
  1545. hp:=hp^.next;
  1546. end;
  1547. end;
  1548. {$endif EXTDEBUG}
  1549. procedure tcallcandidates.get_information;
  1550. var
  1551. hp : pcandidate;
  1552. currpara : tparavarsym;
  1553. paraidx : integer;
  1554. currparanr : byte;
  1555. rfh,rth : bestreal;
  1556. def_from,
  1557. def_to : tdef;
  1558. currpt,
  1559. pt : tcallparanode;
  1560. eq : tequaltype;
  1561. convtype : tconverttype;
  1562. pdoper : tprocdef;
  1563. releasecurrpt : boolean;
  1564. cdoptions : tcompare_defs_options;
  1565. begin
  1566. cdoptions:=[cdo_check_operator];
  1567. if FAllowVariant then
  1568. include(cdoptions,cdo_allow_variant);
  1569. { process all procs }
  1570. hp:=FProcs;
  1571. while assigned(hp) do
  1572. begin
  1573. { We compare parameters in reverse order (right to left),
  1574. the firstpara is already pointing to the last parameter
  1575. were we need to start comparing }
  1576. currparanr:=FParalength;
  1577. paraidx:=hp^.firstparaidx;
  1578. while (paraidx>=0) and (vo_is_hidden_para in tparavarsym(hp^.data.paras[paraidx]).varoptions) do
  1579. dec(paraidx);
  1580. pt:=tcallparanode(FParaNode);
  1581. while assigned(pt) and (paraidx>=0) do
  1582. begin
  1583. currpara:=tparavarsym(hp^.data.paras[paraidx]);
  1584. { currpt can be changed from loadn to calln when a procvar
  1585. is passed. This is to prevent that the change is permanent }
  1586. currpt:=pt;
  1587. releasecurrpt:=false;
  1588. { retrieve current parameter definitions to compares }
  1589. eq:=te_incompatible;
  1590. def_from:=currpt.resulttype.def;
  1591. def_to:=currpara.vartype.def;
  1592. if not(assigned(def_from)) then
  1593. internalerror(200212091);
  1594. if not(
  1595. assigned(def_to) or
  1596. ((po_varargs in hp^.data.procoptions) and
  1597. (currparanr>hp^.data.minparacount))
  1598. ) then
  1599. internalerror(200212092);
  1600. { Convert tp procvars when not expecting a procvar }
  1601. if (def_to.deftype<>procvardef) and
  1602. (currpt.left.resulttype.def.deftype=procvardef) then
  1603. begin
  1604. releasecurrpt:=true;
  1605. currpt:=tcallparanode(pt.getcopy);
  1606. if maybe_call_procvar(currpt.left,true) then
  1607. begin
  1608. currpt.resulttype:=currpt.left.resulttype;
  1609. def_from:=currpt.left.resulttype.def;
  1610. end;
  1611. end;
  1612. { varargs are always equal, but not exact }
  1613. if (po_varargs in hp^.data.procoptions) and
  1614. (currparanr>hp^.data.minparacount) then
  1615. begin
  1616. eq:=te_equal;
  1617. end
  1618. else
  1619. { same definition -> exact }
  1620. if (def_from=def_to) then
  1621. begin
  1622. eq:=te_exact;
  1623. end
  1624. else
  1625. { for value and const parameters check if a integer is constant or
  1626. included in other integer -> equal and calc ordinal_distance }
  1627. if not(currpara.varspez in [vs_var,vs_out]) and
  1628. is_integer(def_from) and
  1629. is_integer(def_to) and
  1630. is_in_limit(def_from,def_to) then
  1631. begin
  1632. eq:=te_equal;
  1633. hp^.ordinal_distance:=hp^.ordinal_distance+
  1634. abs(bestreal(torddef(def_from).low)-bestreal(torddef(def_to).low));
  1635. if (torddef(def_to).typ=u64bit) then
  1636. rth:=bestreal(qword(torddef(def_to).high))
  1637. else
  1638. rth:=bestreal(torddef(def_to).high);
  1639. if (torddef(def_from).typ=u64bit) then
  1640. rfh:=bestreal(qword(torddef(def_from).high))
  1641. else
  1642. rfh:=bestreal(torddef(def_from).high);
  1643. hp^.ordinal_distance:=hp^.ordinal_distance+abs(rth-rfh);
  1644. { Give wrong sign a small penalty, this is need to get a diffrence
  1645. from word->[longword,longint] }
  1646. if is_signed(def_from)<>is_signed(def_to) then
  1647. hp^.ordinal_distance:=hp^.ordinal_distance+1.0;
  1648. end
  1649. else
  1650. { generic type comparision }
  1651. begin
  1652. eq:=compare_defs_ext(def_from,def_to,currpt.left.nodetype,convtype,pdoper,cdoptions);
  1653. { when the types are not equal we need to check
  1654. some special case for parameter passing }
  1655. if (eq<te_equal) then
  1656. begin
  1657. if currpara.varspez in [vs_var,vs_out] then
  1658. begin
  1659. { para requires an equal type so the previous found
  1660. match was not good enough, reset to incompatible }
  1661. eq:=te_incompatible;
  1662. { var_para_allowed will return te_equal and te_convert_l1 to
  1663. make a difference for best matching }
  1664. var_para_allowed(eq,currpt.resulttype.def,currpara.vartype.def)
  1665. end
  1666. else
  1667. para_allowed(eq,currpt,def_to);
  1668. end;
  1669. end;
  1670. { when a procvar was changed to a call an exact much is
  1671. downgraded to equal. This way an overload call with the
  1672. procvar is choosen. See tb0471 (PFV) }
  1673. if (pt<>currpt) and (eq=te_exact) then
  1674. eq:=te_equal;
  1675. { increase correct counter }
  1676. case eq of
  1677. te_exact :
  1678. inc(hp^.exact_count);
  1679. te_equal :
  1680. inc(hp^.equal_count);
  1681. te_convert_l1 :
  1682. inc(hp^.cl1_count);
  1683. te_convert_l2 :
  1684. inc(hp^.cl2_count);
  1685. te_convert_l3 :
  1686. inc(hp^.cl3_count);
  1687. te_convert_operator :
  1688. inc(hp^.coper_count);
  1689. te_incompatible :
  1690. hp^.invalid:=true;
  1691. else
  1692. internalerror(200212072);
  1693. end;
  1694. { stop checking when an incompatible parameter is found }
  1695. if hp^.invalid then
  1696. begin
  1697. { store the current parameter info for
  1698. a nice error message when no procedure is found }
  1699. hp^.wrongparaidx:=paraidx;
  1700. hp^.wrongparanr:=currparanr;
  1701. break;
  1702. end;
  1703. {$ifdef EXTDEBUG}
  1704. { store equal in node tree for dump }
  1705. currpara.eqval:=eq;
  1706. {$endif EXTDEBUG}
  1707. { maybe release temp currpt }
  1708. if releasecurrpt then
  1709. currpt.free;
  1710. { next parameter in the call tree }
  1711. pt:=tcallparanode(pt.right);
  1712. { next parameter for definition, only goto next para
  1713. if we're out of the varargs }
  1714. if not(po_varargs in hp^.data.procoptions) or
  1715. (currparanr<=hp^.data.maxparacount) then
  1716. begin
  1717. { Ignore vs_hidden parameters }
  1718. repeat
  1719. dec(paraidx);
  1720. until (paraidx<0) or not(vo_is_hidden_para in tparavarsym(hp^.data.paras[paraidx]).varoptions);
  1721. end;
  1722. dec(currparanr);
  1723. end;
  1724. if not(hp^.invalid) and
  1725. (assigned(pt) or (paraidx>=0) or (currparanr<>0)) then
  1726. internalerror(200212141);
  1727. { next candidate }
  1728. hp:=hp^.next;
  1729. end;
  1730. end;
  1731. function is_better_candidate(currpd,bestpd:pcandidate):integer;
  1732. var
  1733. res : integer;
  1734. begin
  1735. {
  1736. Return values:
  1737. > 0 when currpd is better than bestpd
  1738. < 0 when bestpd is better than currpd
  1739. = 0 when both are equal
  1740. To choose the best candidate we use the following order:
  1741. - Incompatible flag
  1742. - (Smaller) Number of convert operator parameters.
  1743. - (Smaller) Number of convertlevel 2 parameters.
  1744. - (Smaller) Number of convertlevel 1 parameters.
  1745. - (Bigger) Number of exact parameters.
  1746. - (Smaller) Number of equal parameters.
  1747. - (Smaller) Total of ordinal distance. For example, the distance of a word
  1748. to a byte is 65535-255=65280.
  1749. }
  1750. if bestpd^.invalid then
  1751. begin
  1752. if currpd^.invalid then
  1753. res:=0
  1754. else
  1755. res:=1;
  1756. end
  1757. else
  1758. if currpd^.invalid then
  1759. res:=-1
  1760. else
  1761. begin
  1762. { less operator parameters? }
  1763. res:=(bestpd^.coper_count-currpd^.coper_count);
  1764. if (res=0) then
  1765. begin
  1766. { less cl3 parameters? }
  1767. res:=(bestpd^.cl3_count-currpd^.cl3_count);
  1768. if (res=0) then
  1769. begin
  1770. { less cl2 parameters? }
  1771. res:=(bestpd^.cl2_count-currpd^.cl2_count);
  1772. if (res=0) then
  1773. begin
  1774. { less cl1 parameters? }
  1775. res:=(bestpd^.cl1_count-currpd^.cl1_count);
  1776. if (res=0) then
  1777. begin
  1778. { more exact parameters? }
  1779. res:=(currpd^.exact_count-bestpd^.exact_count);
  1780. if (res=0) then
  1781. begin
  1782. { less equal parameters? }
  1783. res:=(bestpd^.equal_count-currpd^.equal_count);
  1784. if (res=0) then
  1785. begin
  1786. { smaller ordinal distance? }
  1787. if (currpd^.ordinal_distance<bestpd^.ordinal_distance) then
  1788. res:=1
  1789. else
  1790. if (currpd^.ordinal_distance>bestpd^.ordinal_distance) then
  1791. res:=-1
  1792. else
  1793. res:=0;
  1794. end;
  1795. end;
  1796. end;
  1797. end;
  1798. end;
  1799. end;
  1800. end;
  1801. is_better_candidate:=res;
  1802. end;
  1803. function tcallcandidates.choose_best(var bestpd:tabstractprocdef):integer;
  1804. var
  1805. besthpstart,
  1806. hp : pcandidate;
  1807. cntpd,
  1808. res : integer;
  1809. begin
  1810. {
  1811. Returns the number of candidates left and the
  1812. first candidate is returned in pdbest
  1813. }
  1814. { Setup the first procdef as best, only count it as a result
  1815. when it is valid }
  1816. bestpd:=FProcs^.data;
  1817. if FProcs^.invalid then
  1818. cntpd:=0
  1819. else
  1820. cntpd:=1;
  1821. if assigned(FProcs^.next) then
  1822. begin
  1823. besthpstart:=FProcs;
  1824. hp:=FProcs^.next;
  1825. while assigned(hp) do
  1826. begin
  1827. res:=is_better_candidate(hp,besthpstart);
  1828. if (res>0) then
  1829. begin
  1830. { hp is better, flag all procs to be incompatible }
  1831. while (besthpstart<>hp) do
  1832. begin
  1833. besthpstart^.invalid:=true;
  1834. besthpstart:=besthpstart^.next;
  1835. end;
  1836. { besthpstart is already set to hp }
  1837. bestpd:=besthpstart^.data;
  1838. cntpd:=1;
  1839. end
  1840. else
  1841. if (res<0) then
  1842. begin
  1843. { besthpstart is better, flag current hp to be incompatible }
  1844. hp^.invalid:=true;
  1845. end
  1846. else
  1847. begin
  1848. { res=0, both are valid }
  1849. if not hp^.invalid then
  1850. inc(cntpd);
  1851. end;
  1852. hp:=hp^.next;
  1853. end;
  1854. end;
  1855. result:=cntpd;
  1856. end;
  1857. procedure tcallcandidates.find_wrong_para;
  1858. var
  1859. currparanr : smallint;
  1860. hp : pcandidate;
  1861. pt : tcallparanode;
  1862. wrongpara : tparavarsym;
  1863. begin
  1864. { Only process the first overloaded procdef }
  1865. hp:=FProcs;
  1866. { Find callparanode corresponding to the argument }
  1867. pt:=tcallparanode(FParanode);
  1868. currparanr:=FParalength;
  1869. while assigned(pt) and
  1870. (currparanr>hp^.wrongparanr) do
  1871. begin
  1872. pt:=tcallparanode(pt.right);
  1873. dec(currparanr);
  1874. end;
  1875. if (currparanr<>hp^.wrongparanr) or
  1876. not assigned(pt) then
  1877. internalerror(200212094);
  1878. { Show error message, when it was a var or out parameter
  1879. guess that it is a missing typeconv }
  1880. wrongpara:=tparavarsym(hp^.data.paras[hp^.wrongparaidx]);
  1881. if wrongpara.varspez in [vs_var,vs_out] then
  1882. begin
  1883. { Maybe passing the correct type but passing a const to var parameter }
  1884. if (compare_defs(pt.resulttype.def,wrongpara.vartype.def,pt.nodetype)<>te_incompatible) and
  1885. not valid_for_var(pt.left) then
  1886. CGMessagePos(pt.left.fileinfo,type_e_variable_id_expected)
  1887. else
  1888. CGMessagePos2(pt.left.fileinfo,parser_e_call_by_ref_without_typeconv,
  1889. FullTypeName(pt.left.resulttype.def,wrongpara.vartype.def),
  1890. FullTypeName(wrongpara.vartype.def,pt.left.resulttype.def))
  1891. end
  1892. else
  1893. CGMessagePos3(pt.left.fileinfo,type_e_wrong_parameter_type,tostr(hp^.wrongparanr),
  1894. FullTypeName(pt.left.resulttype.def,wrongpara.vartype.def),
  1895. FullTypeName(wrongpara.vartype.def,pt.left.resulttype.def));
  1896. end;
  1897. end.
  1898. {
  1899. $Log$
  1900. Revision 1.120 2005-03-25 22:20:18 peter
  1901. * add hint when passing an uninitialized variable to a var parameter
  1902. Revision 1.119 2005/03/10 00:15:20 peter
  1903. don't allow overloading orddef,enumdef.floatdef for unary operators
  1904. Revision 1.118 2005/02/20 13:12:22 peter
  1905. * allow assignment to elements of constant dyn array in delphi mode
  1906. Revision 1.117 2005/02/14 17:13:06 peter
  1907. * truncate log
  1908. Revision 1.116 2005/02/14 16:45:00 peter
  1909. * allow more operations on integers with a typecast to pointer
  1910. Revision 1.115 2005/02/13 20:33:57 peter
  1911. * allow nil^ passed to var parameter
  1912. Revision 1.114 2005/02/02 22:16:39 florian
  1913. * delphi assumes dyn. array access make expressions l-values because it's internally a pointer
  1914. Revision 1.113 2005/02/01 22:50:50 florian
  1915. * inherited; works now in delphi mode for private methods; looks like a delphi bug
  1916. Revision 1.112 2005/01/25 18:49:45 peter
  1917. * fix overload choosing with an qword overload
  1918. * allow to get the address of string temps in delphi mode
  1919. Revision 1.111 2005/01/19 23:23:12 florian
  1920. * taking the address of a resourcestring is allowed now
  1921. Revision 1.110 2005/01/19 22:19:41 peter
  1922. * unit mapping rewrite
  1923. * new derefmap added
  1924. Revision 1.109 2005/01/19 20:53:27 florian
  1925. * tmypointer(12435)^ is an l-value
  1926. Revision 1.108 2005/01/10 22:10:26 peter
  1927. * widestring patches from Alexey Barkovoy
  1928. Revision 1.107 2005/01/07 16:22:47 peter
  1929. * handle string-open array of (wide)char without variants
  1930. }