htypechk.pas 118 KB

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