htypechk.pas 87 KB

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