htypechk.pas 133 KB

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