htypechk.pas 145 KB

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