htypechk.pas 82 KB

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