htypechk.pas 98 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588
  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 if valid_property in opts then
  872. errmsg:=type_e_argument_cant_be_assigned
  873. else
  874. errmsg:=type_e_no_addr_of_constant;
  875. result:=false;
  876. gotsubscript:=false;
  877. gotvec:=false;
  878. gotderef:=false;
  879. gotrecord:=false;
  880. gotclass:=false;
  881. gotpointer:=false;
  882. gotdynarray:=false;
  883. gotstring:=false;
  884. hp:=p;
  885. if not(valid_void in opts) and
  886. is_void(hp.resultdef) then
  887. begin
  888. if report_errors then
  889. CGMessagePos(hp.fileinfo,errmsg);
  890. exit;
  891. end;
  892. while assigned(hp) do
  893. begin
  894. { property allowed? calln has a property check itself }
  895. if (nf_isproperty in hp.flags) then
  896. begin
  897. { check return type }
  898. case hp.resultdef.typ of
  899. pointerdef :
  900. gotpointer:=true;
  901. objectdef :
  902. gotclass:=is_class_or_interface(hp.resultdef);
  903. recorddef :
  904. gotrecord:=true;
  905. classrefdef :
  906. gotclass:=true;
  907. stringdef :
  908. gotstring:=true;
  909. end;
  910. if (valid_property in opts) then
  911. begin
  912. { don't allow writing to calls that will create
  913. temps like calls that return a structure and we
  914. are assigning to a member }
  915. if (valid_const in opts) or
  916. not(
  917. (gotsubscript and gotrecord) or
  918. (gotstring and gotvec)
  919. ) then
  920. result:=true
  921. else
  922. if report_errors then
  923. CGMessagePos(hp.fileinfo,errmsg);
  924. end
  925. else
  926. begin
  927. { 1. if it returns a pointer and we've found a deref,
  928. 2. if it returns a class or record and a subscription or with is found
  929. 3. if the address is needed of a field (subscriptn, vecn) }
  930. if (gotpointer and gotderef) or
  931. (gotstring and gotvec) or
  932. (
  933. (gotclass or gotrecord) and
  934. (gotsubscript)
  935. ) or
  936. (
  937. (gotvec and gotdynarray)
  938. ) or
  939. (
  940. (Valid_Addr in opts) and
  941. (hp.nodetype in [subscriptn,vecn])
  942. ) then
  943. result:=true
  944. else
  945. if report_errors then
  946. CGMessagePos(hp.fileinfo,errmsg);
  947. end;
  948. exit;
  949. end;
  950. if (Valid_Const in opts) and is_constnode(hp) then
  951. begin
  952. result:=true;
  953. exit;
  954. end;
  955. case hp.nodetype of
  956. temprefn :
  957. begin
  958. valid_for_assign := true;
  959. exit;
  960. end;
  961. derefn :
  962. begin
  963. gotderef:=true;
  964. hp:=tderefnode(hp).left;
  965. end;
  966. typeconvn :
  967. begin
  968. { typecast sizes must match, exceptions:
  969. - implicit typecast made by absolute
  970. - from formaldef
  971. - from void
  972. - from/to open array
  973. - typecast from pointer to array }
  974. fromdef:=ttypeconvnode(hp).left.resultdef;
  975. todef:=hp.resultdef;
  976. if not((nf_absolute in ttypeconvnode(hp).flags) or
  977. (fromdef.typ=formaldef) or
  978. is_void(fromdef) or
  979. is_open_array(fromdef) or
  980. is_open_array(todef) or
  981. ((fromdef.typ=pointerdef) and (todef.typ=arraydef)) or
  982. ((fromdef.typ = objectdef) and (todef.typ = objectdef) and
  983. (tobjectdef(fromdef).is_related(tobjectdef(todef))))) and
  984. (fromdef.size<>todef.size) then
  985. begin
  986. { in TP it is allowed to typecast to smaller types. But the variable can't
  987. be in a register }
  988. if (m_tp7 in current_settings.modeswitches) or
  989. (todef.size<fromdef.size) then
  990. make_not_regable(hp,vr_addr)
  991. else
  992. if report_errors then
  993. CGMessagePos2(hp.fileinfo,type_e_typecast_wrong_size_for_assignment,tostr(fromdef.size),tostr(todef.size));
  994. end;
  995. { don't allow assignments to typeconvs that need special code }
  996. if not(gotsubscript or gotvec or gotderef) and
  997. not(ttypeconvnode(hp).assign_allowed) then
  998. begin
  999. if report_errors then
  1000. CGMessagePos(hp.fileinfo,errmsg);
  1001. exit;
  1002. end;
  1003. case hp.resultdef.typ of
  1004. pointerdef :
  1005. gotpointer:=true;
  1006. objectdef :
  1007. gotclass:=is_class_or_interface(hp.resultdef);
  1008. classrefdef :
  1009. gotclass:=true;
  1010. arraydef :
  1011. begin
  1012. { pointer -> array conversion is done then we need to see it
  1013. as a deref, because a ^ is then not required anymore }
  1014. if (ttypeconvnode(hp).left.resultdef.typ=pointerdef) then
  1015. gotderef:=true;
  1016. end;
  1017. end;
  1018. hp:=ttypeconvnode(hp).left;
  1019. end;
  1020. vecn :
  1021. begin
  1022. if { only check for first (= outermost) vec node }
  1023. not gotvec and
  1024. not(valid_packed in opts) and
  1025. (tvecnode(hp).left.resultdef.typ = arraydef) and
  1026. (ado_IsBitPacked in tarraydef(tvecnode(hp).left.resultdef).arrayoptions) and
  1027. (tarraydef(tvecnode(hp).left.resultdef).elepackedbitsize mod 8 <> 0) then
  1028. begin
  1029. if report_errors then
  1030. if (valid_property in opts) then
  1031. CGMessagePos(hp.fileinfo,parser_e_packed_element_no_loop)
  1032. else
  1033. CGMessagePos(hp.fileinfo,parser_e_packed_element_no_var_addr);
  1034. exit;
  1035. end;
  1036. gotvec:=true;
  1037. { accesses to dyn. arrays override read only access in delphi }
  1038. if (m_delphi in current_settings.modeswitches) and is_dynamic_array(tunarynode(hp).left.resultdef) then
  1039. gotdynarray:=true;
  1040. hp:=tunarynode(hp).left;
  1041. end;
  1042. blockn :
  1043. begin
  1044. hp2:=tblocknode(hp).statements;
  1045. if assigned(hp2) then
  1046. begin
  1047. if hp2.nodetype<>statementn then
  1048. internalerror(2006110801);
  1049. while assigned(tstatementnode(hp2).next) do
  1050. hp2:=tstatementnode(hp2).next;
  1051. hp:=tstatementnode(hp2).statement;
  1052. end
  1053. else
  1054. begin
  1055. if report_errors then
  1056. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  1057. exit;
  1058. end;
  1059. end;
  1060. asn :
  1061. begin
  1062. { asn can't be assigned directly, it returns the value in a register instead
  1063. of reference. }
  1064. if not(gotsubscript or gotderef or gotvec) then
  1065. begin
  1066. if report_errors then
  1067. CGMessagePos(hp.fileinfo,errmsg);
  1068. exit;
  1069. end;
  1070. hp:=tunarynode(hp).left;
  1071. end;
  1072. subscriptn :
  1073. begin
  1074. { only check first (= outermost) subscriptn }
  1075. if not gotsubscript and
  1076. not(valid_packed in opts) and
  1077. is_packed_record_or_object(tsubscriptnode(hp).left.resultdef) then
  1078. begin
  1079. if report_errors then
  1080. if (valid_property in opts) then
  1081. CGMessagePos(hp.fileinfo,parser_e_packed_element_no_loop)
  1082. else
  1083. CGMessagePos(hp.fileinfo,parser_e_packed_element_no_var_addr);
  1084. exit;
  1085. end;
  1086. gotsubscript:=true;
  1087. { loop counter? }
  1088. if not(Valid_Const in opts) and
  1089. (vo_is_loop_counter in tsubscriptnode(hp).vs.varoptions) then
  1090. begin
  1091. if report_errors then
  1092. CGMessage1(parser_e_illegal_assignment_to_count_var,tsubscriptnode(hp).vs.realname)
  1093. else
  1094. exit;
  1095. end;
  1096. { a class/interface access is an implicit }
  1097. { dereferencing }
  1098. hp:=tsubscriptnode(hp).left;
  1099. if is_class_or_interface(hp.resultdef) then
  1100. gotderef:=true;
  1101. end;
  1102. muln,
  1103. divn,
  1104. andn,
  1105. xorn,
  1106. orn,
  1107. notn,
  1108. subn,
  1109. addn :
  1110. begin
  1111. { Allow operators on a pointer, or an integer
  1112. and a pointer typecast and deref has been found }
  1113. if ((hp.resultdef.typ=pointerdef) or
  1114. (is_integer(hp.resultdef) and gotpointer)) and
  1115. gotderef then
  1116. result:=true
  1117. else
  1118. { Temp strings are stored in memory, for compatibility with
  1119. delphi only }
  1120. if (m_delphi in current_settings.modeswitches) and
  1121. ((valid_addr in opts) or
  1122. (valid_const in opts)) and
  1123. (hp.resultdef.typ=stringdef) then
  1124. result:=true
  1125. else
  1126. if report_errors then
  1127. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  1128. exit;
  1129. end;
  1130. niln,
  1131. pointerconstn :
  1132. begin
  1133. { to support e.g. @tmypointer(0)^.data; see tests/tbs/tb0481 }
  1134. if gotderef then
  1135. result:=true
  1136. else
  1137. if report_errors then
  1138. CGMessagePos(hp.fileinfo,type_e_no_assign_to_addr);
  1139. exit;
  1140. end;
  1141. addrn :
  1142. begin
  1143. if gotderef then
  1144. result:=true
  1145. else
  1146. if report_errors then
  1147. CGMessagePos(hp.fileinfo,type_e_no_assign_to_addr);
  1148. exit;
  1149. end;
  1150. calln :
  1151. begin
  1152. { check return type }
  1153. case hp.resultdef.typ of
  1154. arraydef :
  1155. begin
  1156. { dynamic arrays are allowed when there is also a
  1157. vec node }
  1158. if is_dynamic_array(hp.resultdef) and
  1159. gotvec then
  1160. begin
  1161. gotderef:=true;
  1162. gotpointer:=true;
  1163. end;
  1164. end;
  1165. pointerdef :
  1166. gotpointer:=true;
  1167. objectdef :
  1168. gotclass:=is_class_or_interface(hp.resultdef);
  1169. recorddef, { handle record like class it needs a subscription }
  1170. classrefdef :
  1171. gotclass:=true;
  1172. stringdef :
  1173. gotstring:=true;
  1174. end;
  1175. { 1. if it returns a pointer and we've found a deref,
  1176. 2. if it returns a class or record and a subscription or with is found
  1177. 3. string is returned }
  1178. if (gotstring and gotvec) or
  1179. (gotpointer and gotderef) or
  1180. (gotclass and gotsubscript) then
  1181. result:=true
  1182. else
  1183. { Temp strings are stored in memory, for compatibility with
  1184. delphi only }
  1185. if (m_delphi in current_settings.modeswitches) and
  1186. (valid_addr in opts) and
  1187. (hp.resultdef.typ=stringdef) then
  1188. result:=true
  1189. else
  1190. if ([valid_const,valid_addr] * opts = [valid_const]) then
  1191. result:=true
  1192. else
  1193. if report_errors then
  1194. CGMessagePos(hp.fileinfo,errmsg);
  1195. exit;
  1196. end;
  1197. inlinen :
  1198. begin
  1199. if ((valid_const in opts) and
  1200. (tinlinenode(hp).inlinenumber in [in_typeof_x]))
  1201. {$ifdef SUPPORT_UNALIGNED}
  1202. or (tinlinenode(hp).inlinenumber in [in_unaligned_x])
  1203. {$endif SUPPORT_UNALIGNED}
  1204. then
  1205. result:=true
  1206. else
  1207. if report_errors then
  1208. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  1209. exit;
  1210. end;
  1211. dataconstn:
  1212. begin
  1213. { only created internally, so no additional checks necessary }
  1214. result:=true;
  1215. exit;
  1216. end;
  1217. loadn :
  1218. begin
  1219. case tloadnode(hp).symtableentry.typ of
  1220. absolutevarsym,
  1221. staticvarsym,
  1222. localvarsym,
  1223. paravarsym :
  1224. begin
  1225. { loop counter? }
  1226. if not(Valid_Const in opts) and
  1227. not gotderef and
  1228. (vo_is_loop_counter in tabstractvarsym(tloadnode(hp).symtableentry).varoptions) then
  1229. if report_errors then
  1230. CGMessage1(parser_e_illegal_assignment_to_count_var,tloadnode(hp).symtableentry.realname)
  1231. else
  1232. exit;
  1233. { read-only variable? }
  1234. if (tabstractvarsym(tloadnode(hp).symtableentry).varspez=vs_const) then
  1235. begin
  1236. { allow p^:= constructions with p is const parameter }
  1237. if gotderef or gotdynarray or (Valid_Const in opts) or
  1238. (nf_isinternal_ignoreconst in tloadnode(hp).flags) then
  1239. result:=true
  1240. else
  1241. if report_errors then
  1242. CGMessagePos(tloadnode(hp).fileinfo,type_e_no_assign_to_const);
  1243. exit;
  1244. end;
  1245. result:=true;
  1246. exit;
  1247. end;
  1248. procsym :
  1249. begin
  1250. if (Valid_Const in opts) then
  1251. result:=true
  1252. else
  1253. if report_errors then
  1254. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  1255. exit;
  1256. end;
  1257. labelsym :
  1258. begin
  1259. if (Valid_Addr in opts) then
  1260. result:=true
  1261. else
  1262. if report_errors then
  1263. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  1264. exit;
  1265. end;
  1266. constsym:
  1267. begin
  1268. if (tconstsym(tloadnode(hp).symtableentry).consttyp=constresourcestring) and
  1269. (valid_addr in opts) then
  1270. result:=true
  1271. else
  1272. if report_errors then
  1273. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  1274. exit;
  1275. end;
  1276. else
  1277. begin
  1278. if report_errors then
  1279. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  1280. exit;
  1281. end;
  1282. end;
  1283. end;
  1284. else
  1285. begin
  1286. if report_errors then
  1287. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  1288. exit;
  1289. end;
  1290. end;
  1291. end;
  1292. end;
  1293. function valid_for_var(p:tnode; report_errors: boolean):boolean;
  1294. begin
  1295. valid_for_var:=valid_for_assign(p,[],report_errors);
  1296. end;
  1297. function valid_for_formal_var(p : tnode; report_errors: boolean) : boolean;
  1298. begin
  1299. valid_for_formal_var:=valid_for_assign(p,[valid_void],report_errors);
  1300. end;
  1301. function valid_for_formal_const(p : tnode; report_errors: boolean) : boolean;
  1302. begin
  1303. valid_for_formal_const:=(p.resultdef.typ=formaldef) or
  1304. valid_for_assign(p,[valid_void,valid_const,valid_property],report_errors);
  1305. end;
  1306. function valid_for_assignment(p:tnode; report_errors: boolean):boolean;
  1307. begin
  1308. valid_for_assignment:=valid_for_assign(p,[valid_property,valid_packed],report_errors);
  1309. end;
  1310. function valid_for_loopvar(p:tnode; report_errors: boolean):boolean;
  1311. begin
  1312. valid_for_loopvar:=valid_for_assign(p,[valid_property],report_errors);
  1313. end;
  1314. function valid_for_addr(p : tnode; report_errors: boolean) : boolean;
  1315. begin
  1316. result:=valid_for_assign(p,[valid_const,valid_addr,valid_void],report_errors);
  1317. end;
  1318. procedure var_para_allowed(var eq:tequaltype;def_from,def_to:Tdef);
  1319. begin
  1320. { Note: eq must be already valid, it will only be updated! }
  1321. case def_to.typ of
  1322. formaldef :
  1323. begin
  1324. { all types can be passed to a formaldef,
  1325. but it is not the prefered way }
  1326. eq:=te_convert_l2;
  1327. end;
  1328. orddef :
  1329. begin
  1330. { allows conversion from word to integer and
  1331. byte to shortint, but only for TP7 compatibility }
  1332. if (m_tp7 in current_settings.modeswitches) and
  1333. (def_from.typ=orddef) and
  1334. (def_from.size=def_to.size) then
  1335. eq:=te_convert_l1;
  1336. end;
  1337. arraydef :
  1338. begin
  1339. if is_open_array(def_to) then
  1340. begin
  1341. if is_dynamic_array(def_from) and
  1342. equal_defs(tarraydef(def_from).elementdef,tarraydef(def_to).elementdef) then
  1343. eq:=te_convert_l2
  1344. else
  1345. if equal_defs(def_from,tarraydef(def_to).elementdef) then
  1346. eq:=te_convert_l2;
  1347. end;
  1348. end;
  1349. pointerdef :
  1350. begin
  1351. { an implicit pointer conversion is allowed }
  1352. if (def_from.typ=pointerdef) then
  1353. eq:=te_convert_l1;
  1354. end;
  1355. stringdef :
  1356. begin
  1357. { all shortstrings are allowed, size is not important }
  1358. if is_shortstring(def_from) and
  1359. is_shortstring(def_to) then
  1360. eq:=te_equal;
  1361. end;
  1362. objectdef :
  1363. begin
  1364. { child objects can be also passed }
  1365. { in non-delphi mode, otherwise }
  1366. { they must match exactly, except }
  1367. { if they are objects }
  1368. if (def_from.typ=objectdef) and
  1369. (
  1370. not(m_delphi in current_settings.modeswitches) or
  1371. (
  1372. (tobjectdef(def_from).objecttype=odt_object) and
  1373. (tobjectdef(def_to).objecttype=odt_object)
  1374. )
  1375. ) and
  1376. (tobjectdef(def_from).is_related(tobjectdef(def_to))) then
  1377. eq:=te_convert_l1;
  1378. end;
  1379. filedef :
  1380. begin
  1381. { an implicit file conversion is also allowed }
  1382. { from a typed file to an untyped one }
  1383. if (def_from.typ=filedef) and
  1384. (tfiledef(def_from).filetyp = ft_typed) and
  1385. (tfiledef(def_to).filetyp = ft_untyped) then
  1386. eq:=te_convert_l1;
  1387. end;
  1388. end;
  1389. end;
  1390. procedure para_allowed(var eq:tequaltype;p:tcallparanode;def_to:tdef);
  1391. begin
  1392. { Note: eq must be already valid, it will only be updated! }
  1393. case def_to.typ of
  1394. formaldef :
  1395. begin
  1396. { all types can be passed to a formaldef }
  1397. eq:=te_equal;
  1398. end;
  1399. stringdef :
  1400. begin
  1401. { to support ansi/long/wide strings in a proper way }
  1402. { string and string[10] are assumed as equal }
  1403. { when searching the correct overloaded procedure }
  1404. if (p.resultdef.typ=stringdef) and
  1405. (tstringdef(def_to).stringtype=tstringdef(p.resultdef).stringtype) then
  1406. eq:=te_equal
  1407. else
  1408. { Passing a constant char to ansistring or shortstring or
  1409. a widechar to widestring then handle it as equal. }
  1410. if (p.left.nodetype=ordconstn) and
  1411. (
  1412. is_char(p.resultdef) and
  1413. (is_shortstring(def_to) or is_ansistring(def_to))
  1414. ) or
  1415. (
  1416. is_widechar(p.resultdef) and
  1417. is_widestring(def_to)
  1418. ) then
  1419. eq:=te_equal
  1420. end;
  1421. setdef :
  1422. begin
  1423. { set can also be a not yet converted array constructor }
  1424. if (p.resultdef.typ=arraydef) and
  1425. is_array_constructor(p.resultdef) and
  1426. not is_variant_array(p.resultdef) then
  1427. eq:=te_equal;
  1428. end;
  1429. procvardef :
  1430. begin
  1431. { in tp7 mode proc -> procvar is allowed }
  1432. if ((m_tp_procvar in current_settings.modeswitches) or
  1433. (m_mac_procvar in current_settings.modeswitches)) and
  1434. (p.left.nodetype=calln) and
  1435. (proc_to_procvar_equal(tprocdef(tcallnode(p.left).procdefinition),tprocvardef(def_to))>=te_equal) then
  1436. eq:=te_equal
  1437. else
  1438. if (m_mac_procvar in current_settings.modeswitches) and
  1439. is_procvar_load(p.left) then
  1440. eq:=te_convert_l2;
  1441. end;
  1442. end;
  1443. end;
  1444. function allowenumop(nt:tnodetype):boolean;
  1445. begin
  1446. result:=(nt in [equaln,unequaln,ltn,lten,gtn,gten]) or
  1447. ((cs_allow_enum_calc in current_settings.localswitches) and
  1448. (nt in [addn,subn]));
  1449. end;
  1450. {****************************************************************************
  1451. TCallCandidates
  1452. ****************************************************************************}
  1453. constructor tcallcandidates.create(sym:tprocsym;st:TSymtable;ppn:tnode;isprop,ignorevis : boolean);
  1454. var
  1455. j : integer;
  1456. pd : tprocdef;
  1457. hp : pcandidate;
  1458. found,
  1459. has_overload_directive : boolean;
  1460. topclassh : tobjectdef;
  1461. srsymtable : TSymtable;
  1462. srprocsym : tprocsym;
  1463. pt : tcallparanode;
  1464. checkstack : psymtablestackitem;
  1465. hashedid : THashedIDString;
  1466. begin
  1467. if not assigned(sym) then
  1468. internalerror(200411015);
  1469. FProcSym:=sym;
  1470. FProcs:=nil;
  1471. FProccnt:=0;
  1472. FProcvisiblecnt:=0;
  1473. FParanode:=ppn;
  1474. FAllowVariant:=true;
  1475. { determine length of parameter list }
  1476. pt:=tcallparanode(ppn);
  1477. FParalength:=0;
  1478. while assigned(pt) do
  1479. begin
  1480. inc(FParalength);
  1481. pt:=tcallparanode(pt.right);
  1482. end;
  1483. { when the definition has overload directive set, we search for
  1484. overloaded definitions in the class, this only needs to be done once
  1485. for class entries as the tree keeps always the same }
  1486. if (not sym.overloadchecked) and
  1487. (sym.owner.symtabletype=ObjectSymtable) and
  1488. (po_overload in tprocdef(sym.ProcdefList[0]).procoptions) then
  1489. search_class_overloads(sym);
  1490. { when the class passed is defined in this unit we
  1491. need to use the scope of that class. This is a trick
  1492. that can be used to access protected members in other
  1493. units. At least kylix supports it this way (PFV) }
  1494. if assigned(st) and
  1495. (
  1496. (st.symtabletype=ObjectSymtable) or
  1497. ((st.symtabletype=withsymtable) and
  1498. (st.defowner.typ=objectdef))
  1499. ) and
  1500. (st.defowner.owner.symtabletype in [globalsymtable,staticsymtable]) and
  1501. st.defowner.owner.iscurrentunit then
  1502. topclassh:=tobjectdef(st.defowner)
  1503. else
  1504. begin
  1505. if assigned(current_procinfo) then
  1506. topclassh:=current_procinfo.procdef._class
  1507. else
  1508. topclassh:=nil;
  1509. end;
  1510. { link all procedures which have the same # of parameters }
  1511. for j:=0 to sym.ProcdefList.Count-1 do
  1512. begin
  1513. pd:=tprocdef(sym.ProcdefList[j]);
  1514. { Is the procdef visible? This needs to be checked on
  1515. procdef level since a symbol can contain both private and
  1516. public declarations. But the check should not be done
  1517. when the callnode is generated by a property
  1518. inherited overrides invisible anonymous inherited (FK) }
  1519. if isprop or ignorevis or
  1520. (pd.owner.symtabletype<>ObjectSymtable) or
  1521. pd.is_visible_for_object(topclassh,nil) then
  1522. begin
  1523. { we have at least one procedure that is visible }
  1524. inc(FProcvisiblecnt);
  1525. { only when the # of parameter are supported by the
  1526. procedure }
  1527. if (FParalength>=pd.minparacount) and
  1528. ((po_varargs in pd.procoptions) or { varargs }
  1529. (FParalength<=pd.maxparacount)) then
  1530. proc_add(pd);
  1531. end;
  1532. end;
  1533. { remember if the procedure is declared with the overload directive,
  1534. it's information is still needed also after all procs are removed }
  1535. has_overload_directive:=(po_overload in tprocdef(sym.ProcdefList[0]).procoptions);
  1536. { when the definition has overload directive set, we search for
  1537. overloaded definitions in the symtablestack. The found
  1538. entries are only added to the procs list and not the procsym, because
  1539. the list can change in every situation }
  1540. if has_overload_directive and
  1541. (sym.owner.symtabletype<>ObjectSymtable) then
  1542. begin
  1543. srsymtable:=sym.owner;
  1544. checkstack:=symtablestack.stack;
  1545. while assigned(checkstack) and
  1546. (checkstack^.symtable<>srsymtable) do
  1547. checkstack:=checkstack^.next;
  1548. { we've already processed the current symtable, start with
  1549. the next symtable in the stack }
  1550. if assigned(checkstack) then
  1551. checkstack:=checkstack^.next;
  1552. hashedid.id:=sym.name;
  1553. while assigned(checkstack) do
  1554. begin
  1555. srsymtable:=checkstack^.symtable;
  1556. if srsymtable.symtabletype in [localsymtable,staticsymtable,globalsymtable] then
  1557. begin
  1558. srprocsym:=tprocsym(srsymtable.FindWithHash(hashedid));
  1559. if assigned(srprocsym) and
  1560. (srprocsym.typ=procsym) then
  1561. begin
  1562. { if this visible procedure doesn't have overload we can stop
  1563. searching }
  1564. if not(po_overload in tprocdef(srprocsym.ProcdefList[0]).procoptions) and
  1565. tprocdef(srprocsym.ProcdefList[0]).is_visible_for_object(topclassh,nil) then
  1566. break;
  1567. { process all overloaded definitions }
  1568. for j:=0 to srprocsym.ProcdefList.Count-1 do
  1569. begin
  1570. pd:=tprocdef(srprocsym.ProcdefList[j]);
  1571. { only visible procedures need to be added }
  1572. if pd.is_visible_for_object(topclassh,nil) then
  1573. begin
  1574. { only when the # of parameter are supported by the
  1575. procedure }
  1576. if (FParalength>=pd.minparacount) and
  1577. ((po_varargs in pd.procoptions) or { varargs }
  1578. (FParalength<=pd.maxparacount)) then
  1579. begin
  1580. found:=false;
  1581. hp:=FProcs;
  1582. while assigned(hp) do
  1583. begin
  1584. { Only compare visible parameters for the user }
  1585. if compare_paras(hp^.data.paras,pd.paras,cp_value_equal_const,[cpo_ignorehidden])>=te_equal then
  1586. begin
  1587. found:=true;
  1588. break;
  1589. end;
  1590. hp:=hp^.next;
  1591. end;
  1592. if not found then
  1593. proc_add(pd);
  1594. end;
  1595. end;
  1596. end;
  1597. end;
  1598. end;
  1599. checkstack:=checkstack^.next;
  1600. end;
  1601. end;
  1602. end;
  1603. constructor tcallcandidates.create_operator(op:ttoken;ppn:tnode);
  1604. var
  1605. j : integer;
  1606. pd : tprocdef;
  1607. hp : pcandidate;
  1608. found : boolean;
  1609. srsymtable : TSymtable;
  1610. srprocsym : tprocsym;
  1611. pt : tcallparanode;
  1612. checkstack : psymtablestackitem;
  1613. hashedid : THashedIDString;
  1614. begin
  1615. FProcSym:=nil;
  1616. FProcs:=nil;
  1617. FProccnt:=0;
  1618. FProcvisiblecnt:=0;
  1619. FParanode:=ppn;
  1620. FAllowVariant:=false;
  1621. { determine length of parameter list }
  1622. pt:=tcallparanode(ppn);
  1623. FParalength:=0;
  1624. while assigned(pt) do
  1625. begin
  1626. if pt.resultdef.typ=variantdef then
  1627. FAllowVariant:=true;
  1628. inc(FParalength);
  1629. pt:=tcallparanode(pt.right);
  1630. end;
  1631. { we search all overloaded operator definitions in the symtablestack. The found
  1632. entries are only added to the procs list and not the procsym, because
  1633. the list can change in every situation }
  1634. hashedid.id:=overloaded_names[op];
  1635. checkstack:=symtablestack.stack;
  1636. while assigned(checkstack) do
  1637. begin
  1638. srsymtable:=checkstack^.symtable;
  1639. if srsymtable.symtabletype in [localsymtable,staticsymtable,globalsymtable] then
  1640. begin
  1641. srprocsym:=tprocsym(srsymtable.FindWithHash(hashedid));
  1642. if assigned(srprocsym) and
  1643. (srprocsym.typ=procsym) then
  1644. begin
  1645. { Store first procsym found }
  1646. if not assigned(FProcsym) then
  1647. FProcsym:=srprocsym;
  1648. { process all overloaded definitions }
  1649. for j:=0 to srprocsym.ProcdefList.Count-1 do
  1650. begin
  1651. pd:=tprocdef(srprocsym.ProcdefList[j]);
  1652. { only when the # of parameter are supported by the
  1653. procedure }
  1654. if (FParalength>=pd.minparacount) and
  1655. (FParalength<=pd.maxparacount) then
  1656. begin
  1657. found:=false;
  1658. hp:=FProcs;
  1659. while assigned(hp) do
  1660. begin
  1661. { Only compare visible parameters for the user }
  1662. if compare_paras(hp^.data.paras,pd.paras,cp_value_equal_const,[cpo_ignorehidden])>=te_equal then
  1663. begin
  1664. found:=true;
  1665. break;
  1666. end;
  1667. hp:=hp^.next;
  1668. end;
  1669. if not found then
  1670. proc_add(pd);
  1671. end;
  1672. end;
  1673. end;
  1674. end;
  1675. checkstack:=checkstack^.next;
  1676. end;
  1677. end;
  1678. destructor tcallcandidates.destroy;
  1679. var
  1680. hpnext,
  1681. hp : pcandidate;
  1682. begin
  1683. hp:=FProcs;
  1684. while assigned(hp) do
  1685. begin
  1686. hpnext:=hp^.next;
  1687. dispose(hp);
  1688. hp:=hpnext;
  1689. end;
  1690. end;
  1691. function tcallcandidates.proc_add(pd:tprocdef):pcandidate;
  1692. var
  1693. defaultparacnt : integer;
  1694. begin
  1695. { generate new candidate entry }
  1696. new(result);
  1697. fillchar(result^,sizeof(tcandidate),0);
  1698. result^.data:=pd;
  1699. result^.next:=FProcs;
  1700. FProcs:=result;
  1701. inc(FProccnt);
  1702. { Find last parameter, skip all default parameters
  1703. that are not passed. Ignore this skipping for varargs }
  1704. result^.firstparaidx:=pd.paras.count-1;
  1705. if not(po_varargs in pd.procoptions) then
  1706. begin
  1707. { ignore hidden parameters }
  1708. while (result^.firstparaidx>=0) and (vo_is_hidden_para in tparavarsym(pd.paras[result^.firstparaidx]).varoptions) do
  1709. dec(result^.firstparaidx);
  1710. defaultparacnt:=pd.maxparacount-FParalength;
  1711. if defaultparacnt>0 then
  1712. begin
  1713. if defaultparacnt>result^.firstparaidx+1 then
  1714. internalerror(200401141);
  1715. dec(result^.firstparaidx,defaultparacnt);
  1716. end;
  1717. end;
  1718. end;
  1719. procedure tcallcandidates.list(all:boolean);
  1720. var
  1721. hp : pcandidate;
  1722. begin
  1723. hp:=FProcs;
  1724. while assigned(hp) do
  1725. begin
  1726. if all or
  1727. (not hp^.invalid) then
  1728. MessagePos1(hp^.data.fileinfo,sym_h_param_list,hp^.data.fullprocname(false));
  1729. hp:=hp^.next;
  1730. end;
  1731. end;
  1732. {$ifdef EXTDEBUG}
  1733. procedure tcallcandidates.dump_info(lvl:longint);
  1734. function ParaTreeStr(p:tcallparanode):string;
  1735. begin
  1736. result:='';
  1737. while assigned(p) do
  1738. begin
  1739. if result<>'' then
  1740. result:=','+result;
  1741. result:=p.resultdef.typename+result;
  1742. p:=tcallparanode(p.right);
  1743. end;
  1744. end;
  1745. var
  1746. hp : pcandidate;
  1747. i : integer;
  1748. currpara : tparavarsym;
  1749. begin
  1750. if not CheckVerbosity(lvl) then
  1751. exit;
  1752. Comment(lvl+V_LineInfo,'Overloaded callnode: '+FProcSym.name+'('+ParaTreeStr(tcallparanode(FParaNode))+')');
  1753. hp:=FProcs;
  1754. while assigned(hp) do
  1755. begin
  1756. Comment(lvl,' '+hp^.data.fullprocname(false));
  1757. if (hp^.invalid) then
  1758. Comment(lvl,' invalid')
  1759. else
  1760. begin
  1761. Comment(lvl,' ex: '+tostr(hp^.exact_count)+
  1762. ' eq: '+tostr(hp^.equal_count)+
  1763. ' l1: '+tostr(hp^.cl1_count)+
  1764. ' l2: '+tostr(hp^.cl2_count)+
  1765. ' l3: '+tostr(hp^.cl3_count)+
  1766. ' oper: '+tostr(hp^.coper_count)+
  1767. ' ord: '+realtostr(hp^.ordinal_distance));
  1768. { Print parameters in left-right order }
  1769. for i:=0 to hp^.data.paras.count-1 do
  1770. begin
  1771. currpara:=tparavarsym(hp^.data.paras[i]);
  1772. if not(vo_is_hidden_para in currpara.varoptions) then
  1773. Comment(lvl,' - '+currpara.vardef.typename+' : '+EqualTypeName[currpara.eqval]);
  1774. end;
  1775. end;
  1776. hp:=hp^.next;
  1777. end;
  1778. end;
  1779. {$endif EXTDEBUG}
  1780. procedure tcallcandidates.get_information;
  1781. var
  1782. hp : pcandidate;
  1783. currpara : tparavarsym;
  1784. paraidx : integer;
  1785. currparanr : byte;
  1786. rfh,rth : bestreal;
  1787. objdef : tobjectdef;
  1788. def_from,
  1789. def_to : tdef;
  1790. currpt,
  1791. pt : tcallparanode;
  1792. eq : tequaltype;
  1793. convtype : tconverttype;
  1794. pdtemp,
  1795. pdoper : tprocdef;
  1796. releasecurrpt : boolean;
  1797. cdoptions : tcompare_defs_options;
  1798. begin
  1799. cdoptions:=[cdo_check_operator];
  1800. if FAllowVariant then
  1801. include(cdoptions,cdo_allow_variant);
  1802. { process all procs }
  1803. hp:=FProcs;
  1804. while assigned(hp) do
  1805. begin
  1806. { We compare parameters in reverse order (right to left),
  1807. the firstpara is already pointing to the last parameter
  1808. were we need to start comparing }
  1809. currparanr:=FParalength;
  1810. paraidx:=hp^.firstparaidx;
  1811. while (paraidx>=0) and (vo_is_hidden_para in tparavarsym(hp^.data.paras[paraidx]).varoptions) do
  1812. dec(paraidx);
  1813. pt:=tcallparanode(FParaNode);
  1814. while assigned(pt) and (paraidx>=0) do
  1815. begin
  1816. currpara:=tparavarsym(hp^.data.paras[paraidx]);
  1817. { currpt can be changed from loadn to calln when a procvar
  1818. is passed. This is to prevent that the change is permanent }
  1819. currpt:=pt;
  1820. releasecurrpt:=false;
  1821. { retrieve current parameter definitions to compares }
  1822. eq:=te_incompatible;
  1823. def_from:=currpt.resultdef;
  1824. def_to:=currpara.vardef;
  1825. if not(assigned(def_from)) then
  1826. internalerror(200212091);
  1827. if not(
  1828. assigned(def_to) or
  1829. ((po_varargs in hp^.data.procoptions) and
  1830. (currparanr>hp^.data.minparacount))
  1831. ) then
  1832. internalerror(200212092);
  1833. { Convert tp procvars when not expecting a procvar }
  1834. if (def_to.typ<>procvardef) and
  1835. (currpt.left.resultdef.typ=procvardef) and
  1836. { Only convert to call when there is no overload or the return type
  1837. is equal to the expected type. }
  1838. (
  1839. (count=1) or
  1840. equal_defs(tprocvardef(currpt.left.resultdef).returndef,def_to)
  1841. ) then
  1842. begin
  1843. releasecurrpt:=true;
  1844. currpt:=tcallparanode(pt.getcopy);
  1845. if maybe_call_procvar(currpt.left,true) then
  1846. begin
  1847. currpt.resultdef:=currpt.left.resultdef;
  1848. def_from:=currpt.left.resultdef;
  1849. end;
  1850. end;
  1851. { If we expect a procvar and the left is loadnode that
  1852. returns a procdef we need to find the correct overloaded
  1853. procdef that matches the expected procvar. The loadnode
  1854. temporary returned the first procdef (PFV) }
  1855. if (def_to.typ=procvardef) and
  1856. (currpt.left.nodetype=loadn) and
  1857. (currpt.left.resultdef.typ=procdef) then
  1858. begin
  1859. pdtemp:=tprocsym(Tloadnode(currpt.left).symtableentry).Find_procdef_byprocvardef(Tprocvardef(def_to));
  1860. if assigned(pdtemp) then
  1861. begin
  1862. tloadnode(currpt.left).setprocdef(pdtemp);
  1863. currpt.resultdef:=currpt.left.resultdef;
  1864. def_from:=currpt.left.resultdef;
  1865. end;
  1866. end;
  1867. { varargs are always equal, but not exact }
  1868. if (po_varargs in hp^.data.procoptions) and
  1869. (currparanr>hp^.data.minparacount) and
  1870. not is_array_of_const(def_from) and
  1871. not is_array_constructor(def_from) then
  1872. begin
  1873. eq:=te_equal;
  1874. end
  1875. else
  1876. { same definition -> exact }
  1877. if (def_from=def_to) then
  1878. begin
  1879. eq:=te_exact;
  1880. end
  1881. else
  1882. { for value and const parameters check if a integer is constant or
  1883. included in other integer -> equal and calc ordinal_distance }
  1884. if not(currpara.varspez in [vs_var,vs_out]) and
  1885. is_integer(def_from) and
  1886. is_integer(def_to) and
  1887. is_in_limit(def_from,def_to) then
  1888. begin
  1889. eq:=te_equal;
  1890. hp^.ordinal_distance:=hp^.ordinal_distance+
  1891. abs(bestreal(torddef(def_from).low)-bestreal(torddef(def_to).low));
  1892. if (torddef(def_to).ordtype=u64bit) then
  1893. rth:=bestreal(qword(torddef(def_to).high))
  1894. else
  1895. rth:=bestreal(torddef(def_to).high);
  1896. if (torddef(def_from).ordtype=u64bit) then
  1897. rfh:=bestreal(qword(torddef(def_from).high))
  1898. else
  1899. rfh:=bestreal(torddef(def_from).high);
  1900. hp^.ordinal_distance:=hp^.ordinal_distance+abs(rth-rfh);
  1901. { Give wrong sign a small penalty, this is need to get a diffrence
  1902. from word->[longword,longint] }
  1903. if is_signed(def_from)<>is_signed(def_to) then
  1904. hp^.ordinal_distance:=hp^.ordinal_distance+1.0;
  1905. end
  1906. else
  1907. { for value and const parameters check precision of real, give
  1908. penalty for loosing of precision. var and out parameters must match exactly }
  1909. if not(currpara.varspez in [vs_var,vs_out]) and
  1910. is_real(def_from) and
  1911. is_real(def_to) then
  1912. begin
  1913. eq:=te_equal;
  1914. if is_extended(def_to) then
  1915. rth:=bestreal(4)
  1916. else
  1917. if is_double (def_to) then
  1918. rth:=bestreal(2)
  1919. else
  1920. rth:=bestreal(1);
  1921. if is_extended(def_from) then
  1922. rfh:=bestreal(4)
  1923. else
  1924. if is_double (def_from) then
  1925. rfh:=bestreal(2)
  1926. else
  1927. rfh:=bestreal(1);
  1928. { penalty for shrinking of precision }
  1929. if rth<rfh then
  1930. rfh:=(rfh-rth)*16
  1931. else
  1932. rfh:=rth-rfh;
  1933. hp^.ordinal_distance:=hp^.ordinal_distance+rfh;
  1934. end
  1935. else
  1936. { related object parameters also need to determine the distance between the current
  1937. object and the object we are comparing with. var and out parameters must match exactly }
  1938. if not(currpara.varspez in [vs_var,vs_out]) and
  1939. (def_from.typ=objectdef) and
  1940. (def_to.typ=objectdef) and
  1941. (tobjectdef(def_from).objecttype=tobjectdef(def_to).objecttype) and
  1942. tobjectdef(def_from).is_related(tobjectdef(def_to)) then
  1943. begin
  1944. eq:=te_convert_l1;
  1945. objdef:=tobjectdef(def_from);
  1946. while assigned(objdef) do
  1947. begin
  1948. if objdef=def_to then
  1949. break;
  1950. hp^.ordinal_distance:=hp^.ordinal_distance+1;
  1951. objdef:=objdef.childof;
  1952. end;
  1953. end
  1954. else
  1955. { generic type comparision }
  1956. begin
  1957. eq:=compare_defs_ext(def_from,def_to,currpt.left.nodetype,convtype,pdoper,cdoptions);
  1958. { when the types are not equal we need to check
  1959. some special case for parameter passing }
  1960. if (eq<te_equal) then
  1961. begin
  1962. if currpara.varspez in [vs_var,vs_out] then
  1963. begin
  1964. { para requires an equal type so the previous found
  1965. match was not good enough, reset to incompatible }
  1966. eq:=te_incompatible;
  1967. { var_para_allowed will return te_equal and te_convert_l1 to
  1968. make a difference for best matching }
  1969. var_para_allowed(eq,currpt.resultdef,currpara.vardef)
  1970. end
  1971. else
  1972. para_allowed(eq,currpt,def_to);
  1973. end;
  1974. end;
  1975. { when a procvar was changed to a call an exact much is
  1976. downgraded to equal. This way an overload call with the
  1977. procvar is choosen. See tb0471 (PFV) }
  1978. if (pt<>currpt) and (eq=te_exact) then
  1979. eq:=te_equal;
  1980. { increase correct counter }
  1981. case eq of
  1982. te_exact :
  1983. inc(hp^.exact_count);
  1984. te_equal :
  1985. inc(hp^.equal_count);
  1986. te_convert_l1 :
  1987. inc(hp^.cl1_count);
  1988. te_convert_l2 :
  1989. inc(hp^.cl2_count);
  1990. te_convert_l3 :
  1991. inc(hp^.cl3_count);
  1992. te_convert_operator :
  1993. inc(hp^.coper_count);
  1994. te_incompatible :
  1995. hp^.invalid:=true;
  1996. else
  1997. internalerror(200212072);
  1998. end;
  1999. { stop checking when an incompatible parameter is found }
  2000. if hp^.invalid then
  2001. begin
  2002. { store the current parameter info for
  2003. a nice error message when no procedure is found }
  2004. hp^.wrongparaidx:=paraidx;
  2005. hp^.wrongparanr:=currparanr;
  2006. break;
  2007. end;
  2008. {$ifdef EXTDEBUG}
  2009. { store equal in node tree for dump }
  2010. currpara.eqval:=eq;
  2011. {$endif EXTDEBUG}
  2012. { maybe release temp currpt }
  2013. if releasecurrpt then
  2014. currpt.free;
  2015. { next parameter in the call tree }
  2016. pt:=tcallparanode(pt.right);
  2017. { next parameter for definition, only goto next para
  2018. if we're out of the varargs }
  2019. if not(po_varargs in hp^.data.procoptions) or
  2020. (currparanr<=hp^.data.maxparacount) then
  2021. begin
  2022. { Ignore vs_hidden parameters }
  2023. repeat
  2024. dec(paraidx);
  2025. until (paraidx<0) or not(vo_is_hidden_para in tparavarsym(hp^.data.paras[paraidx]).varoptions);
  2026. end;
  2027. dec(currparanr);
  2028. end;
  2029. if not(hp^.invalid) and
  2030. (assigned(pt) or (paraidx>=0) or (currparanr<>0)) then
  2031. internalerror(200212141);
  2032. { next candidate }
  2033. hp:=hp^.next;
  2034. end;
  2035. end;
  2036. function get_variantequaltype(def: tdef): tvariantequaltype;
  2037. const
  2038. variantorddef_cl: array[tordtype] of tvariantequaltype =
  2039. (tve_incompatible,tve_byte,tve_word,tve_cardinal,tve_chari64,
  2040. tve_shortint,tve_smallint,tve_longint,tve_chari64,
  2041. tve_boolformal,tve_boolformal,tve_boolformal,tve_boolformal,
  2042. tve_chari64,tve_chari64,tve_dblcurrency);
  2043. {$warning fixme for 128 bit floats }
  2044. variantfloatdef_cl: array[tfloattype] of tvariantequaltype =
  2045. (tve_single,tve_dblcurrency,tve_extended,
  2046. tve_dblcurrency,tve_dblcurrency,tve_extended);
  2047. variantstringdef_cl: array[tstringtype] of tvariantequaltype =
  2048. (tve_sstring,tve_astring,tve_astring,tve_wstring);
  2049. begin
  2050. result:=tve_incompatible;
  2051. case def.typ of
  2052. orddef:
  2053. begin
  2054. result:=variantorddef_cl[torddef(def).ordtype];
  2055. end;
  2056. floatdef:
  2057. begin
  2058. result:=variantfloatdef_cl[tfloatdef(def).floattype];
  2059. end;
  2060. stringdef:
  2061. begin
  2062. result:=variantstringdef_cl[tstringdef(def).stringtype];
  2063. end;
  2064. formaldef:
  2065. begin
  2066. result:=tve_boolformal;
  2067. end;
  2068. else
  2069. internalerror(2006122804);
  2070. end
  2071. end;
  2072. function is_better_candidate(currpd,bestpd:pcandidate):integer;
  2073. var
  2074. res : integer;
  2075. begin
  2076. {
  2077. Return values:
  2078. > 0 when currpd is better than bestpd
  2079. < 0 when bestpd is better than currpd
  2080. = 0 when both are equal
  2081. To choose the best candidate we use the following order:
  2082. - Incompatible flag
  2083. - (Smaller) Number of convert operator parameters.
  2084. - (Smaller) Number of convertlevel 2 parameters.
  2085. - (Smaller) Number of convertlevel 1 parameters.
  2086. - (Bigger) Number of exact parameters.
  2087. - (Smaller) Number of equal parameters.
  2088. - (Smaller) Total of ordinal distance. For example, the distance of a word
  2089. to a byte is 65535-255=65280.
  2090. }
  2091. if bestpd^.invalid then
  2092. begin
  2093. if currpd^.invalid then
  2094. res:=0
  2095. else
  2096. res:=1;
  2097. end
  2098. else
  2099. if currpd^.invalid then
  2100. res:=-1
  2101. else
  2102. begin
  2103. { less operator parameters? }
  2104. res:=(bestpd^.coper_count-currpd^.coper_count);
  2105. if (res=0) then
  2106. begin
  2107. { less cl3 parameters? }
  2108. res:=(bestpd^.cl3_count-currpd^.cl3_count);
  2109. if (res=0) then
  2110. begin
  2111. { less cl2 parameters? }
  2112. res:=(bestpd^.cl2_count-currpd^.cl2_count);
  2113. if (res=0) then
  2114. begin
  2115. { less cl1 parameters? }
  2116. res:=(bestpd^.cl1_count-currpd^.cl1_count);
  2117. if (res=0) then
  2118. begin
  2119. { more exact parameters? }
  2120. res:=(currpd^.exact_count-bestpd^.exact_count);
  2121. if (res=0) then
  2122. begin
  2123. { less equal parameters? }
  2124. res:=(bestpd^.equal_count-currpd^.equal_count);
  2125. if (res=0) then
  2126. begin
  2127. { smaller ordinal distance? }
  2128. if (currpd^.ordinal_distance<bestpd^.ordinal_distance) then
  2129. res:=1
  2130. else
  2131. if (currpd^.ordinal_distance>bestpd^.ordinal_distance) then
  2132. res:=-1
  2133. else
  2134. res:=0;
  2135. end;
  2136. end;
  2137. end;
  2138. end;
  2139. end;
  2140. end;
  2141. end;
  2142. is_better_candidate:=res;
  2143. end;
  2144. { Delphi precedence rules extracted from test programs. Only valid if passing
  2145. a variant parameter to overloaded procedures expecting exactly one parameter.
  2146. single > (char, currency, int64, shortstring, ansistring, widestring, extended, double)
  2147. double/currency > (char, int64, shortstring, ansistring, widestring, extended)
  2148. extended > (char, int64, shortstring, ansistring, widestring)
  2149. longint/cardinal > (int64, shortstring, ansistring, widestring, extended, double, single, char, currency)
  2150. smallint > (longint, int64, shortstring, ansistring, widestring, extended, double single, char, currency);
  2151. word > (longint, cardinal, int64, shortstring, ansistring, widestring, extended, double single, char, currency);
  2152. shortint > (longint, smallint, int64, shortstring, ansistring, widestring, extended, double, single, char, currency)
  2153. byte > (longint, cardinal, word, smallint, int64, shortstring, ansistring, widestring, extended, double, single, char, currency);
  2154. boolean/formal > (char, int64, shortstring, ansistring, widestring)
  2155. shortstring > (char, int64, ansistring, widestring)
  2156. ansistring > (char, int64, widestring)
  2157. widestring > (char, int64)
  2158. Relations not mentioned mean that they conflict: no decision possible }
  2159. function is_better_candidate_single_variant(currpd,bestpd:pcandidate):integer;
  2160. function calculate_relation(const currvcl, bestvcl, testvcl:
  2161. tvariantequaltype; const conflictvcls: tvariantequaltypes):integer;
  2162. begin
  2163. { if (bestvcl=conflictvcl) or
  2164. (currvcl=conflictvcl) then
  2165. result:=0
  2166. else if (bestvcl=testvcl) then
  2167. result:=-1
  2168. else result:=1 }
  2169. result:=1-2*ord(bestvcl=testvcl)+
  2170. ord(currvcl in conflictvcls)-ord(bestvcl in conflictvcls);
  2171. end;
  2172. var
  2173. paraidx,
  2174. res: integer;
  2175. currpara, bestpara: tparavarsym;
  2176. currvcl, bestvcl: tvariantequaltype;
  2177. begin
  2178. {
  2179. Return values:
  2180. > 0 when currpd is better than bestpd
  2181. < 0 when bestpd is better than currpd
  2182. = 0 when both are equal
  2183. }
  2184. if (currpd^.firstparaidx<>bestpd^.firstparaidx) then
  2185. internalerror(2006122801);
  2186. paraidx:=currpd^.firstparaidx;
  2187. while (paraidx>=0) and (vo_is_hidden_para in tparavarsym(currpd^.data.paras[paraidx]).varoptions) do
  2188. if (vo_is_hidden_para in tparavarsym(bestpd^.data.paras[paraidx]).varoptions) then
  2189. dec(paraidx)
  2190. else
  2191. internalerror(2006122802);
  2192. if (vo_is_hidden_para in tparavarsym(currpd^.data.paras[paraidx]).varoptions) then
  2193. internalerror(2006122803);
  2194. currpara:=tparavarsym(currpd^.data.paras[paraidx]);
  2195. bestpara:=tparavarsym(bestpd^.data.paras[paraidx]);
  2196. { if one of the parameters is a regular variant, fall back to the }
  2197. { default algorithm }
  2198. if (currpara.vardef.typ = variantdef) or
  2199. (bestpara.vardef.typ = variantdef) then
  2200. begin
  2201. result:=is_better_candidate(currpd,bestpd);
  2202. exit;
  2203. end;
  2204. currvcl:=get_variantequaltype(currpara.vardef);
  2205. bestvcl:=get_variantequaltype(bestpara.vardef);
  2206. { sanity check }
  2207. result:=-5;
  2208. { if both are the same, there is a conflict }
  2209. if (currvcl=bestvcl) then
  2210. result:=0
  2211. { boolean and formal are better than chari64str, but conflict with }
  2212. { everything else }
  2213. else if (currvcl=tve_boolformal) or
  2214. (bestvcl=tve_boolformal) then
  2215. if (currvcl=tve_boolformal) then
  2216. result:=ord(bestvcl in [tve_chari64,tve_sstring,tve_astring,tve_wstring])
  2217. else
  2218. result:=-ord(currvcl in [tve_chari64,tve_sstring,tve_astring,tve_wstring])
  2219. { byte is better than everything else (we assume both aren't byte, }
  2220. { since there's only one parameter and that one can't be the same) }
  2221. else if (currvcl=tve_byte) or
  2222. (bestvcl=tve_byte) then
  2223. result:=calculate_relation(currvcl,bestvcl,tve_byte,[tve_shortint])
  2224. { shortint conflicts with word and cardinal, but is better than }
  2225. { everything else but byte (which has already been handled) }
  2226. else if (currvcl=tve_shortint) or
  2227. (bestvcl=tve_shortint) then
  2228. result:=calculate_relation(currvcl,bestvcl,tve_shortint,[tve_word, tve_cardinal])
  2229. { word conflicts with smallint, but is better than everything else }
  2230. { but shortint and byte (which has already been handled) }
  2231. else if (currvcl=tve_word) or
  2232. (bestvcl=tve_word) then
  2233. result:=calculate_relation(currvcl,bestvcl,tve_word,[tve_smallint])
  2234. { smallint conflicts with cardinal, but is better than everything }
  2235. { which has not yet been tested }
  2236. else if (currvcl=tve_smallint) or
  2237. (bestvcl=tve_smallint) then
  2238. result:=calculate_relation(currvcl,bestvcl,tve_smallint,[tve_cardinal])
  2239. { cardinal conflicts with each longint and is better than everything }
  2240. { which has not yet been tested }
  2241. else if (currvcl = tve_cardinal) or
  2242. (bestvcl=tve_cardinal) then
  2243. result:=calculate_relation(currvcl,bestvcl,tve_cardinal,[tve_longint])
  2244. { longint is better than everything which has not yet been tested }
  2245. else if (currvcl=tve_longint) or
  2246. (bestvcl=tve_longint) then
  2247. { if bestvcl=tve_longint then
  2248. result:=-1
  2249. else
  2250. result:=1 }
  2251. result:=1-2*ord(bestvcl=tve_longint)
  2252. { single is better than everything left }
  2253. else if (currvcl=tve_single) or
  2254. (bestvcl=tve_single) then
  2255. result:=1-2*ord(bestvcl=tve_single)
  2256. { double/comp/currency are better than everything left, and conflict }
  2257. { with each other (but that's already tested) }
  2258. else if (currvcl=tve_dblcurrency) or
  2259. (bestvcl=tve_dblcurrency) then
  2260. result:=1-2*ord(bestvcl=tve_dblcurrency)
  2261. { extended is better than everything left }
  2262. else if (currvcl=tve_extended) or
  2263. (bestvcl=tve_extended) then
  2264. result:=1-2*ord(bestvcl=tve_extended)
  2265. { shortstring is better than everything left }
  2266. else if (currvcl=tve_sstring) or
  2267. (bestvcl=tve_sstring) then
  2268. result:=1-2*ord(bestvcl=tve_sstring)
  2269. { ansistring is better than everything left }
  2270. else if (currvcl=tve_astring) or
  2271. (bestvcl=tve_astring) then
  2272. result:=1-2*ord(bestvcl=tve_astring)
  2273. { widestring is better than everything left }
  2274. else if (currvcl=tve_wstring) or
  2275. (bestvcl=tve_wstring) then
  2276. result:=1-2*ord(bestvcl=tve_wstring);
  2277. { all possibilities should have been checked now }
  2278. if (result=-5) then
  2279. internalerror(2006122805);
  2280. end;
  2281. function tcallcandidates.choose_best(var bestpd:tabstractprocdef; singlevariant: boolean):integer;
  2282. var
  2283. besthpstart,
  2284. hp : pcandidate;
  2285. cntpd,
  2286. res : integer;
  2287. begin
  2288. {
  2289. Returns the number of candidates left and the
  2290. first candidate is returned in pdbest
  2291. }
  2292. { Setup the first procdef as best, only count it as a result
  2293. when it is valid }
  2294. bestpd:=FProcs^.data;
  2295. if FProcs^.invalid then
  2296. cntpd:=0
  2297. else
  2298. cntpd:=1;
  2299. if assigned(FProcs^.next) then
  2300. begin
  2301. besthpstart:=FProcs;
  2302. hp:=FProcs^.next;
  2303. while assigned(hp) do
  2304. begin
  2305. if not singlevariant then
  2306. res:=is_better_candidate(hp,besthpstart)
  2307. else
  2308. res:=is_better_candidate_single_variant(hp,besthpstart);
  2309. if (res>0) then
  2310. begin
  2311. { hp is better, flag all procs to be incompatible }
  2312. while (besthpstart<>hp) do
  2313. begin
  2314. besthpstart^.invalid:=true;
  2315. besthpstart:=besthpstart^.next;
  2316. end;
  2317. { besthpstart is already set to hp }
  2318. bestpd:=besthpstart^.data;
  2319. cntpd:=1;
  2320. end
  2321. else
  2322. if (res<0) then
  2323. begin
  2324. { besthpstart is better, flag current hp to be incompatible }
  2325. hp^.invalid:=true;
  2326. end
  2327. else
  2328. begin
  2329. { res=0, both are valid }
  2330. if not hp^.invalid then
  2331. inc(cntpd);
  2332. end;
  2333. hp:=hp^.next;
  2334. end;
  2335. end;
  2336. result:=cntpd;
  2337. end;
  2338. procedure tcallcandidates.find_wrong_para;
  2339. var
  2340. currparanr : smallint;
  2341. hp : pcandidate;
  2342. pt : tcallparanode;
  2343. wrongpara : tparavarsym;
  2344. begin
  2345. { Only process the first overloaded procdef }
  2346. hp:=FProcs;
  2347. { Find callparanode corresponding to the argument }
  2348. pt:=tcallparanode(FParanode);
  2349. currparanr:=FParalength;
  2350. while assigned(pt) and
  2351. (currparanr>hp^.wrongparanr) do
  2352. begin
  2353. pt:=tcallparanode(pt.right);
  2354. dec(currparanr);
  2355. end;
  2356. if (currparanr<>hp^.wrongparanr) or
  2357. not assigned(pt) then
  2358. internalerror(200212094);
  2359. { Show error message, when it was a var or out parameter
  2360. guess that it is a missing typeconv }
  2361. wrongpara:=tparavarsym(hp^.data.paras[hp^.wrongparaidx]);
  2362. if wrongpara.varspez in [vs_var,vs_out] then
  2363. begin
  2364. { Maybe passing the correct type but passing a const to var parameter }
  2365. if (compare_defs(pt.resultdef,wrongpara.vardef,pt.nodetype)<>te_incompatible) and
  2366. not valid_for_var(pt.left,true) then
  2367. CGMessagePos(pt.left.fileinfo,type_e_variable_id_expected)
  2368. else
  2369. CGMessagePos3(pt.left.fileinfo,parser_e_call_by_ref_without_typeconv,tostr(hp^.wrongparanr),
  2370. FullTypeName(pt.left.resultdef,wrongpara.vardef),
  2371. FullTypeName(wrongpara.vardef,pt.left.resultdef))
  2372. end
  2373. else
  2374. CGMessagePos3(pt.left.fileinfo,type_e_wrong_parameter_type,tostr(hp^.wrongparanr),
  2375. FullTypeName(pt.left.resultdef,wrongpara.vardef),
  2376. FullTypeName(wrongpara.vardef,pt.left.resultdef));
  2377. end;
  2378. procedure check_hints(const srsym: tsym; const symoptions: tsymoptions);
  2379. begin
  2380. if not assigned(srsym) then
  2381. internalerror(200602051);
  2382. if sp_hint_deprecated in symoptions then
  2383. Message1(sym_w_deprecated_symbol,srsym.realname);
  2384. if sp_hint_platform in symoptions then
  2385. Message1(sym_w_non_portable_symbol,srsym.realname);
  2386. if sp_hint_unimplemented in symoptions then
  2387. Message1(sym_w_non_implemented_symbol,srsym.realname);
  2388. end;
  2389. procedure check_ranges(const location: tfileposinfo; source: tnode; destdef: tdef);
  2390. begin
  2391. { check if the assignment may cause a range check error }
  2392. { if its not explicit, and only if the values are }
  2393. { ordinals, enumdef and floatdef }
  2394. if assigned(destdef) and
  2395. (destdef.typ in [enumdef,orddef,floatdef]) and
  2396. not is_boolean(destdef) and
  2397. assigned(source.resultdef) and
  2398. (source.resultdef.typ in [enumdef,orddef,floatdef]) and
  2399. not is_boolean(source.resultdef) and
  2400. not is_constrealnode(source) then
  2401. begin
  2402. if (destdef.size < source.resultdef.size) then
  2403. begin
  2404. if (cs_check_range in current_settings.localswitches) then
  2405. MessagePos(location,type_w_smaller_possible_range_check)
  2406. else
  2407. MessagePos(location,type_h_smaller_possible_range_check);
  2408. end;
  2409. end;
  2410. end;
  2411. end.