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