htypechk.pas 86 KB

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