htypechk.pas 142 KB

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