htypechk.pas 101 KB

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