htypechk.pas 86 KB

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