htypechk.pas 76 KB

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