htypechk.pas 142 KB

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