htypechk.pas 74 KB

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