htypechk.pas 134 KB

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