htypechk.pas 74 KB

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