htypechk.pas 98 KB

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