htypechk.pas 100 KB

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