htypechk.pas 121 KB

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