htypechk.pas 78 KB

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