htypechk.pas 134 KB

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