htypechk.pas 81 KB

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