htypechk.pas 133 KB

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