htypechk.pas 99 KB

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