htypechk.pas 142 KB

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