htypechk.pas 85 KB

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