htypechk.pas 135 KB

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