htypechk.pas 98 KB

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