htypechk.pas 103 KB

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