htypechk.pas 116 KB

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