htypechk.pas 97 KB

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