htypechk.pas 85 KB

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