htypechk.pas 143 KB

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