htypechk.pas 143 KB

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