htypechk.pas 84 KB

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