htypechk.pas 145 KB

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