htypechk.pas 143 KB

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