htypechk.pas 134 KB

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