htypechk.pas 143 KB

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