htypechk.pas 81 KB

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