htypechk.pas 87 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339
  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. dataconstn:
  1151. begin
  1152. { only created internally, so no additional checks necessary }
  1153. result:=true;
  1154. exit;
  1155. end;
  1156. loadn :
  1157. begin
  1158. case tloadnode(hp).symtableentry.typ of
  1159. absolutevarsym,
  1160. globalvarsym,
  1161. localvarsym,
  1162. paravarsym :
  1163. begin
  1164. { loop counter? }
  1165. if not(Valid_Const in opts) and
  1166. not gotderef and
  1167. (vo_is_loop_counter in tabstractvarsym(tloadnode(hp).symtableentry).varoptions) then
  1168. if report_errors then
  1169. CGMessage1(parser_e_illegal_assignment_to_count_var,tloadnode(hp).symtableentry.realname)
  1170. else
  1171. exit;
  1172. { derefed pointer }
  1173. if (tabstractvarsym(tloadnode(hp).symtableentry).varspez=vs_const) then
  1174. begin
  1175. { allow p^:= constructions with p is const parameter }
  1176. if gotderef or gotdynarray or (Valid_Const in opts) or
  1177. (nf_isinternal_ignoreconst in tloadnode(hp).flags) then
  1178. result:=true
  1179. else
  1180. if report_errors then
  1181. CGMessagePos(tloadnode(hp).fileinfo,type_e_no_assign_to_const);
  1182. exit;
  1183. end;
  1184. result:=true;
  1185. exit;
  1186. end;
  1187. typedconstsym :
  1188. begin
  1189. if ttypedconstsym(tloadnode(hp).symtableentry).is_writable or
  1190. (valid_addr in opts) or
  1191. (valid_const in opts) then
  1192. result:=true
  1193. else
  1194. if report_errors then
  1195. CGMessagePos(hp.fileinfo,type_e_no_assign_to_const);
  1196. exit;
  1197. end;
  1198. procsym :
  1199. begin
  1200. if (Valid_Const in opts) then
  1201. result:=true
  1202. else
  1203. if report_errors then
  1204. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  1205. exit;
  1206. end;
  1207. labelsym :
  1208. begin
  1209. if (Valid_Addr in opts) then
  1210. result:=true
  1211. else
  1212. if report_errors then
  1213. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  1214. exit;
  1215. end;
  1216. constsym:
  1217. begin
  1218. if (tconstsym(tloadnode(hp).symtableentry).consttyp=constresourcestring) and
  1219. (valid_addr in opts) then
  1220. result:=true
  1221. else
  1222. if report_errors then
  1223. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  1224. exit;
  1225. end;
  1226. else
  1227. begin
  1228. if report_errors then
  1229. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  1230. exit;
  1231. end;
  1232. end;
  1233. end;
  1234. else
  1235. begin
  1236. if report_errors then
  1237. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  1238. exit;
  1239. end;
  1240. end;
  1241. end;
  1242. end;
  1243. function valid_for_var(p:tnode; report_errors: boolean):boolean;
  1244. begin
  1245. valid_for_var:=valid_for_assign(p,[],report_errors);
  1246. end;
  1247. function valid_for_formal_var(p : tnode; report_errors: boolean) : boolean;
  1248. begin
  1249. valid_for_formal_var:=valid_for_assign(p,[valid_void],report_errors);
  1250. end;
  1251. function valid_for_formal_const(p : tnode; report_errors: boolean) : boolean;
  1252. begin
  1253. valid_for_formal_const:=(p.resultdef.typ=formaldef) or
  1254. valid_for_assign(p,[valid_void,valid_const],report_errors);
  1255. end;
  1256. function valid_for_assignment(p:tnode; report_errors: boolean):boolean;
  1257. begin
  1258. valid_for_assignment:=valid_for_assign(p,[valid_property,valid_packed],report_errors);
  1259. end;
  1260. function valid_for_loopvar(p:tnode; report_errors: boolean):boolean;
  1261. begin
  1262. valid_for_loopvar:=valid_for_assign(p,[valid_property],report_errors);
  1263. end;
  1264. function valid_for_addr(p : tnode; report_errors: boolean) : boolean;
  1265. begin
  1266. result:=valid_for_assign(p,[valid_const,valid_addr,valid_void],report_errors);
  1267. end;
  1268. procedure var_para_allowed(var eq:tequaltype;def_from,def_to:Tdef);
  1269. begin
  1270. { Note: eq must be already valid, it will only be updated! }
  1271. case def_to.typ of
  1272. formaldef :
  1273. begin
  1274. { all types can be passed to a formaldef,
  1275. but it is not the prefered way }
  1276. eq:=te_convert_l2;
  1277. end;
  1278. orddef :
  1279. begin
  1280. { allows conversion from word to integer and
  1281. byte to shortint, but only for TP7 compatibility }
  1282. if (m_tp7 in current_settings.modeswitches) and
  1283. (def_from.typ=orddef) and
  1284. (def_from.size=def_to.size) then
  1285. eq:=te_convert_l1;
  1286. end;
  1287. arraydef :
  1288. begin
  1289. if is_open_array(def_to) then
  1290. begin
  1291. if is_dynamic_array(def_from) and
  1292. equal_defs(tarraydef(def_from).elementdef,tarraydef(def_to).elementdef) then
  1293. eq:=te_convert_l2
  1294. else
  1295. if equal_defs(def_from,tarraydef(def_to).elementdef) then
  1296. eq:=te_convert_l2;
  1297. end;
  1298. end;
  1299. pointerdef :
  1300. begin
  1301. { an implicit pointer conversion is allowed }
  1302. if (def_from.typ=pointerdef) then
  1303. eq:=te_convert_l1;
  1304. end;
  1305. stringdef :
  1306. begin
  1307. { all shortstrings are allowed, size is not important }
  1308. if is_shortstring(def_from) and
  1309. is_shortstring(def_to) then
  1310. eq:=te_equal;
  1311. end;
  1312. objectdef :
  1313. begin
  1314. { child objects can be also passed }
  1315. { in non-delphi mode, otherwise }
  1316. { they must match exactly, except }
  1317. { if they are objects }
  1318. if (def_from.typ=objectdef) and
  1319. (
  1320. not(m_delphi in current_settings.modeswitches) or
  1321. (
  1322. (tobjectdef(def_from).objecttype=odt_object) and
  1323. (tobjectdef(def_to).objecttype=odt_object)
  1324. )
  1325. ) and
  1326. (tobjectdef(def_from).is_related(tobjectdef(def_to))) then
  1327. eq:=te_convert_l1;
  1328. end;
  1329. filedef :
  1330. begin
  1331. { an implicit file conversion is also allowed }
  1332. { from a typed file to an untyped one }
  1333. if (def_from.typ=filedef) and
  1334. (tfiledef(def_from).filetyp = ft_typed) and
  1335. (tfiledef(def_to).filetyp = ft_untyped) then
  1336. eq:=te_convert_l1;
  1337. end;
  1338. end;
  1339. end;
  1340. procedure para_allowed(var eq:tequaltype;p:tcallparanode;def_to:tdef);
  1341. begin
  1342. { Note: eq must be already valid, it will only be updated! }
  1343. case def_to.typ of
  1344. formaldef :
  1345. begin
  1346. { all types can be passed to a formaldef }
  1347. eq:=te_equal;
  1348. end;
  1349. stringdef :
  1350. begin
  1351. { to support ansi/long/wide strings in a proper way }
  1352. { string and string[10] are assumed as equal }
  1353. { when searching the correct overloaded procedure }
  1354. if (p.resultdef.typ=stringdef) and
  1355. (tstringdef(def_to).stringtype=tstringdef(p.resultdef).stringtype) then
  1356. eq:=te_equal
  1357. else
  1358. { Passing a constant char to ansistring or shortstring or
  1359. a widechar to widestring then handle it as equal. }
  1360. if (p.left.nodetype=ordconstn) and
  1361. (
  1362. is_char(p.resultdef) and
  1363. (is_shortstring(def_to) or is_ansistring(def_to))
  1364. ) or
  1365. (
  1366. is_widechar(p.resultdef) and
  1367. is_widestring(def_to)
  1368. ) then
  1369. eq:=te_equal
  1370. end;
  1371. setdef :
  1372. begin
  1373. { set can also be a not yet converted array constructor }
  1374. if (p.resultdef.typ=arraydef) and
  1375. is_array_constructor(p.resultdef) and
  1376. not is_variant_array(p.resultdef) then
  1377. eq:=te_equal;
  1378. end;
  1379. procvardef :
  1380. begin
  1381. { in tp7 mode proc -> procvar is allowed }
  1382. if ((m_tp_procvar in current_settings.modeswitches) or
  1383. (m_mac_procvar in current_settings.modeswitches)) and
  1384. (p.left.nodetype=calln) and
  1385. (proc_to_procvar_equal(tprocdef(tcallnode(p.left).procdefinition),tprocvardef(def_to))>=te_equal) then
  1386. eq:=te_equal
  1387. else
  1388. if (m_mac_procvar in current_settings.modeswitches) and
  1389. is_procvar_load(p.left) then
  1390. eq:=te_convert_l2;
  1391. end;
  1392. end;
  1393. end;
  1394. function allowenumop(nt:tnodetype):boolean;
  1395. begin
  1396. result:=(nt in [equaln,unequaln,ltn,lten,gtn,gten]) or
  1397. ((cs_allow_enum_calc in current_settings.localswitches) and
  1398. (nt in [addn,subn]));
  1399. end;
  1400. {****************************************************************************
  1401. TCallCandidates
  1402. ****************************************************************************}
  1403. constructor tcallcandidates.create(sym:tprocsym;st:TSymtable;ppn:tnode;isprop,ignorevis : boolean);
  1404. var
  1405. j : integer;
  1406. pd : tprocdef;
  1407. hp : pcandidate;
  1408. found,
  1409. has_overload_directive : boolean;
  1410. topclassh : tobjectdef;
  1411. srsymtable : TSymtable;
  1412. srprocsym : tprocsym;
  1413. pt : tcallparanode;
  1414. checkstack : psymtablestackitem;
  1415. hashedid : THashedIDString;
  1416. begin
  1417. if not assigned(sym) then
  1418. internalerror(200411015);
  1419. FProcSym:=sym;
  1420. FProcs:=nil;
  1421. FProccnt:=0;
  1422. FProcvisiblecnt:=0;
  1423. FParanode:=ppn;
  1424. FAllowVariant:=true;
  1425. { determine length of parameter list }
  1426. pt:=tcallparanode(ppn);
  1427. FParalength:=0;
  1428. while assigned(pt) do
  1429. begin
  1430. inc(FParalength);
  1431. pt:=tcallparanode(pt.right);
  1432. end;
  1433. { when the definition has overload directive set, we search for
  1434. overloaded definitions in the class, this only needs to be done once
  1435. for class entries as the tree keeps always the same }
  1436. if (not sym.overloadchecked) and
  1437. (sym.owner.symtabletype=ObjectSymtable) and
  1438. (po_overload in tprocdef(sym.ProcdefList[0]).procoptions) then
  1439. search_class_overloads(sym);
  1440. { when the class passed is defined in this unit we
  1441. need to use the scope of that class. This is a trick
  1442. that can be used to access protected members in other
  1443. units. At least kylix supports it this way (PFV) }
  1444. if assigned(st) and
  1445. (
  1446. (st.symtabletype=ObjectSymtable) or
  1447. ((st.symtabletype=withsymtable) and
  1448. (st.defowner.typ=objectdef))
  1449. ) and
  1450. (st.defowner.owner.symtabletype in [globalsymtable,staticsymtable]) and
  1451. st.defowner.owner.iscurrentunit then
  1452. topclassh:=tobjectdef(st.defowner)
  1453. else
  1454. begin
  1455. if assigned(current_procinfo) then
  1456. topclassh:=current_procinfo.procdef._class
  1457. else
  1458. topclassh:=nil;
  1459. end;
  1460. { link all procedures which have the same # of parameters }
  1461. for j:=0 to sym.ProcdefList.Count-1 do
  1462. begin
  1463. pd:=tprocdef(sym.ProcdefList[j]);
  1464. { Is the procdef visible? This needs to be checked on
  1465. procdef level since a symbol can contain both private and
  1466. public declarations. But the check should not be done
  1467. when the callnode is generated by a property
  1468. inherited overrides invisible anonymous inherited (FK) }
  1469. if isprop or ignorevis or
  1470. (pd.owner.symtabletype<>ObjectSymtable) or
  1471. pd.is_visible_for_object(topclassh,nil) then
  1472. begin
  1473. { we have at least one procedure that is visible }
  1474. inc(FProcvisiblecnt);
  1475. { only when the # of parameter are supported by the
  1476. procedure }
  1477. if (FParalength>=pd.minparacount) and
  1478. ((po_varargs in pd.procoptions) or { varargs }
  1479. (FParalength<=pd.maxparacount)) then
  1480. proc_add(pd);
  1481. end;
  1482. end;
  1483. { remember if the procedure is declared with the overload directive,
  1484. it's information is still needed also after all procs are removed }
  1485. has_overload_directive:=(po_overload in tprocdef(sym.ProcdefList[0]).procoptions);
  1486. { when the definition has overload directive set, we search for
  1487. overloaded definitions in the symtablestack. The found
  1488. entries are only added to the procs list and not the procsym, because
  1489. the list can change in every situation }
  1490. if has_overload_directive and
  1491. (sym.owner.symtabletype<>ObjectSymtable) then
  1492. begin
  1493. srsymtable:=sym.owner;
  1494. checkstack:=symtablestack.stack;
  1495. while assigned(checkstack) and
  1496. (checkstack^.symtable<>srsymtable) do
  1497. checkstack:=checkstack^.next;
  1498. { we've already processed the current symtable, start with
  1499. the next symtable in the stack }
  1500. if assigned(checkstack) then
  1501. checkstack:=checkstack^.next;
  1502. hashedid.id:=sym.name;
  1503. while assigned(checkstack) do
  1504. begin
  1505. srsymtable:=checkstack^.symtable;
  1506. if srsymtable.symtabletype in [localsymtable,staticsymtable,globalsymtable] then
  1507. begin
  1508. srprocsym:=tprocsym(srsymtable.FindWithHash(hashedid));
  1509. if assigned(srprocsym) and
  1510. (srprocsym.typ=procsym) then
  1511. begin
  1512. { if this visible procedure doesn't have overload we can stop
  1513. searching }
  1514. if not(po_overload in tprocdef(srprocsym.ProcdefList[0]).procoptions) and
  1515. tprocdef(srprocsym.ProcdefList[0]).is_visible_for_object(topclassh,nil) then
  1516. break;
  1517. { process all overloaded definitions }
  1518. for j:=0 to srprocsym.ProcdefList.Count-1 do
  1519. begin
  1520. pd:=tprocdef(srprocsym.ProcdefList[j]);
  1521. { only visible procedures need to be added }
  1522. if pd.is_visible_for_object(topclassh,nil) then
  1523. begin
  1524. { only when the # of parameter are supported by the
  1525. procedure }
  1526. if (FParalength>=pd.minparacount) and
  1527. ((po_varargs in pd.procoptions) or { varargs }
  1528. (FParalength<=pd.maxparacount)) then
  1529. begin
  1530. found:=false;
  1531. hp:=FProcs;
  1532. while assigned(hp) do
  1533. begin
  1534. { Only compare visible parameters for the user }
  1535. if compare_paras(hp^.data.paras,pd.paras,cp_value_equal_const,[cpo_ignorehidden])>=te_equal then
  1536. begin
  1537. found:=true;
  1538. break;
  1539. end;
  1540. hp:=hp^.next;
  1541. end;
  1542. if not found then
  1543. proc_add(pd);
  1544. end;
  1545. end;
  1546. end;
  1547. end;
  1548. end;
  1549. checkstack:=checkstack^.next;
  1550. end;
  1551. end;
  1552. end;
  1553. constructor tcallcandidates.create_operator(op:ttoken;ppn:tnode);
  1554. var
  1555. j : integer;
  1556. pd : tprocdef;
  1557. hp : pcandidate;
  1558. found : boolean;
  1559. srsymtable : TSymtable;
  1560. srprocsym : tprocsym;
  1561. pt : tcallparanode;
  1562. checkstack : psymtablestackitem;
  1563. hashedid : THashedIDString;
  1564. begin
  1565. FProcSym:=nil;
  1566. FProcs:=nil;
  1567. FProccnt:=0;
  1568. FProcvisiblecnt:=0;
  1569. FParanode:=ppn;
  1570. FAllowVariant:=false;
  1571. { determine length of parameter list }
  1572. pt:=tcallparanode(ppn);
  1573. FParalength:=0;
  1574. while assigned(pt) do
  1575. begin
  1576. if pt.resultdef.typ=variantdef then
  1577. FAllowVariant:=true;
  1578. inc(FParalength);
  1579. pt:=tcallparanode(pt.right);
  1580. end;
  1581. { we search all overloaded operator definitions in the symtablestack. The found
  1582. entries are only added to the procs list and not the procsym, because
  1583. the list can change in every situation }
  1584. hashedid.id:=overloaded_names[op];
  1585. checkstack:=symtablestack.stack;
  1586. while assigned(checkstack) do
  1587. begin
  1588. srsymtable:=checkstack^.symtable;
  1589. if srsymtable.symtabletype in [localsymtable,staticsymtable,globalsymtable] then
  1590. begin
  1591. srprocsym:=tprocsym(srsymtable.FindWithHash(hashedid));
  1592. if assigned(srprocsym) and
  1593. (srprocsym.typ=procsym) then
  1594. begin
  1595. { Store first procsym found }
  1596. if not assigned(FProcsym) then
  1597. FProcsym:=srprocsym;
  1598. { process all overloaded definitions }
  1599. for j:=0 to srprocsym.ProcdefList.Count-1 do
  1600. begin
  1601. pd:=tprocdef(srprocsym.ProcdefList[j]);
  1602. { only when the # of parameter are supported by the
  1603. procedure }
  1604. if (FParalength>=pd.minparacount) and
  1605. (FParalength<=pd.maxparacount) then
  1606. begin
  1607. found:=false;
  1608. hp:=FProcs;
  1609. while assigned(hp) do
  1610. begin
  1611. { Only compare visible parameters for the user }
  1612. if compare_paras(hp^.data.paras,pd.paras,cp_value_equal_const,[cpo_ignorehidden])>=te_equal then
  1613. begin
  1614. found:=true;
  1615. break;
  1616. end;
  1617. hp:=hp^.next;
  1618. end;
  1619. if not found then
  1620. proc_add(pd);
  1621. end;
  1622. end;
  1623. end;
  1624. end;
  1625. checkstack:=checkstack^.next;
  1626. end;
  1627. end;
  1628. destructor tcallcandidates.destroy;
  1629. var
  1630. hpnext,
  1631. hp : pcandidate;
  1632. begin
  1633. hp:=FProcs;
  1634. while assigned(hp) do
  1635. begin
  1636. hpnext:=hp^.next;
  1637. dispose(hp);
  1638. hp:=hpnext;
  1639. end;
  1640. end;
  1641. function tcallcandidates.proc_add(pd:tprocdef):pcandidate;
  1642. var
  1643. defaultparacnt : integer;
  1644. begin
  1645. { generate new candidate entry }
  1646. new(result);
  1647. fillchar(result^,sizeof(tcandidate),0);
  1648. result^.data:=pd;
  1649. result^.next:=FProcs;
  1650. FProcs:=result;
  1651. inc(FProccnt);
  1652. { Find last parameter, skip all default parameters
  1653. that are not passed. Ignore this skipping for varargs }
  1654. result^.firstparaidx:=pd.paras.count-1;
  1655. if not(po_varargs in pd.procoptions) then
  1656. begin
  1657. { ignore hidden parameters }
  1658. while (result^.firstparaidx>=0) and (vo_is_hidden_para in tparavarsym(pd.paras[result^.firstparaidx]).varoptions) do
  1659. dec(result^.firstparaidx);
  1660. defaultparacnt:=pd.maxparacount-FParalength;
  1661. if defaultparacnt>0 then
  1662. begin
  1663. if defaultparacnt>result^.firstparaidx+1 then
  1664. internalerror(200401141);
  1665. dec(result^.firstparaidx,defaultparacnt);
  1666. end;
  1667. end;
  1668. end;
  1669. procedure tcallcandidates.list(all:boolean);
  1670. var
  1671. hp : pcandidate;
  1672. begin
  1673. hp:=FProcs;
  1674. while assigned(hp) do
  1675. begin
  1676. if all or
  1677. (not hp^.invalid) then
  1678. MessagePos1(hp^.data.fileinfo,sym_h_param_list,hp^.data.fullprocname(false));
  1679. hp:=hp^.next;
  1680. end;
  1681. end;
  1682. {$ifdef EXTDEBUG}
  1683. procedure tcallcandidates.dump_info(lvl:longint);
  1684. function ParaTreeStr(p:tcallparanode):string;
  1685. begin
  1686. result:='';
  1687. while assigned(p) do
  1688. begin
  1689. if result<>'' then
  1690. result:=','+result;
  1691. result:=p.resultdef.typename+result;
  1692. p:=tcallparanode(p.right);
  1693. end;
  1694. end;
  1695. var
  1696. hp : pcandidate;
  1697. i : integer;
  1698. currpara : tparavarsym;
  1699. begin
  1700. if not CheckVerbosity(lvl) then
  1701. exit;
  1702. Comment(lvl+V_LineInfo,'Overloaded callnode: '+FProcSym.name+'('+ParaTreeStr(tcallparanode(FParaNode))+')');
  1703. hp:=FProcs;
  1704. while assigned(hp) do
  1705. begin
  1706. Comment(lvl,' '+hp^.data.fullprocname(false));
  1707. if (hp^.invalid) then
  1708. Comment(lvl,' invalid')
  1709. else
  1710. begin
  1711. Comment(lvl,' ex: '+tostr(hp^.exact_count)+
  1712. ' eq: '+tostr(hp^.equal_count)+
  1713. ' l1: '+tostr(hp^.cl1_count)+
  1714. ' l2: '+tostr(hp^.cl2_count)+
  1715. ' l3: '+tostr(hp^.cl3_count)+
  1716. ' oper: '+tostr(hp^.coper_count)+
  1717. ' ord: '+realtostr(hp^.ordinal_distance));
  1718. { Print parameters in left-right order }
  1719. for i:=0 to hp^.data.paras.count-1 do
  1720. begin
  1721. currpara:=tparavarsym(hp^.data.paras[i]);
  1722. if not(vo_is_hidden_para in currpara.varoptions) then
  1723. Comment(lvl,' - '+currpara.vardef.typename+' : '+EqualTypeName[currpara.eqval]);
  1724. end;
  1725. end;
  1726. hp:=hp^.next;
  1727. end;
  1728. end;
  1729. {$endif EXTDEBUG}
  1730. procedure tcallcandidates.get_information;
  1731. var
  1732. hp : pcandidate;
  1733. currpara : tparavarsym;
  1734. paraidx : integer;
  1735. currparanr : byte;
  1736. rfh,rth : bestreal;
  1737. objdef : tobjectdef;
  1738. def_from,
  1739. def_to : tdef;
  1740. currpt,
  1741. pt : tcallparanode;
  1742. eq : tequaltype;
  1743. convtype : tconverttype;
  1744. pdtemp,
  1745. pdoper : tprocdef;
  1746. releasecurrpt : boolean;
  1747. cdoptions : tcompare_defs_options;
  1748. begin
  1749. cdoptions:=[cdo_check_operator];
  1750. if FAllowVariant then
  1751. include(cdoptions,cdo_allow_variant);
  1752. { process all procs }
  1753. hp:=FProcs;
  1754. while assigned(hp) do
  1755. begin
  1756. { We compare parameters in reverse order (right to left),
  1757. the firstpara is already pointing to the last parameter
  1758. were we need to start comparing }
  1759. currparanr:=FParalength;
  1760. paraidx:=hp^.firstparaidx;
  1761. while (paraidx>=0) and (vo_is_hidden_para in tparavarsym(hp^.data.paras[paraidx]).varoptions) do
  1762. dec(paraidx);
  1763. pt:=tcallparanode(FParaNode);
  1764. while assigned(pt) and (paraidx>=0) do
  1765. begin
  1766. currpara:=tparavarsym(hp^.data.paras[paraidx]);
  1767. { currpt can be changed from loadn to calln when a procvar
  1768. is passed. This is to prevent that the change is permanent }
  1769. currpt:=pt;
  1770. releasecurrpt:=false;
  1771. { retrieve current parameter definitions to compares }
  1772. eq:=te_incompatible;
  1773. def_from:=currpt.resultdef;
  1774. def_to:=currpara.vardef;
  1775. if not(assigned(def_from)) then
  1776. internalerror(200212091);
  1777. if not(
  1778. assigned(def_to) or
  1779. ((po_varargs in hp^.data.procoptions) and
  1780. (currparanr>hp^.data.minparacount))
  1781. ) then
  1782. internalerror(200212092);
  1783. { Convert tp procvars when not expecting a procvar }
  1784. if (def_to.typ<>procvardef) and
  1785. (currpt.left.resultdef.typ=procvardef) then
  1786. begin
  1787. releasecurrpt:=true;
  1788. currpt:=tcallparanode(pt.getcopy);
  1789. if maybe_call_procvar(currpt.left,true) then
  1790. begin
  1791. currpt.resultdef:=currpt.left.resultdef;
  1792. def_from:=currpt.left.resultdef;
  1793. end;
  1794. end;
  1795. { If we expect a procvar and the left is loadnode that
  1796. returns a procdef we need to find the correct overloaded
  1797. procdef that matches the expected procvar. The loadnode
  1798. temporary returned the first procdef (PFV) }
  1799. if (def_to.typ=procvardef) and
  1800. (currpt.left.nodetype=loadn) and
  1801. (currpt.left.resultdef.typ=procdef) then
  1802. begin
  1803. pdtemp:=tprocsym(Tloadnode(currpt.left).symtableentry).Find_procdef_byprocvardef(Tprocvardef(def_to));
  1804. if assigned(pdtemp) then
  1805. begin
  1806. tloadnode(currpt.left).setprocdef(pdtemp);
  1807. currpt.resultdef:=currpt.left.resultdef;
  1808. def_from:=currpt.left.resultdef;
  1809. end;
  1810. end;
  1811. { varargs are always equal, but not exact }
  1812. if (po_varargs in hp^.data.procoptions) and
  1813. (currparanr>hp^.data.minparacount) and
  1814. not is_array_of_const(def_from) and
  1815. not is_array_constructor(def_from) then
  1816. begin
  1817. eq:=te_equal;
  1818. end
  1819. else
  1820. { same definition -> exact }
  1821. if (def_from=def_to) then
  1822. begin
  1823. eq:=te_exact;
  1824. end
  1825. else
  1826. { for value and const parameters check if a integer is constant or
  1827. included in other integer -> equal and calc ordinal_distance }
  1828. if not(currpara.varspez in [vs_var,vs_out]) and
  1829. is_integer(def_from) and
  1830. is_integer(def_to) and
  1831. is_in_limit(def_from,def_to) then
  1832. begin
  1833. eq:=te_equal;
  1834. hp^.ordinal_distance:=hp^.ordinal_distance+
  1835. abs(bestreal(torddef(def_from).low)-bestreal(torddef(def_to).low));
  1836. if (torddef(def_to).ordtype=u64bit) then
  1837. rth:=bestreal(qword(torddef(def_to).high))
  1838. else
  1839. rth:=bestreal(torddef(def_to).high);
  1840. if (torddef(def_from).ordtype=u64bit) then
  1841. rfh:=bestreal(qword(torddef(def_from).high))
  1842. else
  1843. rfh:=bestreal(torddef(def_from).high);
  1844. hp^.ordinal_distance:=hp^.ordinal_distance+abs(rth-rfh);
  1845. { Give wrong sign a small penalty, this is need to get a diffrence
  1846. from word->[longword,longint] }
  1847. if is_signed(def_from)<>is_signed(def_to) then
  1848. hp^.ordinal_distance:=hp^.ordinal_distance+1.0;
  1849. end
  1850. else
  1851. { for value and const parameters check precision of real, give
  1852. penalty for loosing of precision. var and out parameters must match exactly }
  1853. if not(currpara.varspez in [vs_var,vs_out]) and
  1854. is_real(def_from) and
  1855. is_real(def_to) then
  1856. begin
  1857. eq:=te_equal;
  1858. if is_extended(def_to) then
  1859. rth:=bestreal(4)
  1860. else
  1861. if is_double (def_to) then
  1862. rth:=bestreal(2)
  1863. else
  1864. rth:=bestreal(1);
  1865. if is_extended(def_from) then
  1866. rfh:=bestreal(4)
  1867. else
  1868. if is_double (def_from) then
  1869. rfh:=bestreal(2)
  1870. else
  1871. rfh:=bestreal(1);
  1872. { penalty for shrinking of precision }
  1873. if rth<rfh then
  1874. rfh:=(rfh-rth)*16
  1875. else
  1876. rfh:=rth-rfh;
  1877. hp^.ordinal_distance:=hp^.ordinal_distance+rfh;
  1878. end
  1879. else
  1880. { related object parameters also need to determine the distance between the current
  1881. object and the object we are comparing with. var and out parameters must match exactly }
  1882. if not(currpara.varspez in [vs_var,vs_out]) and
  1883. (def_from.typ=objectdef) and
  1884. (def_to.typ=objectdef) and
  1885. (tobjectdef(def_from).objecttype=tobjectdef(def_to).objecttype) and
  1886. tobjectdef(def_from).is_related(tobjectdef(def_to)) then
  1887. begin
  1888. eq:=te_convert_l1;
  1889. objdef:=tobjectdef(def_from);
  1890. while assigned(objdef) do
  1891. begin
  1892. if objdef=def_to then
  1893. break;
  1894. hp^.ordinal_distance:=hp^.ordinal_distance+1;
  1895. objdef:=objdef.childof;
  1896. end;
  1897. end
  1898. else
  1899. { generic type comparision }
  1900. begin
  1901. eq:=compare_defs_ext(def_from,def_to,currpt.left.nodetype,convtype,pdoper,cdoptions);
  1902. { when the types are not equal we need to check
  1903. some special case for parameter passing }
  1904. if (eq<te_equal) then
  1905. begin
  1906. if currpara.varspez in [vs_var,vs_out] then
  1907. begin
  1908. { para requires an equal type so the previous found
  1909. match was not good enough, reset to incompatible }
  1910. eq:=te_incompatible;
  1911. { var_para_allowed will return te_equal and te_convert_l1 to
  1912. make a difference for best matching }
  1913. var_para_allowed(eq,currpt.resultdef,currpara.vardef)
  1914. end
  1915. else
  1916. para_allowed(eq,currpt,def_to);
  1917. end;
  1918. end;
  1919. { when a procvar was changed to a call an exact much is
  1920. downgraded to equal. This way an overload call with the
  1921. procvar is choosen. See tb0471 (PFV) }
  1922. if (pt<>currpt) and (eq=te_exact) then
  1923. eq:=te_equal;
  1924. { increase correct counter }
  1925. case eq of
  1926. te_exact :
  1927. inc(hp^.exact_count);
  1928. te_equal :
  1929. inc(hp^.equal_count);
  1930. te_convert_l1 :
  1931. inc(hp^.cl1_count);
  1932. te_convert_l2 :
  1933. inc(hp^.cl2_count);
  1934. te_convert_l3 :
  1935. inc(hp^.cl3_count);
  1936. te_convert_operator :
  1937. inc(hp^.coper_count);
  1938. te_incompatible :
  1939. hp^.invalid:=true;
  1940. else
  1941. internalerror(200212072);
  1942. end;
  1943. { stop checking when an incompatible parameter is found }
  1944. if hp^.invalid then
  1945. begin
  1946. { store the current parameter info for
  1947. a nice error message when no procedure is found }
  1948. hp^.wrongparaidx:=paraidx;
  1949. hp^.wrongparanr:=currparanr;
  1950. break;
  1951. end;
  1952. {$ifdef EXTDEBUG}
  1953. { store equal in node tree for dump }
  1954. currpara.eqval:=eq;
  1955. {$endif EXTDEBUG}
  1956. { maybe release temp currpt }
  1957. if releasecurrpt then
  1958. currpt.free;
  1959. { next parameter in the call tree }
  1960. pt:=tcallparanode(pt.right);
  1961. { next parameter for definition, only goto next para
  1962. if we're out of the varargs }
  1963. if not(po_varargs in hp^.data.procoptions) or
  1964. (currparanr<=hp^.data.maxparacount) then
  1965. begin
  1966. { Ignore vs_hidden parameters }
  1967. repeat
  1968. dec(paraidx);
  1969. until (paraidx<0) or not(vo_is_hidden_para in tparavarsym(hp^.data.paras[paraidx]).varoptions);
  1970. end;
  1971. dec(currparanr);
  1972. end;
  1973. if not(hp^.invalid) and
  1974. (assigned(pt) or (paraidx>=0) or (currparanr<>0)) then
  1975. internalerror(200212141);
  1976. { next candidate }
  1977. hp:=hp^.next;
  1978. end;
  1979. end;
  1980. function is_better_candidate(currpd,bestpd:pcandidate):integer;
  1981. var
  1982. res : integer;
  1983. begin
  1984. {
  1985. Return values:
  1986. > 0 when currpd is better than bestpd
  1987. < 0 when bestpd is better than currpd
  1988. = 0 when both are equal
  1989. To choose the best candidate we use the following order:
  1990. - Incompatible flag
  1991. - (Smaller) Number of convert operator parameters.
  1992. - (Smaller) Number of convertlevel 2 parameters.
  1993. - (Smaller) Number of convertlevel 1 parameters.
  1994. - (Bigger) Number of exact parameters.
  1995. - (Smaller) Number of equal parameters.
  1996. - (Smaller) Total of ordinal distance. For example, the distance of a word
  1997. to a byte is 65535-255=65280.
  1998. }
  1999. if bestpd^.invalid then
  2000. begin
  2001. if currpd^.invalid then
  2002. res:=0
  2003. else
  2004. res:=1;
  2005. end
  2006. else
  2007. if currpd^.invalid then
  2008. res:=-1
  2009. else
  2010. begin
  2011. { less operator parameters? }
  2012. res:=(bestpd^.coper_count-currpd^.coper_count);
  2013. if (res=0) then
  2014. begin
  2015. { less cl3 parameters? }
  2016. res:=(bestpd^.cl3_count-currpd^.cl3_count);
  2017. if (res=0) then
  2018. begin
  2019. { less cl2 parameters? }
  2020. res:=(bestpd^.cl2_count-currpd^.cl2_count);
  2021. if (res=0) then
  2022. begin
  2023. { less cl1 parameters? }
  2024. res:=(bestpd^.cl1_count-currpd^.cl1_count);
  2025. if (res=0) then
  2026. begin
  2027. { more exact parameters? }
  2028. res:=(currpd^.exact_count-bestpd^.exact_count);
  2029. if (res=0) then
  2030. begin
  2031. { less equal parameters? }
  2032. res:=(bestpd^.equal_count-currpd^.equal_count);
  2033. if (res=0) then
  2034. begin
  2035. { smaller ordinal distance? }
  2036. if (currpd^.ordinal_distance<bestpd^.ordinal_distance) then
  2037. res:=1
  2038. else
  2039. if (currpd^.ordinal_distance>bestpd^.ordinal_distance) then
  2040. res:=-1
  2041. else
  2042. res:=0;
  2043. end;
  2044. end;
  2045. end;
  2046. end;
  2047. end;
  2048. end;
  2049. end;
  2050. is_better_candidate:=res;
  2051. end;
  2052. function tcallcandidates.choose_best(var bestpd:tabstractprocdef):integer;
  2053. var
  2054. besthpstart,
  2055. hp : pcandidate;
  2056. cntpd,
  2057. res : integer;
  2058. begin
  2059. {
  2060. Returns the number of candidates left and the
  2061. first candidate is returned in pdbest
  2062. }
  2063. { Setup the first procdef as best, only count it as a result
  2064. when it is valid }
  2065. bestpd:=FProcs^.data;
  2066. if FProcs^.invalid then
  2067. cntpd:=0
  2068. else
  2069. cntpd:=1;
  2070. if assigned(FProcs^.next) then
  2071. begin
  2072. besthpstart:=FProcs;
  2073. hp:=FProcs^.next;
  2074. while assigned(hp) do
  2075. begin
  2076. res:=is_better_candidate(hp,besthpstart);
  2077. if (res>0) then
  2078. begin
  2079. { hp is better, flag all procs to be incompatible }
  2080. while (besthpstart<>hp) do
  2081. begin
  2082. besthpstart^.invalid:=true;
  2083. besthpstart:=besthpstart^.next;
  2084. end;
  2085. { besthpstart is already set to hp }
  2086. bestpd:=besthpstart^.data;
  2087. cntpd:=1;
  2088. end
  2089. else
  2090. if (res<0) then
  2091. begin
  2092. { besthpstart is better, flag current hp to be incompatible }
  2093. hp^.invalid:=true;
  2094. end
  2095. else
  2096. begin
  2097. { res=0, both are valid }
  2098. if not hp^.invalid then
  2099. inc(cntpd);
  2100. end;
  2101. hp:=hp^.next;
  2102. end;
  2103. end;
  2104. result:=cntpd;
  2105. end;
  2106. procedure tcallcandidates.find_wrong_para;
  2107. var
  2108. currparanr : smallint;
  2109. hp : pcandidate;
  2110. pt : tcallparanode;
  2111. wrongpara : tparavarsym;
  2112. begin
  2113. { Only process the first overloaded procdef }
  2114. hp:=FProcs;
  2115. { Find callparanode corresponding to the argument }
  2116. pt:=tcallparanode(FParanode);
  2117. currparanr:=FParalength;
  2118. while assigned(pt) and
  2119. (currparanr>hp^.wrongparanr) do
  2120. begin
  2121. pt:=tcallparanode(pt.right);
  2122. dec(currparanr);
  2123. end;
  2124. if (currparanr<>hp^.wrongparanr) or
  2125. not assigned(pt) then
  2126. internalerror(200212094);
  2127. { Show error message, when it was a var or out parameter
  2128. guess that it is a missing typeconv }
  2129. wrongpara:=tparavarsym(hp^.data.paras[hp^.wrongparaidx]);
  2130. if wrongpara.varspez in [vs_var,vs_out] then
  2131. begin
  2132. { Maybe passing the correct type but passing a const to var parameter }
  2133. if (compare_defs(pt.resultdef,wrongpara.vardef,pt.nodetype)<>te_incompatible) and
  2134. not valid_for_var(pt.left,true) then
  2135. CGMessagePos(pt.left.fileinfo,type_e_variable_id_expected)
  2136. else
  2137. CGMessagePos3(pt.left.fileinfo,parser_e_call_by_ref_without_typeconv,tostr(hp^.wrongparanr),
  2138. FullTypeName(pt.left.resultdef,wrongpara.vardef),
  2139. FullTypeName(wrongpara.vardef,pt.left.resultdef))
  2140. end
  2141. else
  2142. CGMessagePos3(pt.left.fileinfo,type_e_wrong_parameter_type,tostr(hp^.wrongparanr),
  2143. FullTypeName(pt.left.resultdef,wrongpara.vardef),
  2144. FullTypeName(wrongpara.vardef,pt.left.resultdef));
  2145. end;
  2146. procedure check_hints(const srsym: tsym; const symoptions: tsymoptions);
  2147. begin
  2148. if not assigned(srsym) then
  2149. internalerror(200602051);
  2150. if sp_hint_deprecated in symoptions then
  2151. Message1(sym_w_deprecated_symbol,srsym.realname);
  2152. if sp_hint_platform in symoptions then
  2153. Message1(sym_w_non_portable_symbol,srsym.realname);
  2154. if sp_hint_unimplemented in symoptions then
  2155. Message1(sym_w_non_implemented_symbol,srsym.realname);
  2156. end;
  2157. procedure check_ranges(const location: tfileposinfo; source: tnode; destdef: tdef);
  2158. begin
  2159. { check if the assignment may cause a range check error }
  2160. { if its not explicit, and only if the values are }
  2161. { ordinals, enumdef and floatdef }
  2162. if assigned(destdef) and
  2163. (destdef.typ in [enumdef,orddef,floatdef]) and
  2164. not is_boolean(destdef) and
  2165. assigned(source.resultdef) and
  2166. (source.resultdef.typ in [enumdef,orddef,floatdef]) and
  2167. not is_boolean(source.resultdef) and
  2168. not is_constrealnode(source) then
  2169. begin
  2170. if (destdef.size < source.resultdef.size) then
  2171. begin
  2172. if (cs_check_range in current_settings.localswitches) then
  2173. MessagePos(location,type_w_smaller_possible_range_check)
  2174. else
  2175. MessagePos(location,type_h_smaller_possible_range_check);
  2176. end;
  2177. end;
  2178. end;
  2179. end.