htypechk.pas 99 KB

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