htypechk.pas 125 KB

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