htypechk.pas 82 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216
  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_written,vs_readwritten),
  665. { vs_declared -> ... }
  666. (vs_none,vs_declared,vs_initialised,vs_read,vs_written,vs_readwritten),
  667. { vs_initialised -> ... }
  668. (vs_none,vs_initialised,vs_initialised,vs_read,vs_written,vs_readwritten),
  669. { vs_read -> ... }
  670. (vs_none,vs_read,vs_read,vs_read,vs_readwritten,vs_readwritten),
  671. { vs_written -> ... }
  672. (vs_none,vs_written,vs_written,vs_readwritten,vs_written,vs_readwritten),
  673. { vs_readwritten -> ... }
  674. (vs_none,vs_readwritten,vs_readwritten,vs_readwritten,vs_readwritten,vs_readwritten));
  675. var
  676. hsym : tabstractvarsym;
  677. begin
  678. while assigned(p) do
  679. begin
  680. case p.nodetype of
  681. typeconvn :
  682. begin
  683. case ttypeconvnode(p).convtype of
  684. tc_cchar_2_pchar,
  685. tc_cstring_2_pchar,
  686. tc_array_2_pointer :
  687. exclude(varstateflags,vsf_must_be_valid);
  688. tc_pchar_2_string,
  689. tc_pointer_2_array :
  690. include(varstateflags,vsf_must_be_valid);
  691. end;
  692. p:=tunarynode(p).left;
  693. end;
  694. subscriptn :
  695. p:=tunarynode(p).left;
  696. vecn:
  697. begin
  698. set_varstate(tbinarynode(p).right,vs_read,[vsf_must_be_valid]);
  699. if (newstate in [vs_read,vs_readwritten]) or
  700. not(tunarynode(p).left.resulttype.def.deftype in [stringdef,arraydef]) then
  701. include(varstateflags,vsf_must_be_valid)
  702. else if (newstate = vs_written) then
  703. exclude(varstateflags,vsf_must_be_valid);
  704. p:=tunarynode(p).left;
  705. end;
  706. { do not parse calln }
  707. calln :
  708. break;
  709. loadn :
  710. begin
  711. if (tloadnode(p).symtableentry.typ in [localvarsym,paravarsym,globalvarsym]) then
  712. begin
  713. hsym:=tabstractvarsym(tloadnode(p).symtableentry);
  714. if (vsf_must_be_valid in varstateflags) and (hsym.varstate=vs_declared) then
  715. begin
  716. { Give warning/note for uninitialized locals }
  717. if assigned(hsym.owner) and
  718. not(vo_is_external in hsym.varoptions) and
  719. (hsym.owner.symtabletype in [parasymtable,localsymtable,staticsymtable]) and
  720. ((hsym.owner=current_procinfo.procdef.localst) or
  721. ((hsym.owner=current_procinfo.procdef.parast) and
  722. (vo_is_funcret in hsym.varoptions))) then
  723. begin
  724. if (vo_is_funcret in hsym.varoptions) then
  725. begin
  726. if (vsf_use_hints in varstateflags) then
  727. CGMessage(sym_h_function_result_uninitialized)
  728. else
  729. CGMessage(sym_w_function_result_uninitialized)
  730. end
  731. else
  732. begin
  733. if tloadnode(p).symtable.symtabletype=localsymtable then
  734. begin
  735. if (vsf_use_hints in varstateflags) then
  736. CGMessage1(sym_h_uninitialized_local_variable,hsym.realname)
  737. else
  738. CGMessage1(sym_w_uninitialized_local_variable,hsym.realname);
  739. end
  740. else
  741. begin
  742. if (vsf_use_hints in varstateflags) then
  743. CGMessage1(sym_h_uninitialized_variable,hsym.realname)
  744. else
  745. CGMessage1(sym_w_uninitialized_variable,hsym.realname);
  746. end;
  747. end;
  748. end;
  749. end;
  750. { don't override vs_readwritten with vs_initialised }
  751. hsym.varstate := vstrans[hsym.varstate,newstate];
  752. end;
  753. break;
  754. end;
  755. callparan :
  756. internalerror(200310081);
  757. else
  758. break;
  759. end;{case }
  760. end;
  761. end;
  762. procedure set_unique(p : tnode);
  763. begin
  764. while assigned(p) do
  765. begin
  766. case p.nodetype of
  767. vecn:
  768. begin
  769. include(p.flags,nf_callunique);
  770. break;
  771. end;
  772. typeconvn,
  773. subscriptn,
  774. derefn:
  775. p:=tunarynode(p).left;
  776. else
  777. break;
  778. end;
  779. end;
  780. end;
  781. function valid_for_assign(p:tnode;opts:TValidAssigns; report_errors: boolean):boolean;
  782. var
  783. hp : tnode;
  784. gotstring,
  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. gotdynarray:=false;
  808. gotstring:=false;
  809. hp:=p;
  810. if not(valid_void in opts) and
  811. is_void(hp.resulttype.def) then
  812. begin
  813. if report_errors then
  814. CGMessagePos(hp.fileinfo,errmsg);
  815. exit;
  816. end;
  817. while assigned(hp) do
  818. begin
  819. { property allowed? calln has a property check itself }
  820. if (nf_isproperty in hp.flags) then
  821. begin
  822. if (hp.nodetype=calln) then
  823. begin
  824. { check return type }
  825. case hp.resulttype.def.deftype of
  826. pointerdef :
  827. gotpointer:=true;
  828. objectdef :
  829. gotclass:=is_class_or_interface(hp.resulttype.def);
  830. recorddef :
  831. gotrecord:=true;
  832. classrefdef :
  833. gotclass:=true;
  834. stringdef :
  835. gotstring:=true;
  836. end;
  837. if (valid_property in opts) then
  838. begin
  839. { don't allow writing to calls that will create
  840. temps like calls that return a structure and we
  841. are assigning to a member }
  842. if (valid_const in opts) or
  843. not(
  844. (gotsubscript and gotrecord) or
  845. (gotstring and gotvec)
  846. ) then
  847. result:=true
  848. else
  849. if report_errors then
  850. CGMessagePos(hp.fileinfo,errmsg);
  851. end
  852. else
  853. begin
  854. { 1. if it returns a pointer and we've found a deref,
  855. 2. if it returns a class or record and a subscription or with is found
  856. 3. if the address is needed of a field (subscriptn) }
  857. if (gotpointer and gotderef) or
  858. (gotstring and gotvec) or
  859. (
  860. (gotclass or gotrecord) and
  861. (gotsubscript)
  862. ) or
  863. (
  864. (gotvec and gotdynarray)
  865. ) or
  866. (
  867. (Valid_Addr in opts) and
  868. (hp.nodetype=subscriptn)
  869. ) then
  870. result:=true
  871. else
  872. if report_errors then
  873. CGMessagePos(hp.fileinfo,errmsg);
  874. end;
  875. end
  876. else
  877. result:=true;
  878. exit;
  879. end;
  880. if (Valid_Const in opts) and is_constnode(hp) then
  881. begin
  882. result:=true;
  883. exit;
  884. end;
  885. case hp.nodetype of
  886. temprefn :
  887. begin
  888. valid_for_assign := true;
  889. exit;
  890. end;
  891. derefn :
  892. begin
  893. gotderef:=true;
  894. hp:=tderefnode(hp).left;
  895. end;
  896. typeconvn :
  897. begin
  898. { typecast sizes must match, exceptions:
  899. - implicit typecast made by absolute
  900. - from formaldef
  901. - from void
  902. - from/to open array
  903. - typecast from pointer to array }
  904. fromdef:=ttypeconvnode(hp).left.resulttype.def;
  905. todef:=hp.resulttype.def;
  906. if not((nf_absolute in ttypeconvnode(hp).flags) or
  907. (fromdef.deftype=formaldef) or
  908. is_void(fromdef) or
  909. is_open_array(fromdef) or
  910. is_open_array(todef) or
  911. ((fromdef.deftype=pointerdef) and (todef.deftype=arraydef)) or
  912. ((fromdef.deftype = objectdef) and (todef.deftype = objectdef) and
  913. (tobjectdef(fromdef).is_related(tobjectdef(todef))))) and
  914. (fromdef.size<>todef.size) then
  915. begin
  916. { in TP it is allowed to typecast to smaller types. But the variable can't
  917. be in a register }
  918. if (m_tp7 in aktmodeswitches) or
  919. (todef.size<fromdef.size) then
  920. make_not_regable(hp)
  921. else
  922. if report_errors then
  923. CGMessagePos2(hp.fileinfo,type_e_typecast_wrong_size_for_assignment,tostr(fromdef.size),tostr(todef.size));
  924. end;
  925. { don't allow assignments to typeconvs that need special code }
  926. if not(gotsubscript or gotvec or gotderef) and
  927. not(ttypeconvnode(hp).assign_allowed) then
  928. begin
  929. if report_errors then
  930. CGMessagePos(hp.fileinfo,errmsg);
  931. exit;
  932. end;
  933. case hp.resulttype.def.deftype of
  934. pointerdef :
  935. gotpointer:=true;
  936. objectdef :
  937. gotclass:=is_class_or_interface(hp.resulttype.def);
  938. classrefdef :
  939. gotclass:=true;
  940. arraydef :
  941. begin
  942. { pointer -> array conversion is done then we need to see it
  943. as a deref, because a ^ is then not required anymore }
  944. if (ttypeconvnode(hp).left.resulttype.def.deftype=pointerdef) then
  945. gotderef:=true;
  946. end;
  947. end;
  948. hp:=ttypeconvnode(hp).left;
  949. end;
  950. vecn :
  951. begin
  952. gotvec:=true;
  953. { accesses to dyn. arrays override read only access in delphi }
  954. if (m_delphi in aktmodeswitches) and is_dynamic_array(tunarynode(hp).left.resulttype.def) then
  955. gotdynarray:=true;
  956. hp:=tunarynode(hp).left;
  957. end;
  958. asn :
  959. begin
  960. { asn can't be assigned directly, it returns the value in a register instead
  961. of reference. }
  962. if not(gotsubscript or gotderef or gotvec) then
  963. begin
  964. if report_errors then
  965. CGMessagePos(hp.fileinfo,errmsg);
  966. exit;
  967. end;
  968. hp:=tunarynode(hp).left;
  969. end;
  970. subscriptn :
  971. begin
  972. gotsubscript:=true;
  973. { loop counter? }
  974. if not(Valid_Const in opts) and
  975. (vo_is_loop_counter in tsubscriptnode(hp).vs.varoptions) then
  976. begin
  977. if report_errors then
  978. CGMessage1(parser_e_illegal_assignment_to_count_var,tsubscriptnode(hp).vs.realname)
  979. else
  980. exit;
  981. end;
  982. { a class/interface access is an implicit }
  983. { dereferencing }
  984. hp:=tsubscriptnode(hp).left;
  985. if is_class_or_interface(hp.resulttype.def) then
  986. gotderef:=true;
  987. end;
  988. muln,
  989. divn,
  990. andn,
  991. xorn,
  992. orn,
  993. notn,
  994. subn,
  995. addn :
  996. begin
  997. { Allow operators on a pointer, or an integer
  998. and a pointer typecast and deref has been found }
  999. if ((hp.resulttype.def.deftype=pointerdef) or
  1000. (is_integer(hp.resulttype.def) and gotpointer)) and
  1001. gotderef then
  1002. result:=true
  1003. else
  1004. { Temp strings are stored in memory, for compatibility with
  1005. delphi only }
  1006. if (m_delphi in aktmodeswitches) and
  1007. ((valid_addr in opts) or
  1008. (valid_const in opts)) and
  1009. (hp.resulttype.def.deftype=stringdef) then
  1010. result:=true
  1011. else
  1012. if report_errors then
  1013. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  1014. exit;
  1015. end;
  1016. niln,
  1017. pointerconstn :
  1018. begin
  1019. { to support e.g. @tmypointer(0)^.data; see tests/tbs/tb0481 }
  1020. if gotderef then
  1021. result:=true
  1022. else
  1023. if report_errors then
  1024. CGMessagePos(hp.fileinfo,type_e_no_assign_to_addr);
  1025. exit;
  1026. end;
  1027. addrn :
  1028. begin
  1029. if gotderef then
  1030. result:=true
  1031. else
  1032. if report_errors then
  1033. CGMessagePos(hp.fileinfo,type_e_no_assign_to_addr);
  1034. exit;
  1035. end;
  1036. calln :
  1037. begin
  1038. { check return type }
  1039. case hp.resulttype.def.deftype of
  1040. arraydef :
  1041. begin
  1042. { dynamic arrays are allowed when there is also a
  1043. vec node }
  1044. if is_dynamic_array(hp.resulttype.def) and
  1045. gotvec then
  1046. begin
  1047. gotderef:=true;
  1048. gotpointer:=true;
  1049. end;
  1050. end;
  1051. pointerdef :
  1052. gotpointer:=true;
  1053. objectdef :
  1054. gotclass:=is_class_or_interface(hp.resulttype.def);
  1055. recorddef, { handle record like class it needs a subscription }
  1056. classrefdef :
  1057. gotclass:=true;
  1058. stringdef :
  1059. gotstring:=true;
  1060. end;
  1061. { 1. if it returns a pointer and we've found a deref,
  1062. 2. if it returns a class or record and a subscription or with is found
  1063. 3. string is returned }
  1064. if (gotstring and gotvec) or
  1065. (gotpointer and gotderef) or
  1066. (gotclass and gotsubscript) then
  1067. result:=true
  1068. else
  1069. { Temp strings are stored in memory, for compatibility with
  1070. delphi only }
  1071. if (m_delphi in aktmodeswitches) and
  1072. (valid_addr in opts) and
  1073. (hp.resulttype.def.deftype=stringdef) then
  1074. result:=true
  1075. else
  1076. if ([valid_const,valid_addr] * opts = [valid_const]) then
  1077. result:=true
  1078. else
  1079. if report_errors then
  1080. CGMessagePos(hp.fileinfo,errmsg);
  1081. exit;
  1082. end;
  1083. inlinen :
  1084. begin
  1085. if (valid_const in opts) and
  1086. (tinlinenode(hp).inlinenumber in [in_typeof_x]) then
  1087. result:=true
  1088. else
  1089. if report_errors then
  1090. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  1091. exit;
  1092. end;
  1093. loadn :
  1094. begin
  1095. case tloadnode(hp).symtableentry.typ of
  1096. absolutevarsym,
  1097. globalvarsym,
  1098. localvarsym,
  1099. paravarsym :
  1100. begin
  1101. { loop counter? }
  1102. if not(Valid_Const in opts) and
  1103. not gotderef and
  1104. (vo_is_loop_counter in tabstractvarsym(tloadnode(hp).symtableentry).varoptions) then
  1105. if report_errors then
  1106. CGMessage1(parser_e_illegal_assignment_to_count_var,tloadnode(hp).symtableentry.realname)
  1107. else
  1108. exit;
  1109. { derefed pointer }
  1110. if (tabstractvarsym(tloadnode(hp).symtableentry).varspez=vs_const) then
  1111. begin
  1112. { allow p^:= constructions with p is const parameter }
  1113. if gotderef or gotdynarray or (Valid_Const in opts) then
  1114. result:=true
  1115. else
  1116. if report_errors then
  1117. CGMessagePos(tloadnode(hp).fileinfo,type_e_no_assign_to_const);
  1118. exit;
  1119. end;
  1120. result:=true;
  1121. exit;
  1122. end;
  1123. typedconstsym :
  1124. begin
  1125. if ttypedconstsym(tloadnode(hp).symtableentry).is_writable or
  1126. (valid_addr in opts) or
  1127. (valid_const in opts) then
  1128. result:=true
  1129. else
  1130. if report_errors then
  1131. CGMessagePos(hp.fileinfo,type_e_no_assign_to_const);
  1132. exit;
  1133. end;
  1134. procsym :
  1135. begin
  1136. if (Valid_Const in opts) then
  1137. result:=true
  1138. else
  1139. if report_errors then
  1140. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  1141. exit;
  1142. end;
  1143. labelsym :
  1144. begin
  1145. if (Valid_Addr in opts) then
  1146. result:=true
  1147. else
  1148. if report_errors then
  1149. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  1150. exit;
  1151. end;
  1152. constsym:
  1153. begin
  1154. if (tconstsym(tloadnode(hp).symtableentry).consttyp=constresourcestring) and
  1155. (valid_addr in opts) then
  1156. result:=true
  1157. else
  1158. if report_errors then
  1159. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  1160. exit;
  1161. end;
  1162. else
  1163. begin
  1164. if report_errors then
  1165. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  1166. exit;
  1167. end;
  1168. end;
  1169. end;
  1170. else
  1171. begin
  1172. if report_errors then
  1173. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  1174. exit;
  1175. end;
  1176. end;
  1177. end;
  1178. end;
  1179. function valid_for_var(p:tnode; report_errors: boolean):boolean;
  1180. begin
  1181. valid_for_var:=valid_for_assign(p,[],report_errors);
  1182. end;
  1183. function valid_for_formal_var(p : tnode; report_errors: boolean) : boolean;
  1184. begin
  1185. valid_for_formal_var:=valid_for_assign(p,[valid_void],report_errors);
  1186. end;
  1187. function valid_for_formal_const(p : tnode; report_errors: boolean) : boolean;
  1188. begin
  1189. valid_for_formal_const:=(p.resulttype.def.deftype=formaldef) or
  1190. valid_for_assign(p,[valid_void,valid_const],report_errors);
  1191. end;
  1192. function valid_for_assignment(p:tnode; report_errors: boolean):boolean;
  1193. begin
  1194. valid_for_assignment:=valid_for_assign(p,[valid_property],report_errors);
  1195. end;
  1196. function valid_for_addr(p : tnode; report_errors: boolean) : boolean;
  1197. begin
  1198. result:=valid_for_assign(p,[valid_const,valid_addr,valid_void],report_errors);
  1199. end;
  1200. procedure var_para_allowed(var eq:tequaltype;def_from,def_to:Tdef);
  1201. begin
  1202. { Note: eq must be already valid, it will only be updated! }
  1203. case def_to.deftype of
  1204. formaldef :
  1205. begin
  1206. { all types can be passed to a formaldef }
  1207. eq:=te_equal;
  1208. end;
  1209. orddef :
  1210. begin
  1211. { allows conversion from word to integer and
  1212. byte to shortint, but only for TP7 compatibility }
  1213. if (m_tp7 in aktmodeswitches) and
  1214. (def_from.deftype=orddef) and
  1215. (def_from.size=def_to.size) then
  1216. eq:=te_convert_l1;
  1217. end;
  1218. arraydef :
  1219. begin
  1220. if is_open_array(def_to) and
  1221. is_dynamic_array(def_from) and
  1222. equal_defs(tarraydef(def_from).elementtype.def,tarraydef(def_to).elementtype.def) then
  1223. eq:=te_convert_l2;
  1224. end;
  1225. pointerdef :
  1226. begin
  1227. { an implicit pointer conversion is allowed }
  1228. if (def_from.deftype=pointerdef) then
  1229. eq:=te_convert_l1;
  1230. end;
  1231. stringdef :
  1232. begin
  1233. { all shortstrings are allowed, size is not important }
  1234. if is_shortstring(def_from) and
  1235. is_shortstring(def_to) then
  1236. eq:=te_equal;
  1237. end;
  1238. objectdef :
  1239. begin
  1240. { child objects can be also passed }
  1241. { in non-delphi mode, otherwise }
  1242. { they must match exactly, except }
  1243. { if they are objects }
  1244. if (def_from.deftype=objectdef) and
  1245. (
  1246. not(m_delphi in aktmodeswitches) or
  1247. (
  1248. (tobjectdef(def_from).objecttype=odt_object) and
  1249. (tobjectdef(def_to).objecttype=odt_object)
  1250. )
  1251. ) and
  1252. (tobjectdef(def_from).is_related(tobjectdef(def_to))) then
  1253. eq:=te_convert_l1;
  1254. end;
  1255. filedef :
  1256. begin
  1257. { an implicit file conversion is also allowed }
  1258. { from a typed file to an untyped one }
  1259. if (def_from.deftype=filedef) and
  1260. (tfiledef(def_from).filetyp = ft_typed) and
  1261. (tfiledef(def_to).filetyp = ft_untyped) then
  1262. eq:=te_convert_l1;
  1263. end;
  1264. end;
  1265. end;
  1266. procedure para_allowed(var eq:tequaltype;p:tcallparanode;def_to:tdef);
  1267. begin
  1268. { Note: eq must be already valid, it will only be updated! }
  1269. case def_to.deftype of
  1270. formaldef :
  1271. begin
  1272. { all types can be passed to a formaldef }
  1273. eq:=te_equal;
  1274. end;
  1275. stringdef :
  1276. begin
  1277. { to support ansi/long/wide strings in a proper way }
  1278. { string and string[10] are assumed as equal }
  1279. { when searching the correct overloaded procedure }
  1280. if (p.resulttype.def.deftype=stringdef) and
  1281. (tstringdef(def_to).string_typ=tstringdef(p.resulttype.def).string_typ) then
  1282. eq:=te_equal
  1283. else
  1284. { Passing a constant char to ansistring or shortstring or
  1285. a widechar to widestring then handle it as equal. }
  1286. if (p.left.nodetype=ordconstn) and
  1287. (
  1288. is_char(p.resulttype.def) and
  1289. (is_shortstring(def_to) or is_ansistring(def_to))
  1290. ) or
  1291. (
  1292. is_widechar(p.resulttype.def) and
  1293. is_widestring(def_to)
  1294. ) then
  1295. eq:=te_equal
  1296. end;
  1297. setdef :
  1298. begin
  1299. { set can also be a not yet converted array constructor }
  1300. if (p.resulttype.def.deftype=arraydef) and
  1301. (tarraydef(p.resulttype.def).IsConstructor) and
  1302. not(tarraydef(p.resulttype.def).IsVariant) then
  1303. eq:=te_equal;
  1304. end;
  1305. procvardef :
  1306. begin
  1307. { in tp7 mode proc -> procvar is allowed }
  1308. if ((m_tp_procvar in aktmodeswitches) or
  1309. (m_mac_procvar in aktmodeswitches)) and
  1310. (p.left.nodetype=calln) and
  1311. (proc_to_procvar_equal(tprocdef(tcallnode(p.left).procdefinition),tprocvardef(def_to))>=te_equal) then
  1312. eq:=te_equal
  1313. else
  1314. if (m_mac_procvar in aktmodeswitches) and
  1315. is_procvar_load(p.left) then
  1316. eq:=te_convert_l2;
  1317. end;
  1318. end;
  1319. end;
  1320. function allowenumop(nt:tnodetype):boolean;
  1321. begin
  1322. result:=(nt in [equaln,unequaln,ltn,lten,gtn,gten]) or
  1323. ((cs_allow_enum_calc in aktlocalswitches) and
  1324. (nt in [addn,subn]));
  1325. end;
  1326. {****************************************************************************
  1327. TCallCandidates
  1328. ****************************************************************************}
  1329. constructor tcallcandidates.create(sym:tprocsym;st:tsymtable;ppn:tnode;isprop,ignorevis : boolean);
  1330. var
  1331. j : integer;
  1332. pd : tprocdef;
  1333. hp : pcandidate;
  1334. found,
  1335. has_overload_directive : boolean;
  1336. topclassh : tobjectdef;
  1337. srsymtable : tsymtable;
  1338. srprocsym : tprocsym;
  1339. pt : tcallparanode;
  1340. checkstack : psymtablestackitem;
  1341. begin
  1342. if not assigned(sym) then
  1343. internalerror(200411015);
  1344. FProcSym:=sym;
  1345. FProcs:=nil;
  1346. FProccnt:=0;
  1347. FProcvisiblecnt:=0;
  1348. FParanode:=ppn;
  1349. FAllowVariant:=true;
  1350. { determine length of parameter list }
  1351. pt:=tcallparanode(ppn);
  1352. FParalength:=0;
  1353. while assigned(pt) do
  1354. begin
  1355. inc(FParalength);
  1356. pt:=tcallparanode(pt.right);
  1357. end;
  1358. { when the definition has overload directive set, we search for
  1359. overloaded definitions in the class, this only needs to be done once
  1360. for class entries as the tree keeps always the same }
  1361. if (not sym.overloadchecked) and
  1362. (sym.owner.symtabletype=objectsymtable) and
  1363. (po_overload in sym.first_procdef.procoptions) then
  1364. search_class_overloads(sym);
  1365. { when the class passed is defined in this unit we
  1366. need to use the scope of that class. This is a trick
  1367. that can be used to access protected members in other
  1368. units. At least kylix supports it this way (PFV) }
  1369. if assigned(st) and
  1370. (
  1371. (st.symtabletype=objectsymtable) or
  1372. ((st.symtabletype=withsymtable) and
  1373. (st.defowner.deftype=objectdef))
  1374. ) and
  1375. (st.defowner.owner.symtabletype in [globalsymtable,staticsymtable]) and
  1376. st.defowner.owner.iscurrentunit then
  1377. topclassh:=tobjectdef(st.defowner)
  1378. else
  1379. begin
  1380. if assigned(current_procinfo) then
  1381. topclassh:=current_procinfo.procdef._class
  1382. else
  1383. topclassh:=nil;
  1384. end;
  1385. { link all procedures which have the same # of parameters }
  1386. for j:=1 to sym.procdef_count do
  1387. begin
  1388. pd:=sym.procdef[j];
  1389. { Is the procdef visible? This needs to be checked on
  1390. procdef level since a symbol can contain both private and
  1391. public declarations. But the check should not be done
  1392. when the callnode is generated by a property
  1393. inherited overrides invisible anonymous inherited (FK) }
  1394. if isprop or ignorevis or
  1395. (pd.owner.symtabletype<>objectsymtable) or
  1396. pd.is_visible_for_object(topclassh) then
  1397. begin
  1398. { we have at least one procedure that is visible }
  1399. inc(FProcvisiblecnt);
  1400. { only when the # of parameter are supported by the
  1401. procedure }
  1402. if (FParalength>=pd.minparacount) and
  1403. ((po_varargs in pd.procoptions) or { varargs }
  1404. (FParalength<=pd.maxparacount)) then
  1405. proc_add(pd);
  1406. end;
  1407. end;
  1408. { remember if the procedure is declared with the overload directive,
  1409. it's information is still needed also after all procs are removed }
  1410. has_overload_directive:=(po_overload in sym.first_procdef.procoptions);
  1411. { when the definition has overload directive set, we search for
  1412. overloaded definitions in the symtablestack. The found
  1413. entries are only added to the procs list and not the procsym, because
  1414. the list can change in every situation }
  1415. if has_overload_directive and
  1416. (sym.owner.symtabletype<>objectsymtable) then
  1417. begin
  1418. srsymtable:=sym.owner;
  1419. checkstack:=symtablestack.stack;
  1420. while assigned(checkstack) and
  1421. (checkstack^.symtable<>srsymtable) do
  1422. checkstack:=checkstack^.next;
  1423. { we've already processed the current symtable, start with
  1424. the next symtable in the stack }
  1425. if assigned(checkstack) then
  1426. checkstack:=checkstack^.next;
  1427. while assigned(checkstack) do
  1428. begin
  1429. srsymtable:=checkstack^.symtable;
  1430. if srsymtable.symtabletype in [localsymtable,staticsymtable,globalsymtable] then
  1431. begin
  1432. srprocsym:=tprocsym(srsymtable.speedsearch(sym.name,sym.speedvalue));
  1433. if assigned(srprocsym) and
  1434. (srprocsym.typ=procsym) then
  1435. begin
  1436. { if this visible procedure doesn't have overload we can stop
  1437. searching }
  1438. if not(po_overload in srprocsym.first_procdef.procoptions) and
  1439. srprocsym.first_procdef.is_visible_for_object(topclassh) then
  1440. break;
  1441. { process all overloaded definitions }
  1442. for j:=1 to srprocsym.procdef_count do
  1443. begin
  1444. pd:=srprocsym.procdef[j];
  1445. { only visible procedures need to be added }
  1446. if pd.is_visible_for_object(topclassh) then
  1447. begin
  1448. { only when the # of parameter are supported by the
  1449. procedure }
  1450. if (FParalength>=pd.minparacount) and
  1451. ((po_varargs in pd.procoptions) or { varargs }
  1452. (FParalength<=pd.maxparacount)) then
  1453. begin
  1454. found:=false;
  1455. hp:=FProcs;
  1456. while assigned(hp) do
  1457. begin
  1458. { Only compare visible parameters for the user }
  1459. if compare_paras(hp^.data.paras,pd.paras,cp_value_equal_const,[cpo_ignorehidden])>=te_equal then
  1460. begin
  1461. found:=true;
  1462. break;
  1463. end;
  1464. hp:=hp^.next;
  1465. end;
  1466. if not found then
  1467. proc_add(pd);
  1468. end;
  1469. end;
  1470. end;
  1471. end;
  1472. end;
  1473. checkstack:=checkstack^.next;
  1474. end;
  1475. end;
  1476. end;
  1477. constructor tcallcandidates.create_operator(op:ttoken;ppn:tnode);
  1478. var
  1479. j : integer;
  1480. pd : tprocdef;
  1481. hp : pcandidate;
  1482. found : boolean;
  1483. srsymtable : tsymtable;
  1484. srprocsym : tprocsym;
  1485. pt : tcallparanode;
  1486. sv : cardinal;
  1487. checkstack : psymtablestackitem;
  1488. begin
  1489. FProcSym:=nil;
  1490. FProcs:=nil;
  1491. FProccnt:=0;
  1492. FProcvisiblecnt:=0;
  1493. FParanode:=ppn;
  1494. FAllowVariant:=false;
  1495. { determine length of parameter list }
  1496. pt:=tcallparanode(ppn);
  1497. FParalength:=0;
  1498. while assigned(pt) do
  1499. begin
  1500. if pt.resulttype.def.deftype=variantdef then
  1501. FAllowVariant:=true;
  1502. inc(FParalength);
  1503. pt:=tcallparanode(pt.right);
  1504. end;
  1505. { we search all overloaded operator definitions in the symtablestack. The found
  1506. entries are only added to the procs list and not the procsym, because
  1507. the list can change in every situation }
  1508. sv:=getspeedvalue(overloaded_names[op]);
  1509. checkstack:=symtablestack.stack;
  1510. while assigned(checkstack) do
  1511. begin
  1512. srsymtable:=checkstack^.symtable;
  1513. if srsymtable.symtabletype in [localsymtable,staticsymtable,globalsymtable] then
  1514. begin
  1515. srprocsym:=tprocsym(srsymtable.speedsearch(overloaded_names[op],sv));
  1516. if assigned(srprocsym) and
  1517. (srprocsym.typ=procsym) then
  1518. begin
  1519. { Store first procsym found }
  1520. if not assigned(FProcsym) then
  1521. FProcsym:=srprocsym;
  1522. { process all overloaded definitions }
  1523. for j:=1 to srprocsym.procdef_count do
  1524. begin
  1525. pd:=srprocsym.procdef[j];
  1526. { only when the # of parameter are supported by the
  1527. procedure }
  1528. if (FParalength>=pd.minparacount) and
  1529. (FParalength<=pd.maxparacount) then
  1530. begin
  1531. found:=false;
  1532. hp:=FProcs;
  1533. while assigned(hp) do
  1534. begin
  1535. { Only compare visible parameters for the user }
  1536. if compare_paras(hp^.data.paras,pd.paras,cp_value_equal_const,[cpo_ignorehidden])>=te_equal then
  1537. begin
  1538. found:=true;
  1539. break;
  1540. end;
  1541. hp:=hp^.next;
  1542. end;
  1543. if not found then
  1544. proc_add(pd);
  1545. end;
  1546. end;
  1547. end;
  1548. end;
  1549. checkstack:=checkstack^.next;
  1550. end;
  1551. end;
  1552. destructor tcallcandidates.destroy;
  1553. var
  1554. hpnext,
  1555. hp : pcandidate;
  1556. begin
  1557. hp:=FProcs;
  1558. while assigned(hp) do
  1559. begin
  1560. hpnext:=hp^.next;
  1561. dispose(hp);
  1562. hp:=hpnext;
  1563. end;
  1564. end;
  1565. function tcallcandidates.proc_add(pd:tprocdef):pcandidate;
  1566. var
  1567. defaultparacnt : integer;
  1568. begin
  1569. { generate new candidate entry }
  1570. new(result);
  1571. fillchar(result^,sizeof(tcandidate),0);
  1572. result^.data:=pd;
  1573. result^.next:=FProcs;
  1574. FProcs:=result;
  1575. inc(FProccnt);
  1576. { Find last parameter, skip all default parameters
  1577. that are not passed. Ignore this skipping for varargs }
  1578. result^.firstparaidx:=pd.paras.count-1;
  1579. if not(po_varargs in pd.procoptions) then
  1580. begin
  1581. { ignore hidden parameters }
  1582. while (result^.firstparaidx>=0) and (vo_is_hidden_para in tparavarsym(pd.paras[result^.firstparaidx]).varoptions) do
  1583. dec(result^.firstparaidx);
  1584. defaultparacnt:=pd.maxparacount-FParalength;
  1585. if defaultparacnt>0 then
  1586. begin
  1587. if defaultparacnt>result^.firstparaidx+1 then
  1588. internalerror(200401141);
  1589. dec(result^.firstparaidx,defaultparacnt);
  1590. end;
  1591. end;
  1592. end;
  1593. procedure tcallcandidates.list(all:boolean);
  1594. var
  1595. hp : pcandidate;
  1596. begin
  1597. hp:=FProcs;
  1598. while assigned(hp) do
  1599. begin
  1600. if all or
  1601. (not hp^.invalid) then
  1602. MessagePos1(hp^.data.fileinfo,sym_h_param_list,hp^.data.fullprocname(false));
  1603. hp:=hp^.next;
  1604. end;
  1605. end;
  1606. {$ifdef EXTDEBUG}
  1607. procedure tcallcandidates.dump_info(lvl:longint);
  1608. function ParaTreeStr(p:tcallparanode):string;
  1609. begin
  1610. result:='';
  1611. while assigned(p) do
  1612. begin
  1613. if result<>'' then
  1614. result:=','+result;
  1615. result:=p.resulttype.def.typename+result;
  1616. p:=tcallparanode(p.right);
  1617. end;
  1618. end;
  1619. var
  1620. hp : pcandidate;
  1621. i : integer;
  1622. currpara : tparavarsym;
  1623. begin
  1624. if not CheckVerbosity(lvl) then
  1625. exit;
  1626. Comment(lvl+V_LineInfo,'Overloaded callnode: '+FProcSym.name+'('+ParaTreeStr(tcallparanode(FParaNode))+')');
  1627. hp:=FProcs;
  1628. while assigned(hp) do
  1629. begin
  1630. Comment(lvl,' '+hp^.data.fullprocname(false));
  1631. if (hp^.invalid) then
  1632. Comment(lvl,' invalid')
  1633. else
  1634. begin
  1635. Comment(lvl,' ex: '+tostr(hp^.exact_count)+
  1636. ' eq: '+tostr(hp^.equal_count)+
  1637. ' l1: '+tostr(hp^.cl1_count)+
  1638. ' l2: '+tostr(hp^.cl2_count)+
  1639. ' l3: '+tostr(hp^.cl3_count)+
  1640. ' oper: '+tostr(hp^.coper_count)+
  1641. ' ord: '+realtostr(hp^.ordinal_distance));
  1642. { Print parameters in left-right order }
  1643. for i:=0 to hp^.data.paras.count-1 do
  1644. begin
  1645. currpara:=tparavarsym(hp^.data.paras[i]);
  1646. if (vo_is_hidden_para in currpara.varoptions) then
  1647. Comment(lvl,' - '+currpara.vartype.def.typename+' : '+EqualTypeName[currpara.eqval]);
  1648. end;
  1649. end;
  1650. hp:=hp^.next;
  1651. end;
  1652. end;
  1653. {$endif EXTDEBUG}
  1654. procedure tcallcandidates.get_information;
  1655. var
  1656. hp : pcandidate;
  1657. currpara : tparavarsym;
  1658. paraidx : integer;
  1659. currparanr : byte;
  1660. rfh,rth : bestreal;
  1661. objdef : tobjectdef;
  1662. def_from,
  1663. def_to : tdef;
  1664. currpt,
  1665. pt : tcallparanode;
  1666. eq : tequaltype;
  1667. convtype : tconverttype;
  1668. pdoper : tprocdef;
  1669. releasecurrpt : boolean;
  1670. cdoptions : tcompare_defs_options;
  1671. begin
  1672. cdoptions:=[cdo_check_operator];
  1673. if FAllowVariant then
  1674. include(cdoptions,cdo_allow_variant);
  1675. { process all procs }
  1676. hp:=FProcs;
  1677. while assigned(hp) do
  1678. begin
  1679. { We compare parameters in reverse order (right to left),
  1680. the firstpara is already pointing to the last parameter
  1681. were we need to start comparing }
  1682. currparanr:=FParalength;
  1683. paraidx:=hp^.firstparaidx;
  1684. while (paraidx>=0) and (vo_is_hidden_para in tparavarsym(hp^.data.paras[paraidx]).varoptions) do
  1685. dec(paraidx);
  1686. pt:=tcallparanode(FParaNode);
  1687. while assigned(pt) and (paraidx>=0) do
  1688. begin
  1689. currpara:=tparavarsym(hp^.data.paras[paraidx]);
  1690. { currpt can be changed from loadn to calln when a procvar
  1691. is passed. This is to prevent that the change is permanent }
  1692. currpt:=pt;
  1693. releasecurrpt:=false;
  1694. { retrieve current parameter definitions to compares }
  1695. eq:=te_incompatible;
  1696. def_from:=currpt.resulttype.def;
  1697. def_to:=currpara.vartype.def;
  1698. if not(assigned(def_from)) then
  1699. internalerror(200212091);
  1700. if not(
  1701. assigned(def_to) or
  1702. ((po_varargs in hp^.data.procoptions) and
  1703. (currparanr>hp^.data.minparacount))
  1704. ) then
  1705. internalerror(200212092);
  1706. { Convert tp procvars when not expecting a procvar }
  1707. if (def_to.deftype<>procvardef) and
  1708. (currpt.left.resulttype.def.deftype=procvardef) then
  1709. begin
  1710. releasecurrpt:=true;
  1711. currpt:=tcallparanode(pt.getcopy);
  1712. if maybe_call_procvar(currpt.left,true) then
  1713. begin
  1714. currpt.resulttype:=currpt.left.resulttype;
  1715. def_from:=currpt.left.resulttype.def;
  1716. end;
  1717. end;
  1718. { varargs are always equal, but not exact }
  1719. if (po_varargs in hp^.data.procoptions) and
  1720. (currparanr>hp^.data.minparacount) then
  1721. begin
  1722. eq:=te_equal;
  1723. end
  1724. else
  1725. { same definition -> exact }
  1726. if (def_from=def_to) then
  1727. begin
  1728. eq:=te_exact;
  1729. end
  1730. else
  1731. { for value and const parameters check if a integer is constant or
  1732. included in other integer -> equal and calc ordinal_distance }
  1733. if not(currpara.varspez in [vs_var,vs_out]) and
  1734. is_integer(def_from) and
  1735. is_integer(def_to) and
  1736. is_in_limit(def_from,def_to) then
  1737. begin
  1738. eq:=te_equal;
  1739. hp^.ordinal_distance:=hp^.ordinal_distance+
  1740. abs(bestreal(torddef(def_from).low)-bestreal(torddef(def_to).low));
  1741. if (torddef(def_to).typ=u64bit) then
  1742. rth:=bestreal(qword(torddef(def_to).high))
  1743. else
  1744. rth:=bestreal(torddef(def_to).high);
  1745. if (torddef(def_from).typ=u64bit) then
  1746. rfh:=bestreal(qword(torddef(def_from).high))
  1747. else
  1748. rfh:=bestreal(torddef(def_from).high);
  1749. hp^.ordinal_distance:=hp^.ordinal_distance+abs(rth-rfh);
  1750. { Give wrong sign a small penalty, this is need to get a diffrence
  1751. from word->[longword,longint] }
  1752. if is_signed(def_from)<>is_signed(def_to) then
  1753. hp^.ordinal_distance:=hp^.ordinal_distance+1.0;
  1754. end
  1755. else
  1756. { for value and const parameters check precision of real, give
  1757. penalty for loosing of precision. var and out parameters must match exactly }
  1758. if not(currpara.varspez in [vs_var,vs_out]) and
  1759. is_real(def_from) and
  1760. is_real(def_to) then
  1761. begin
  1762. eq:=te_equal;
  1763. if is_extended(def_to) then
  1764. rth:=bestreal(4)
  1765. else
  1766. if is_double (def_to) then
  1767. rth:=bestreal(2)
  1768. else
  1769. rth:=bestreal(1);
  1770. if is_extended(def_from) then
  1771. rfh:=bestreal(4)
  1772. else
  1773. if is_double (def_from) then
  1774. rfh:=bestreal(2)
  1775. else
  1776. rfh:=bestreal(1);
  1777. { penalty for shrinking of precision }
  1778. if rth<rfh then
  1779. rfh:=(rfh-rth)*16
  1780. else
  1781. rfh:=rth-rfh;
  1782. hp^.ordinal_distance:=hp^.ordinal_distance+rfh;
  1783. end
  1784. else
  1785. { related object parameters also need to determine the distance between the current
  1786. object and the object we are comparing with. var and out parameters must match exactly }
  1787. if not(currpara.varspez in [vs_var,vs_out]) and
  1788. (def_from.deftype=objectdef) and
  1789. (def_to.deftype=objectdef) and
  1790. (tobjectdef(def_from).objecttype=tobjectdef(def_to).objecttype) and
  1791. tobjectdef(def_from).is_related(tobjectdef(def_to)) then
  1792. begin
  1793. eq:=te_convert_l1;
  1794. objdef:=tobjectdef(def_from);
  1795. while assigned(objdef) do
  1796. begin
  1797. if objdef=def_to then
  1798. break;
  1799. hp^.ordinal_distance:=hp^.ordinal_distance+1;
  1800. objdef:=objdef.childof;
  1801. end;
  1802. end
  1803. else
  1804. { generic type comparision }
  1805. begin
  1806. eq:=compare_defs_ext(def_from,def_to,currpt.left.nodetype,convtype,pdoper,cdoptions);
  1807. { when the types are not equal we need to check
  1808. some special case for parameter passing }
  1809. if (eq<te_equal) then
  1810. begin
  1811. if currpara.varspez in [vs_var,vs_out] then
  1812. begin
  1813. { para requires an equal type so the previous found
  1814. match was not good enough, reset to incompatible }
  1815. eq:=te_incompatible;
  1816. { var_para_allowed will return te_equal and te_convert_l1 to
  1817. make a difference for best matching }
  1818. var_para_allowed(eq,currpt.resulttype.def,currpara.vartype.def)
  1819. end
  1820. else
  1821. para_allowed(eq,currpt,def_to);
  1822. end;
  1823. end;
  1824. { when a procvar was changed to a call an exact much is
  1825. downgraded to equal. This way an overload call with the
  1826. procvar is choosen. See tb0471 (PFV) }
  1827. if (pt<>currpt) and (eq=te_exact) then
  1828. eq:=te_equal;
  1829. { increase correct counter }
  1830. case eq of
  1831. te_exact :
  1832. inc(hp^.exact_count);
  1833. te_equal :
  1834. inc(hp^.equal_count);
  1835. te_convert_l1 :
  1836. inc(hp^.cl1_count);
  1837. te_convert_l2 :
  1838. inc(hp^.cl2_count);
  1839. te_convert_l3 :
  1840. inc(hp^.cl3_count);
  1841. te_convert_operator :
  1842. inc(hp^.coper_count);
  1843. te_incompatible :
  1844. hp^.invalid:=true;
  1845. else
  1846. internalerror(200212072);
  1847. end;
  1848. { stop checking when an incompatible parameter is found }
  1849. if hp^.invalid then
  1850. begin
  1851. { store the current parameter info for
  1852. a nice error message when no procedure is found }
  1853. hp^.wrongparaidx:=paraidx;
  1854. hp^.wrongparanr:=currparanr;
  1855. break;
  1856. end;
  1857. {$ifdef EXTDEBUG}
  1858. { store equal in node tree for dump }
  1859. currpara.eqval:=eq;
  1860. {$endif EXTDEBUG}
  1861. { maybe release temp currpt }
  1862. if releasecurrpt then
  1863. currpt.free;
  1864. { next parameter in the call tree }
  1865. pt:=tcallparanode(pt.right);
  1866. { next parameter for definition, only goto next para
  1867. if we're out of the varargs }
  1868. if not(po_varargs in hp^.data.procoptions) or
  1869. (currparanr<=hp^.data.maxparacount) then
  1870. begin
  1871. { Ignore vs_hidden parameters }
  1872. repeat
  1873. dec(paraidx);
  1874. until (paraidx<0) or not(vo_is_hidden_para in tparavarsym(hp^.data.paras[paraidx]).varoptions);
  1875. end;
  1876. dec(currparanr);
  1877. end;
  1878. if not(hp^.invalid) and
  1879. (assigned(pt) or (paraidx>=0) or (currparanr<>0)) then
  1880. internalerror(200212141);
  1881. { next candidate }
  1882. hp:=hp^.next;
  1883. end;
  1884. end;
  1885. function is_better_candidate(currpd,bestpd:pcandidate):integer;
  1886. var
  1887. res : integer;
  1888. begin
  1889. {
  1890. Return values:
  1891. > 0 when currpd is better than bestpd
  1892. < 0 when bestpd is better than currpd
  1893. = 0 when both are equal
  1894. To choose the best candidate we use the following order:
  1895. - Incompatible flag
  1896. - (Smaller) Number of convert operator parameters.
  1897. - (Smaller) Number of convertlevel 2 parameters.
  1898. - (Smaller) Number of convertlevel 1 parameters.
  1899. - (Bigger) Number of exact parameters.
  1900. - (Smaller) Number of equal parameters.
  1901. - (Smaller) Total of ordinal distance. For example, the distance of a word
  1902. to a byte is 65535-255=65280.
  1903. }
  1904. if bestpd^.invalid then
  1905. begin
  1906. if currpd^.invalid then
  1907. res:=0
  1908. else
  1909. res:=1;
  1910. end
  1911. else
  1912. if currpd^.invalid then
  1913. res:=-1
  1914. else
  1915. begin
  1916. { less operator parameters? }
  1917. res:=(bestpd^.coper_count-currpd^.coper_count);
  1918. if (res=0) then
  1919. begin
  1920. { less cl3 parameters? }
  1921. res:=(bestpd^.cl3_count-currpd^.cl3_count);
  1922. if (res=0) then
  1923. begin
  1924. { less cl2 parameters? }
  1925. res:=(bestpd^.cl2_count-currpd^.cl2_count);
  1926. if (res=0) then
  1927. begin
  1928. { less cl1 parameters? }
  1929. res:=(bestpd^.cl1_count-currpd^.cl1_count);
  1930. if (res=0) then
  1931. begin
  1932. { more exact parameters? }
  1933. res:=(currpd^.exact_count-bestpd^.exact_count);
  1934. if (res=0) then
  1935. begin
  1936. { less equal parameters? }
  1937. res:=(bestpd^.equal_count-currpd^.equal_count);
  1938. if (res=0) then
  1939. begin
  1940. { smaller ordinal distance? }
  1941. if (currpd^.ordinal_distance<bestpd^.ordinal_distance) then
  1942. res:=1
  1943. else
  1944. if (currpd^.ordinal_distance>bestpd^.ordinal_distance) then
  1945. res:=-1
  1946. else
  1947. res:=0;
  1948. end;
  1949. end;
  1950. end;
  1951. end;
  1952. end;
  1953. end;
  1954. end;
  1955. is_better_candidate:=res;
  1956. end;
  1957. function tcallcandidates.choose_best(var bestpd:tabstractprocdef):integer;
  1958. var
  1959. besthpstart,
  1960. hp : pcandidate;
  1961. cntpd,
  1962. res : integer;
  1963. begin
  1964. {
  1965. Returns the number of candidates left and the
  1966. first candidate is returned in pdbest
  1967. }
  1968. { Setup the first procdef as best, only count it as a result
  1969. when it is valid }
  1970. bestpd:=FProcs^.data;
  1971. if FProcs^.invalid then
  1972. cntpd:=0
  1973. else
  1974. cntpd:=1;
  1975. if assigned(FProcs^.next) then
  1976. begin
  1977. besthpstart:=FProcs;
  1978. hp:=FProcs^.next;
  1979. while assigned(hp) do
  1980. begin
  1981. res:=is_better_candidate(hp,besthpstart);
  1982. if (res>0) then
  1983. begin
  1984. { hp is better, flag all procs to be incompatible }
  1985. while (besthpstart<>hp) do
  1986. begin
  1987. besthpstart^.invalid:=true;
  1988. besthpstart:=besthpstart^.next;
  1989. end;
  1990. { besthpstart is already set to hp }
  1991. bestpd:=besthpstart^.data;
  1992. cntpd:=1;
  1993. end
  1994. else
  1995. if (res<0) then
  1996. begin
  1997. { besthpstart is better, flag current hp to be incompatible }
  1998. hp^.invalid:=true;
  1999. end
  2000. else
  2001. begin
  2002. { res=0, both are valid }
  2003. if not hp^.invalid then
  2004. inc(cntpd);
  2005. end;
  2006. hp:=hp^.next;
  2007. end;
  2008. end;
  2009. result:=cntpd;
  2010. end;
  2011. procedure tcallcandidates.find_wrong_para;
  2012. var
  2013. currparanr : smallint;
  2014. hp : pcandidate;
  2015. pt : tcallparanode;
  2016. wrongpara : tparavarsym;
  2017. begin
  2018. { Only process the first overloaded procdef }
  2019. hp:=FProcs;
  2020. { Find callparanode corresponding to the argument }
  2021. pt:=tcallparanode(FParanode);
  2022. currparanr:=FParalength;
  2023. while assigned(pt) and
  2024. (currparanr>hp^.wrongparanr) do
  2025. begin
  2026. pt:=tcallparanode(pt.right);
  2027. dec(currparanr);
  2028. end;
  2029. if (currparanr<>hp^.wrongparanr) or
  2030. not assigned(pt) then
  2031. internalerror(200212094);
  2032. { Show error message, when it was a var or out parameter
  2033. guess that it is a missing typeconv }
  2034. wrongpara:=tparavarsym(hp^.data.paras[hp^.wrongparaidx]);
  2035. if wrongpara.varspez in [vs_var,vs_out] then
  2036. begin
  2037. { Maybe passing the correct type but passing a const to var parameter }
  2038. if (compare_defs(pt.resulttype.def,wrongpara.vartype.def,pt.nodetype)<>te_incompatible) and
  2039. not valid_for_var(pt.left,true) then
  2040. CGMessagePos(pt.left.fileinfo,type_e_variable_id_expected)
  2041. else
  2042. CGMessagePos2(pt.left.fileinfo,parser_e_call_by_ref_without_typeconv,
  2043. FullTypeName(pt.left.resulttype.def,wrongpara.vartype.def),
  2044. FullTypeName(wrongpara.vartype.def,pt.left.resulttype.def))
  2045. end
  2046. else
  2047. CGMessagePos3(pt.left.fileinfo,type_e_wrong_parameter_type,tostr(hp^.wrongparanr),
  2048. FullTypeName(pt.left.resulttype.def,wrongpara.vartype.def),
  2049. FullTypeName(wrongpara.vartype.def,pt.left.resulttype.def));
  2050. end;
  2051. procedure check_hints(const srsym: tsym; const symoptions: tsymoptions);
  2052. begin
  2053. if not assigned(srsym) then
  2054. internalerror(200602051);
  2055. if sp_hint_deprecated in symoptions then
  2056. Message1(sym_w_deprecated_symbol,srsym.realname);
  2057. if sp_hint_platform in symoptions then
  2058. Message1(sym_w_non_portable_symbol,srsym.realname);
  2059. if sp_hint_unimplemented in symoptions then
  2060. Message1(sym_w_non_implemented_symbol,srsym.realname);
  2061. end;
  2062. end.