htypechk.pas 116 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962
  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. { indexing a dynamic array = dereference }
  1040. (gotdynarray and gotvec) or
  1041. (
  1042. { allowing assignments to typecasted properties
  1043. a) is Delphi-incompatible
  1044. b) causes problems in case the getter is a function
  1045. (because then the result of the getter is
  1046. typecasted to this type, and then we "assign" to
  1047. this typecasted function result) -> always
  1048. disallow, since property accessors should be
  1049. transparantly changeable to functions at all
  1050. times
  1051. }
  1052. not(gottypeconv) and
  1053. not(gotsubscript and gotrecord) and
  1054. not(gotstring and gotvec) and
  1055. not(nf_no_lvalue in hp.flags)
  1056. ) then
  1057. result:=true
  1058. else
  1059. if report_errors then
  1060. CGMessagePos(hp.fileinfo,errmsg);
  1061. end
  1062. else
  1063. begin
  1064. { 1. if it returns a pointer and we've found a deref,
  1065. 2. if it returns a class and a subscription or with is found
  1066. 3. if the address is needed of a field (subscriptn, vecn) }
  1067. if (gotpointer and gotderef) or
  1068. (gotstring and gotvec) or
  1069. (gotclass and gotsubscript) or
  1070. (
  1071. (gotvec and gotdynarray)
  1072. ) or
  1073. (
  1074. (Valid_Addr in opts) and
  1075. (hp.nodetype in [subscriptn,vecn])
  1076. ) then
  1077. result:=true
  1078. else
  1079. if report_errors then
  1080. CGMessagePos(hp.fileinfo,errmsg);
  1081. end;
  1082. exit;
  1083. end;
  1084. case hp.nodetype of
  1085. temprefn :
  1086. begin
  1087. valid_for_assign := true;
  1088. exit;
  1089. end;
  1090. derefn :
  1091. begin
  1092. gotderef:=true;
  1093. hp:=tderefnode(hp).left;
  1094. end;
  1095. typeconvn :
  1096. begin
  1097. gottypeconv:=true;
  1098. { typecast sizes must match, exceptions:
  1099. - implicit typecast made by absolute
  1100. - from formaldef
  1101. - from void
  1102. - from/to open array
  1103. - typecast from pointer to array }
  1104. fromdef:=ttypeconvnode(hp).left.resultdef;
  1105. todef:=hp.resultdef;
  1106. if not((nf_absolute in ttypeconvnode(hp).flags) or
  1107. (fromdef.typ=formaldef) or
  1108. is_void(fromdef) or
  1109. is_open_array(fromdef) or
  1110. is_open_array(todef) or
  1111. ((fromdef.typ=pointerdef) and (todef.typ=arraydef)) or
  1112. ((fromdef.typ = objectdef) and (todef.typ = objectdef) and
  1113. (tobjectdef(fromdef).is_related(tobjectdef(todef))))) and
  1114. (fromdef.size<>todef.size) then
  1115. begin
  1116. { in TP it is allowed to typecast to smaller types. But the variable can't
  1117. be in a register }
  1118. if (m_tp7 in current_settings.modeswitches) or
  1119. (todef.size<fromdef.size) then
  1120. make_not_regable(hp,[ra_addr_regable])
  1121. else
  1122. if report_errors then
  1123. CGMessagePos2(hp.fileinfo,type_e_typecast_wrong_size_for_assignment,tostr(fromdef.size),tostr(todef.size));
  1124. end;
  1125. { don't allow assignments to typeconvs that need special code }
  1126. if not(gotsubscript or gotvec or gotderef) and
  1127. not(ttypeconvnode(hp).assign_allowed) then
  1128. begin
  1129. if report_errors then
  1130. CGMessagePos(hp.fileinfo,errmsg);
  1131. exit;
  1132. end;
  1133. case hp.resultdef.typ of
  1134. pointerdef :
  1135. gotpointer:=true;
  1136. objectdef :
  1137. gotclass:=is_implicit_pointer_object_type(hp.resultdef);
  1138. classrefdef :
  1139. gotclass:=true;
  1140. arraydef :
  1141. begin
  1142. { pointer -> array conversion is done then we need to see it
  1143. as a deref, because a ^ is then not required anymore }
  1144. if (ttypeconvnode(hp).left.resultdef.typ=pointerdef) then
  1145. gotderef:=true;
  1146. end;
  1147. end;
  1148. hp:=ttypeconvnode(hp).left;
  1149. end;
  1150. vecn :
  1151. begin
  1152. if { only check for first (= outermost) vec node }
  1153. not gotvec and
  1154. not(valid_packed in opts) and
  1155. (tvecnode(hp).left.resultdef.typ = arraydef) and
  1156. (ado_IsBitPacked in tarraydef(tvecnode(hp).left.resultdef).arrayoptions) and
  1157. ((tarraydef(tvecnode(hp).left.resultdef).elepackedbitsize mod 8 <> 0) or
  1158. (is_ordinal(tarraydef(tvecnode(hp).left.resultdef).elementdef) and
  1159. not ispowerof2(tarraydef(tvecnode(hp).left.resultdef).elepackedbitsize div 8,temp))) then
  1160. begin
  1161. if report_errors then
  1162. if (valid_property in opts) then
  1163. CGMessagePos(hp.fileinfo,parser_e_packed_element_no_loop)
  1164. else
  1165. CGMessagePos(hp.fileinfo,parser_e_packed_element_no_var_addr);
  1166. exit;
  1167. end;
  1168. gotvec:=true;
  1169. { accesses to dyn. arrays override read only access in delphi
  1170. -- now also in FPC, because the elements of a dynamic array
  1171. returned by a function can also be changed, or you can
  1172. assign the dynamic array to a variable and then change
  1173. its elements anyway }
  1174. if is_dynamic_array(tunarynode(hp).left.resultdef) then
  1175. gotdynarray:=true;
  1176. hp:=tunarynode(hp).left;
  1177. end;
  1178. blockn :
  1179. begin
  1180. hp2:=tblocknode(hp).statements;
  1181. if assigned(hp2) then
  1182. begin
  1183. if hp2.nodetype<>statementn then
  1184. internalerror(2006110801);
  1185. while assigned(tstatementnode(hp2).next) do
  1186. hp2:=tstatementnode(hp2).next;
  1187. hp:=tstatementnode(hp2).statement;
  1188. end
  1189. else
  1190. begin
  1191. if report_errors then
  1192. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  1193. exit;
  1194. end;
  1195. end;
  1196. asn :
  1197. begin
  1198. { asn can't be assigned directly, it returns the value in a register instead
  1199. of reference. }
  1200. if not(gotsubscript or gotderef or gotvec) then
  1201. begin
  1202. if report_errors then
  1203. CGMessagePos(hp.fileinfo,errmsg);
  1204. exit;
  1205. end;
  1206. hp:=tunarynode(hp).left;
  1207. end;
  1208. subscriptn :
  1209. begin
  1210. { only check first (= outermost) subscriptn }
  1211. if not gotsubscript and
  1212. not(valid_packed in opts) and
  1213. is_packed_record_or_object(tsubscriptnode(hp).left.resultdef) and
  1214. ((tsubscriptnode(hp).vs.fieldoffset mod 8 <> 0) or
  1215. (is_ordinal(tsubscriptnode(hp).resultdef) and
  1216. not ispowerof2(tsubscriptnode(hp).resultdef.packedbitsize div 8,temp))) then
  1217. begin
  1218. if report_errors then
  1219. if (valid_property in opts) then
  1220. CGMessagePos(hp.fileinfo,parser_e_packed_element_no_loop)
  1221. else
  1222. CGMessagePos(hp.fileinfo,parser_e_packed_element_no_var_addr);
  1223. exit;
  1224. end;
  1225. gotsubscript:=true;
  1226. { loop counter? }
  1227. if not(Valid_Const in opts) and
  1228. (vo_is_loop_counter in tsubscriptnode(hp).vs.varoptions) then
  1229. begin
  1230. if report_errors then
  1231. CGMessage1(parser_e_illegal_assignment_to_count_var,tsubscriptnode(hp).vs.realname);
  1232. exit;
  1233. end;
  1234. { implicit pointer object types result in dereferencing }
  1235. hp:=tsubscriptnode(hp).left;
  1236. if is_implicit_pointer_object_type(hp.resultdef) then
  1237. gotderef:=true;
  1238. end;
  1239. muln,
  1240. divn,
  1241. andn,
  1242. xorn,
  1243. orn,
  1244. notn,
  1245. subn,
  1246. addn :
  1247. begin
  1248. { Allow operators on a pointer, or an integer
  1249. and a pointer typecast and deref has been found }
  1250. if ((hp.resultdef.typ=pointerdef) or
  1251. (is_integer(hp.resultdef) and gotpointer)) and
  1252. gotderef then
  1253. result:=true
  1254. else
  1255. { Temp strings are stored in memory, for compatibility with
  1256. delphi only }
  1257. if (m_delphi in current_settings.modeswitches) and
  1258. ((valid_addr in opts) or
  1259. (valid_const in opts)) and
  1260. (hp.resultdef.typ=stringdef) then
  1261. result:=true
  1262. else
  1263. if report_errors then
  1264. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  1265. exit;
  1266. end;
  1267. niln,
  1268. pointerconstn :
  1269. begin
  1270. { to support e.g. @tmypointer(0)^.data; see tests/tbs/tb0481 }
  1271. if gotderef then
  1272. result:=true
  1273. else
  1274. if report_errors then
  1275. CGMessagePos(hp.fileinfo,type_e_no_assign_to_addr);
  1276. exit;
  1277. end;
  1278. ordconstn,
  1279. realconstn :
  1280. begin
  1281. { these constants will be passed by value }
  1282. if report_errors then
  1283. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  1284. exit;
  1285. end;
  1286. setconstn,
  1287. stringconstn,
  1288. guidconstn :
  1289. begin
  1290. { these constants will be passed by reference }
  1291. if valid_const in opts then
  1292. result:=true
  1293. else
  1294. if report_errors then
  1295. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  1296. exit;
  1297. end;
  1298. addrn :
  1299. begin
  1300. if gotderef then
  1301. result:=true
  1302. else
  1303. if report_errors then
  1304. CGMessagePos(hp.fileinfo,type_e_no_assign_to_addr);
  1305. exit;
  1306. end;
  1307. calln :
  1308. begin
  1309. { check return type }
  1310. case hp.resultdef.typ of
  1311. arraydef :
  1312. begin
  1313. { dynamic arrays are allowed when there is also a
  1314. vec node }
  1315. if is_dynamic_array(hp.resultdef) and
  1316. gotvec then
  1317. begin
  1318. gotderef:=true;
  1319. gotpointer:=true;
  1320. end;
  1321. end;
  1322. pointerdef :
  1323. gotpointer:=true;
  1324. objectdef :
  1325. gotclass:=is_implicit_pointer_object_type(hp.resultdef);
  1326. recorddef, { handle record like class it needs a subscription }
  1327. classrefdef :
  1328. gotclass:=true;
  1329. stringdef :
  1330. gotstring:=true;
  1331. end;
  1332. { 1. if it returns a pointer and we've found a deref,
  1333. 2. if it returns a class or record and a subscription or with is found
  1334. 3. string is returned }
  1335. if (gotstring and gotvec) or
  1336. (gotpointer and gotderef) or
  1337. (gotclass and gotsubscript) then
  1338. result:=true
  1339. else
  1340. { Temp strings are stored in memory, for compatibility with
  1341. delphi only }
  1342. if (m_delphi in current_settings.modeswitches) and
  1343. (valid_addr in opts) and
  1344. (hp.resultdef.typ=stringdef) then
  1345. result:=true
  1346. else
  1347. if ([valid_const,valid_addr] * opts = [valid_const]) then
  1348. result:=true
  1349. else
  1350. if report_errors then
  1351. CGMessagePos(hp.fileinfo,errmsg);
  1352. exit;
  1353. end;
  1354. inlinen :
  1355. begin
  1356. if ((valid_const in opts) and
  1357. (tinlinenode(hp).inlinenumber in [in_typeof_x])) or
  1358. (tinlinenode(hp).inlinenumber in [in_unaligned_x]) then
  1359. result:=true
  1360. else
  1361. if report_errors then
  1362. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  1363. exit;
  1364. end;
  1365. dataconstn:
  1366. begin
  1367. { only created internally, so no additional checks necessary }
  1368. result:=true;
  1369. exit;
  1370. end;
  1371. loadn :
  1372. begin
  1373. case tloadnode(hp).symtableentry.typ of
  1374. absolutevarsym,
  1375. staticvarsym,
  1376. localvarsym,
  1377. paravarsym :
  1378. begin
  1379. { loop counter? }
  1380. if not(Valid_Const in opts) and
  1381. not gotderef and
  1382. (vo_is_loop_counter in tabstractvarsym(tloadnode(hp).symtableentry).varoptions) then
  1383. begin
  1384. if report_errors then
  1385. CGMessage1(parser_e_illegal_assignment_to_count_var,tloadnode(hp).symtableentry.realname);
  1386. exit;
  1387. end;
  1388. { read-only variable? }
  1389. if (tabstractvarsym(tloadnode(hp).symtableentry).varspez in [vs_const,vs_constref]) then
  1390. begin
  1391. { allow p^:= constructions with p is const parameter }
  1392. if gotderef or gotdynarray or (Valid_Const in opts) or
  1393. (loadnf_isinternal_ignoreconst in tloadnode(hp).loadnodeflags) then
  1394. result:=true
  1395. else
  1396. if report_errors then
  1397. CGMessagePos(tloadnode(hp).fileinfo,type_e_no_assign_to_const);
  1398. exit;
  1399. end;
  1400. result:=true;
  1401. exit;
  1402. end;
  1403. procsym :
  1404. begin
  1405. if (Valid_Const in opts) then
  1406. result:=true
  1407. else
  1408. if report_errors then
  1409. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  1410. exit;
  1411. end;
  1412. labelsym :
  1413. begin
  1414. if (Valid_Addr in opts) then
  1415. result:=true
  1416. else
  1417. if report_errors then
  1418. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  1419. exit;
  1420. end;
  1421. constsym:
  1422. begin
  1423. if (tconstsym(tloadnode(hp).symtableentry).consttyp=constresourcestring) and
  1424. (valid_addr in opts) then
  1425. result:=true
  1426. else
  1427. if report_errors then
  1428. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  1429. exit;
  1430. end;
  1431. else
  1432. begin
  1433. if report_errors then
  1434. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  1435. exit;
  1436. end;
  1437. end;
  1438. end;
  1439. else
  1440. begin
  1441. if report_errors then
  1442. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  1443. exit;
  1444. end;
  1445. end;
  1446. end;
  1447. end;
  1448. function valid_for_var(p:tnode; report_errors: boolean):boolean;
  1449. begin
  1450. valid_for_var:=valid_for_assign(p,[],report_errors);
  1451. end;
  1452. function valid_for_formal_var(p : tnode; report_errors: boolean) : boolean;
  1453. begin
  1454. valid_for_formal_var:=valid_for_assign(p,[valid_void],report_errors);
  1455. end;
  1456. function valid_for_formal_const(p : tnode; report_errors: boolean) : boolean;
  1457. begin
  1458. valid_for_formal_const:=(p.resultdef.typ=formaldef) or
  1459. valid_for_assign(p,[valid_void,valid_const,valid_property],report_errors);
  1460. end;
  1461. function valid_for_assignment(p:tnode; report_errors: boolean):boolean;
  1462. begin
  1463. valid_for_assignment:=valid_for_assign(p,[valid_property,valid_packed],report_errors);
  1464. end;
  1465. function valid_for_loopvar(p:tnode; report_errors: boolean):boolean;
  1466. begin
  1467. valid_for_loopvar:=valid_for_assign(p,[valid_property],report_errors);
  1468. end;
  1469. function valid_for_addr(p : tnode; report_errors: boolean) : boolean;
  1470. begin
  1471. result:=valid_for_assign(p,[valid_const,valid_addr,valid_void],report_errors);
  1472. end;
  1473. procedure var_para_allowed(var eq:tequaltype;def_from,def_to:Tdef; fromnode: tnode);
  1474. begin
  1475. { Note: eq must be already valid, it will only be updated! }
  1476. case def_to.typ of
  1477. formaldef :
  1478. begin
  1479. { all types can be passed to a formaldef,
  1480. but it is not the prefered way }
  1481. if not is_constnode(fromnode) then
  1482. eq:=te_convert_l2
  1483. else
  1484. eq:=te_incompatible;
  1485. end;
  1486. orddef :
  1487. begin
  1488. { allows conversion from word to integer and
  1489. byte to shortint, but only for TP7 compatibility }
  1490. if (m_tp7 in current_settings.modeswitches) and
  1491. (def_from.typ=orddef) and
  1492. (def_from.size=def_to.size) then
  1493. eq:=te_convert_l1;
  1494. end;
  1495. arraydef :
  1496. begin
  1497. if is_open_array(def_to) then
  1498. begin
  1499. if is_dynamic_array(def_from) and
  1500. equal_defs(tarraydef(def_from).elementdef,tarraydef(def_to).elementdef) then
  1501. eq:=te_convert_l2
  1502. else
  1503. if equal_defs(def_from,tarraydef(def_to).elementdef) then
  1504. eq:=te_convert_l3;
  1505. end;
  1506. end;
  1507. pointerdef :
  1508. begin
  1509. { an implicit pointer conversion is allowed }
  1510. if (def_from.typ=pointerdef) then
  1511. eq:=te_convert_l1;
  1512. end;
  1513. stringdef :
  1514. begin
  1515. { all shortstrings are allowed, size is not important }
  1516. if is_shortstring(def_from) and
  1517. is_shortstring(def_to) then
  1518. eq:=te_equal;
  1519. end;
  1520. objectdef :
  1521. begin
  1522. { child objects can be also passed }
  1523. { in non-delphi mode, otherwise }
  1524. { they must match exactly, except }
  1525. { if they are objects }
  1526. if (def_from.typ=objectdef) and
  1527. (
  1528. (tobjectdef(def_from).objecttype=odt_object) and
  1529. (tobjectdef(def_to).objecttype=odt_object)
  1530. ) and
  1531. (tobjectdef(def_from).is_related(tobjectdef(def_to))) then
  1532. eq:=te_convert_l1;
  1533. end;
  1534. filedef :
  1535. begin
  1536. { an implicit file conversion is also allowed }
  1537. { from a typed file to an untyped one }
  1538. if (def_from.typ=filedef) and
  1539. (tfiledef(def_from).filetyp = ft_typed) and
  1540. (tfiledef(def_to).filetyp = ft_untyped) then
  1541. eq:=te_convert_l1;
  1542. end;
  1543. end;
  1544. end;
  1545. procedure para_allowed(var eq:tequaltype;p:tcallparanode;def_to:tdef);
  1546. var
  1547. acn: tarrayconstructornode;
  1548. realprocdef: tprocdef;
  1549. tmpeq: tequaltype;
  1550. begin
  1551. { Note: eq must be already valid, it will only be updated! }
  1552. case def_to.typ of
  1553. formaldef :
  1554. begin
  1555. { all types can be passed to a formaldef }
  1556. eq:=te_equal;
  1557. end;
  1558. stringdef :
  1559. begin
  1560. { to support ansi/long/wide strings in a proper way }
  1561. { string and string[10] are assumed as equal }
  1562. { when searching the correct overloaded procedure }
  1563. if (p.resultdef.typ=stringdef) and
  1564. (tstringdef(def_to).stringtype=tstringdef(p.resultdef).stringtype) then
  1565. eq:=te_equal
  1566. end;
  1567. setdef :
  1568. begin
  1569. { set can also be a not yet converted array constructor }
  1570. if (p.resultdef.typ=arraydef) and
  1571. is_array_constructor(p.resultdef) and
  1572. not is_variant_array(p.resultdef) then
  1573. eq:=te_equal;
  1574. end;
  1575. procvardef :
  1576. begin
  1577. tmpeq:=te_incompatible;
  1578. { in tp/macpas mode proc -> procvar is allowed }
  1579. if ((m_tp_procvar in current_settings.modeswitches) or
  1580. (m_mac_procvar in current_settings.modeswitches)) and
  1581. (p.left.nodetype=calln) then
  1582. tmpeq:=proc_to_procvar_equal(tprocdef(tcallnode(p.left).procdefinition),tprocvardef(def_to),false);
  1583. if (tmpeq=te_incompatible) and
  1584. (m_nested_procvars in current_settings.modeswitches) and
  1585. is_proc2procvar_load(p.left,realprocdef) then
  1586. tmpeq:=proc_to_procvar_equal(realprocdef,tprocvardef(def_to),false);
  1587. if (tmpeq=te_incompatible) and
  1588. (m_mac in current_settings.modeswitches) and
  1589. is_ambiguous_funcret_load(p.left,realprocdef) then
  1590. tmpeq:=proc_to_procvar_equal(realprocdef,tprocvardef(def_to),false);
  1591. if tmpeq<>te_incompatible then
  1592. eq:=tmpeq;
  1593. end;
  1594. arraydef :
  1595. begin
  1596. { an arrayconstructor of proccalls may have to be converted to
  1597. an array of procvars }
  1598. if ((m_tp_procvar in current_settings.modeswitches) or
  1599. (m_mac_procvar in current_settings.modeswitches)) and
  1600. (tarraydef(def_to).elementdef.typ=procvardef) and
  1601. is_array_constructor(p.resultdef) and
  1602. not is_variant_array(p.resultdef) then
  1603. begin
  1604. acn:=tarrayconstructornode(p.left);
  1605. if assigned(acn.left) then
  1606. begin
  1607. eq:=te_exact;
  1608. while assigned(acn) and
  1609. (eq<>te_incompatible) do
  1610. begin
  1611. if (acn.left.nodetype=calln) then
  1612. tmpeq:=proc_to_procvar_equal(tprocdef(tcallnode(acn.left).procdefinition),tprocvardef(tarraydef(def_to).elementdef),false)
  1613. else
  1614. tmpeq:=compare_defs(acn.left.resultdef,tarraydef(def_to).elementdef,acn.left.nodetype);
  1615. if tmpeq<eq then
  1616. eq:=tmpeq;
  1617. acn:=tarrayconstructornode(acn.right);
  1618. end;
  1619. end
  1620. end;
  1621. end;
  1622. end;
  1623. end;
  1624. function allowenumop(nt:tnodetype):boolean;
  1625. begin
  1626. result:=(nt in [equaln,unequaln,ltn,lten,gtn,gten]) or
  1627. ((cs_allow_enum_calc in current_settings.localswitches) and
  1628. (nt in [addn,subn]));
  1629. end;
  1630. {****************************************************************************
  1631. TCallCandidates
  1632. ****************************************************************************}
  1633. constructor tcallcandidates.create(sym:tprocsym;st:TSymtable;ppn:tnode;ignorevisibility,allowdefaultparas,objcidcall,explicitunit,searchhelpers,anoninherited:boolean);
  1634. begin
  1635. if not assigned(sym) then
  1636. internalerror(200411015);
  1637. FOperator:=NOTOKEN;
  1638. FProcsym:=sym;
  1639. FProcsymtable:=st;
  1640. FParanode:=ppn;
  1641. create_candidate_list(ignorevisibility,allowdefaultparas,objcidcall,explicitunit,searchhelpers,anoninherited);
  1642. end;
  1643. constructor tcallcandidates.create_operator(op:ttoken;ppn:tnode);
  1644. begin
  1645. FOperator:=op;
  1646. FProcsym:=nil;
  1647. FProcsymtable:=nil;
  1648. FParanode:=ppn;
  1649. create_candidate_list(false,false,false,false,false,false);
  1650. end;
  1651. destructor tcallcandidates.destroy;
  1652. var
  1653. hpnext,
  1654. hp : pcandidate;
  1655. begin
  1656. hp:=FCandidateProcs;
  1657. while assigned(hp) do
  1658. begin
  1659. hpnext:=hp^.next;
  1660. dispose(hp);
  1661. hp:=hpnext;
  1662. end;
  1663. end;
  1664. procedure tcallcandidates.collect_overloads_in_struct(structdef:tabstractrecorddef;ProcdefOverloadList:TFPObjectList;searchhelpers,anoninherited:boolean);
  1665. function processprocsym(srsym:tprocsym; out foundanything: boolean):boolean;
  1666. var
  1667. j : integer;
  1668. pd : tprocdef;
  1669. begin
  1670. { add all definitions }
  1671. result:=false;
  1672. foundanything:=false;
  1673. for j:=0 to srsym.ProcdefList.Count-1 do
  1674. begin
  1675. pd:=tprocdef(srsym.ProcdefList[j]);
  1676. { in case of anonymous inherited, only match procdefs identical
  1677. to the current one (apart from hidden parameters), rather than
  1678. anything compatible to the parameters -- except in case of
  1679. the presence of a messagestr/int, in which case those have to
  1680. match exactly }
  1681. if anoninherited then
  1682. if po_msgint in current_procinfo.procdef.procoptions then
  1683. begin
  1684. if not(po_msgint in pd.procoptions) or
  1685. (pd.messageinf.i<>current_procinfo.procdef.messageinf.i) then
  1686. continue
  1687. end
  1688. else if po_msgstr in current_procinfo.procdef.procoptions then
  1689. begin
  1690. if not(po_msgstr in pd.procoptions) or
  1691. (pd.messageinf.str^<>current_procinfo.procdef.messageinf.str^) then
  1692. continue
  1693. end
  1694. else if (compare_paras(current_procinfo.procdef.paras,pd.paras,cp_all,[cpo_ignorehidden])<te_equal) then
  1695. continue;
  1696. foundanything:=true;
  1697. { Store first procsym found }
  1698. if not assigned(FProcsym) then
  1699. FProcsym:=tprocsym(srsym);
  1700. if po_overload in pd.procoptions then
  1701. result:=true;
  1702. ProcdefOverloadList.Add(srsym.ProcdefList[j]);
  1703. end;
  1704. end;
  1705. var
  1706. srsym : tsym;
  1707. hashedid : THashedIDString;
  1708. hasoverload,
  1709. foundanything : boolean;
  1710. helperdef : tobjectdef;
  1711. begin
  1712. if FOperator=NOTOKEN then
  1713. hashedid.id:=FProcsym.name
  1714. else
  1715. hashedid.id:=overloaded_names[FOperator];
  1716. hasoverload:=false;
  1717. while assigned(structdef) do
  1718. begin
  1719. { first search in helpers for this type }
  1720. if (is_class(structdef) or is_record(structdef))
  1721. and searchhelpers then
  1722. begin
  1723. if search_last_objectpascal_helper(structdef,nil,helperdef) then
  1724. begin
  1725. srsym:=nil;
  1726. while assigned(helperdef) do
  1727. begin
  1728. srsym:=tsym(helperdef.symtable.FindWithHash(hashedid));
  1729. if assigned(srsym) and
  1730. { Delphi allows hiding a property by a procedure with the same name }
  1731. (srsym.typ=procsym) then
  1732. begin
  1733. hasoverload:=processprocsym(tprocsym(srsym),foundanything);
  1734. { when there is no explicit overload we stop searching }
  1735. if foundanything and
  1736. not hasoverload then
  1737. break;
  1738. end;
  1739. helperdef:=helperdef.childof;
  1740. end;
  1741. if not hasoverload and assigned(srsym) then
  1742. exit;
  1743. end;
  1744. end;
  1745. { now search in the type itself }
  1746. srsym:=tprocsym(structdef.symtable.FindWithHash(hashedid));
  1747. if assigned(srsym) and
  1748. { Delphi allows hiding a property by a procedure with the same name }
  1749. (srsym.typ=procsym) then
  1750. begin
  1751. hasoverload:=processprocsym(tprocsym(srsym),foundanything);
  1752. { when there is no explicit overload we stop searching }
  1753. if foundanything and
  1754. not hasoverload then
  1755. break;
  1756. end;
  1757. if is_objectpascal_helper(structdef) and
  1758. (tobjectdef(structdef).typ in [recorddef,objectdef]) then
  1759. begin
  1760. { search methods in the extended type as well }
  1761. srsym:=tprocsym(tabstractrecorddef(tobjectdef(structdef).extendeddef).symtable.FindWithHash(hashedid));
  1762. if assigned(srsym) and
  1763. { Delphi allows hiding a property by a procedure with the same name }
  1764. (srsym.typ=procsym) then
  1765. begin
  1766. hasoverload:=processprocsym(tprocsym(srsym),foundanything);
  1767. { when there is no explicit overload we stop searching }
  1768. if foundanything and
  1769. not hasoverload then
  1770. break;
  1771. end;
  1772. end;
  1773. { next parent }
  1774. if (structdef.typ=objectdef) then
  1775. structdef:=tobjectdef(structdef).childof
  1776. else
  1777. structdef:=nil;
  1778. end;
  1779. end;
  1780. procedure tcallcandidates.collect_overloads_in_units(ProcdefOverloadList:TFPObjectList; objcidcall,explicitunit: boolean);
  1781. var
  1782. j : integer;
  1783. pd : tprocdef;
  1784. srsymtable : TSymtable;
  1785. srsym : tsym;
  1786. checkstack : psymtablestackitem;
  1787. hashedid : THashedIDString;
  1788. hasoverload : boolean;
  1789. begin
  1790. { we search all overloaded operator definitions in the symtablestack. The found
  1791. entries are only added to the procs list and not the procsym, because
  1792. the list can change in every situation }
  1793. if FOperator=NOTOKEN then
  1794. begin
  1795. if not objcidcall then
  1796. hashedid.id:=FProcsym.name
  1797. else
  1798. hashedid.id:=class_helper_prefix+FProcsym.name;
  1799. end
  1800. else
  1801. hashedid.id:=overloaded_names[FOperator];
  1802. checkstack:=symtablestack.stack;
  1803. if assigned(FProcsymtable) then
  1804. begin
  1805. while assigned(checkstack) and
  1806. (checkstack^.symtable<>FProcsymtable) do
  1807. checkstack:=checkstack^.next;
  1808. end;
  1809. while assigned(checkstack) do
  1810. begin
  1811. srsymtable:=checkstack^.symtable;
  1812. { if the unit in which the routine has to be searched has been
  1813. specified explicitly, stop searching after its symtable(s) have
  1814. been checked (can be both the static and the global symtable
  1815. in case it's the current unit itself) }
  1816. if explicitunit and
  1817. (FProcsymtable.symtabletype in [globalsymtable,staticsymtable]) and
  1818. (srsymtable.moduleid<>FProcsymtable.moduleid) then
  1819. break;
  1820. if srsymtable.symtabletype in [localsymtable,staticsymtable,globalsymtable] then
  1821. begin
  1822. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  1823. if assigned(srsym) and
  1824. (srsym.typ=procsym) then
  1825. begin
  1826. { Store first procsym found }
  1827. if not assigned(FProcsym) then
  1828. FProcsym:=tprocsym(srsym);
  1829. { add all definitions }
  1830. hasoverload:=false;
  1831. for j:=0 to tprocsym(srsym).ProcdefList.Count-1 do
  1832. begin
  1833. pd:=tprocdef(tprocsym(srsym).ProcdefList[j]);
  1834. if po_overload in pd.procoptions then
  1835. hasoverload:=true;
  1836. ProcdefOverloadList.Add(tprocsym(srsym).ProcdefList[j]);
  1837. end;
  1838. { when there is no explicit overload we stop searching,
  1839. except for Objective-C methods called via id }
  1840. if not hasoverload and
  1841. not objcidcall then
  1842. break;
  1843. end;
  1844. end;
  1845. checkstack:=checkstack^.next
  1846. end;
  1847. end;
  1848. procedure tcallcandidates.create_candidate_list(ignorevisibility,allowdefaultparas,objcidcall,explicitunit,searchhelpers,anoninherited:boolean);
  1849. var
  1850. j : integer;
  1851. pd : tprocdef;
  1852. hp : pcandidate;
  1853. pt : tcallparanode;
  1854. found : boolean;
  1855. st : TSymtable;
  1856. contextstructdef : tabstractrecorddef;
  1857. ProcdefOverloadList : TFPObjectList;
  1858. cpoptions : tcompare_paras_options;
  1859. begin
  1860. FCandidateProcs:=nil;
  1861. { Find all available overloads for this procsym }
  1862. ProcdefOverloadList:=TFPObjectList.Create(false);
  1863. if not objcidcall and
  1864. (FOperator=NOTOKEN) and
  1865. (FProcsym.owner.symtabletype in [objectsymtable,recordsymtable]) then
  1866. collect_overloads_in_struct(tabstractrecorddef(FProcsym.owner.defowner),ProcdefOverloadList,searchhelpers,anoninherited)
  1867. else
  1868. if (FOperator<>NOTOKEN) then
  1869. begin
  1870. { check operands and if they contain records then search in records,
  1871. then search in unit }
  1872. pt:=tcallparanode(FParaNode);
  1873. while assigned(pt) do
  1874. begin
  1875. if (pt.resultdef.typ=recorddef) then
  1876. collect_overloads_in_struct(tabstractrecorddef(pt.resultdef),ProcdefOverloadList,searchhelpers,anoninherited);
  1877. pt:=tcallparanode(pt.right);
  1878. end;
  1879. collect_overloads_in_units(ProcdefOverloadList,objcidcall,explicitunit);
  1880. end
  1881. else
  1882. collect_overloads_in_units(ProcdefOverloadList,objcidcall,explicitunit);
  1883. { determine length of parameter list.
  1884. for operators also enable the variant-operators if
  1885. a variant parameter is passed }
  1886. FParalength:=0;
  1887. FAllowVariant:=(FOperator=NOTOKEN);
  1888. pt:=tcallparanode(FParaNode);
  1889. while assigned(pt) do
  1890. begin
  1891. if (pt.resultdef.typ=variantdef) then
  1892. FAllowVariant:=true;
  1893. inc(FParalength);
  1894. pt:=tcallparanode(pt.right);
  1895. end;
  1896. { when the class passed is defined in this unit we
  1897. need to use the scope of that class. This is a trick
  1898. that can be used to access protected members in other
  1899. units. At least kylix supports it this way (PFV) }
  1900. if assigned(FProcSymtable) and
  1901. (
  1902. (FProcSymtable.symtabletype in [ObjectSymtable,recordsymtable]) or
  1903. ((FProcSymtable.symtabletype=withsymtable) and
  1904. (FProcSymtable.defowner.typ in [objectdef,recorddef]))
  1905. ) and
  1906. (FProcSymtable.defowner.owner.symtabletype in [globalsymtable,staticsymtable,objectsymtable,recordsymtable]) and
  1907. FProcSymtable.defowner.owner.iscurrentunit then
  1908. contextstructdef:=tabstractrecorddef(FProcSymtable.defowner)
  1909. else
  1910. contextstructdef:=current_structdef;
  1911. { symtable is needed later to calculate the distance }
  1912. if assigned(FProcsym) then
  1913. st:=FProcsym.Owner
  1914. else
  1915. st:=nil;
  1916. { Process all found overloads }
  1917. for j:=0 to ProcdefOverloadList.Count-1 do
  1918. begin
  1919. pd:=tprocdef(ProcdefOverloadList[j]);
  1920. { only when the # of parameter are supported by the procedure and
  1921. it is visible }
  1922. if (FParalength>=pd.minparacount) and
  1923. (
  1924. (
  1925. allowdefaultparas and
  1926. (
  1927. (FParalength<=pd.maxparacount) or
  1928. (po_varargs in pd.procoptions)
  1929. )
  1930. ) or
  1931. (
  1932. not allowdefaultparas and
  1933. (FParalength=pd.maxparacount)
  1934. )
  1935. ) and
  1936. (
  1937. ignorevisibility or
  1938. not (pd.owner.symtabletype in [objectsymtable,recordsymtable]) or
  1939. is_visible_for_object(pd,contextstructdef)
  1940. ) then
  1941. begin
  1942. { don't add duplicates, only compare visible parameters for the user }
  1943. cpoptions:=[cpo_ignorehidden];
  1944. if (po_compilerproc in pd.procoptions) then
  1945. cpoptions:=cpoptions+[cpo_compilerproc];
  1946. if (po_rtlproc in pd.procoptions) then
  1947. cpoptions:=cpoptions+[cpo_rtlproc];
  1948. found:=false;
  1949. hp:=FCandidateProcs;
  1950. while assigned(hp) do
  1951. begin
  1952. if (compare_paras(hp^.data.paras,pd.paras,cp_value_equal_const,cpoptions)>=te_equal) and
  1953. (not(po_objc in pd.procoptions) or
  1954. (pd.messageinf.str^=hp^.data.messageinf.str^)) then
  1955. begin
  1956. found:=true;
  1957. break;
  1958. end;
  1959. hp:=hp^.next;
  1960. end;
  1961. if not found then
  1962. proc_add(st,pd,objcidcall);
  1963. end;
  1964. end;
  1965. ProcdefOverloadList.Free;
  1966. end;
  1967. function tcallcandidates.proc_add(st:tsymtable;pd:tprocdef;objcidcall: boolean):pcandidate;
  1968. var
  1969. defaultparacnt : integer;
  1970. begin
  1971. { generate new candidate entry }
  1972. new(result);
  1973. fillchar(result^,sizeof(tcandidate),0);
  1974. result^.data:=pd;
  1975. result^.next:=FCandidateProcs;
  1976. FCandidateProcs:=result;
  1977. inc(FProccnt);
  1978. { Find last parameter, skip all default parameters
  1979. that are not passed. Ignore this skipping for varargs }
  1980. result^.firstparaidx:=pd.paras.count-1;
  1981. if not(po_varargs in pd.procoptions) then
  1982. begin
  1983. { ignore hidden parameters }
  1984. while (result^.firstparaidx>=0) and (vo_is_hidden_para in tparavarsym(pd.paras[result^.firstparaidx]).varoptions) do
  1985. dec(result^.firstparaidx);
  1986. defaultparacnt:=pd.maxparacount-FParalength;
  1987. if defaultparacnt>0 then
  1988. begin
  1989. if defaultparacnt>result^.firstparaidx+1 then
  1990. internalerror(200401141);
  1991. dec(result^.firstparaidx,defaultparacnt);
  1992. end;
  1993. end;
  1994. { Give a small penalty for overloaded methods not in
  1995. defined the current class/unit }
  1996. { when calling Objective-C methods via id.method, then the found
  1997. procsym will be inside an arbitrary ObjectSymtable, and we don't
  1998. want togive the methods of that particular objcclass precedence over
  1999. other methods, so instead check against the symtable in which this
  2000. objcclass is defined }
  2001. if objcidcall then
  2002. st:=st.defowner.owner;
  2003. if (st<>pd.owner) then
  2004. result^.ordinal_distance:=result^.ordinal_distance+1.0;
  2005. end;
  2006. procedure tcallcandidates.list(all:boolean);
  2007. var
  2008. hp : pcandidate;
  2009. begin
  2010. hp:=FCandidateProcs;
  2011. while assigned(hp) do
  2012. begin
  2013. if all or
  2014. (not hp^.invalid) then
  2015. MessagePos1(hp^.data.fileinfo,sym_h_param_list,hp^.data.fullprocname(false));
  2016. hp:=hp^.next;
  2017. end;
  2018. end;
  2019. {$ifdef EXTDEBUG}
  2020. procedure tcallcandidates.dump_info(lvl:longint);
  2021. function ParaTreeStr(p:tcallparanode):string;
  2022. begin
  2023. result:='';
  2024. while assigned(p) do
  2025. begin
  2026. if result<>'' then
  2027. result:=','+result;
  2028. result:=p.resultdef.typename+result;
  2029. p:=tcallparanode(p.right);
  2030. end;
  2031. end;
  2032. var
  2033. hp : pcandidate;
  2034. i : integer;
  2035. currpara : tparavarsym;
  2036. begin
  2037. if not CheckVerbosity(lvl) then
  2038. exit;
  2039. Comment(lvl+V_LineInfo,'Overloaded callnode: '+FProcsym.name+'('+ParaTreeStr(tcallparanode(FParaNode))+')');
  2040. hp:=FCandidateProcs;
  2041. while assigned(hp) do
  2042. begin
  2043. Comment(lvl,' '+hp^.data.fullprocname(false));
  2044. if (hp^.invalid) then
  2045. Comment(lvl,' invalid')
  2046. else
  2047. begin
  2048. Comment(lvl,' ex: '+tostr(hp^.exact_count)+
  2049. ' eq: '+tostr(hp^.equal_count)+
  2050. ' l1: '+tostr(hp^.cl1_count)+
  2051. ' l2: '+tostr(hp^.cl2_count)+
  2052. ' l3: '+tostr(hp^.cl3_count)+
  2053. ' l4: '+tostr(hp^.cl4_count)+
  2054. ' l5: '+tostr(hp^.cl5_count)+
  2055. ' oper: '+tostr(hp^.coper_count)+
  2056. ' ord: '+realtostr(hp^.ordinal_distance));
  2057. { Print parameters in left-right order }
  2058. for i:=0 to hp^.data.paras.count-1 do
  2059. begin
  2060. currpara:=tparavarsym(hp^.data.paras[i]);
  2061. if not(vo_is_hidden_para in currpara.varoptions) then
  2062. Comment(lvl,' - '+currpara.vardef.typename+' : '+EqualTypeName[currpara.eqval]);
  2063. end;
  2064. end;
  2065. hp:=hp^.next;
  2066. end;
  2067. end;
  2068. {$endif EXTDEBUG}
  2069. procedure tcallcandidates.get_information;
  2070. var
  2071. hp : pcandidate;
  2072. currpara : tparavarsym;
  2073. paraidx : integer;
  2074. currparanr : byte;
  2075. rfh,rth : double;
  2076. objdef : tobjectdef;
  2077. def_from,
  2078. def_to : tdef;
  2079. currpt,
  2080. pt : tcallparanode;
  2081. eq : tequaltype;
  2082. convtype : tconverttype;
  2083. pdtemp,
  2084. pdoper : tprocdef;
  2085. releasecurrpt : boolean;
  2086. cdoptions : tcompare_defs_options;
  2087. n : tnode;
  2088. {$push}
  2089. {$r-}
  2090. {$q-}
  2091. const
  2092. inf=1.0/0.0;
  2093. {$pop}
  2094. begin
  2095. cdoptions:=[cdo_check_operator];
  2096. if FAllowVariant then
  2097. include(cdoptions,cdo_allow_variant);
  2098. { process all procs }
  2099. hp:=FCandidateProcs;
  2100. while assigned(hp) do
  2101. begin
  2102. { We compare parameters in reverse order (right to left),
  2103. the firstpara is already pointing to the last parameter
  2104. were we need to start comparing }
  2105. currparanr:=FParalength;
  2106. paraidx:=hp^.firstparaidx;
  2107. while (paraidx>=0) and (vo_is_hidden_para in tparavarsym(hp^.data.paras[paraidx]).varoptions) do
  2108. dec(paraidx);
  2109. pt:=tcallparanode(FParaNode);
  2110. while assigned(pt) and (paraidx>=0) do
  2111. begin
  2112. currpara:=tparavarsym(hp^.data.paras[paraidx]);
  2113. { currpt can be changed from loadn to calln when a procvar
  2114. is passed. This is to prevent that the change is permanent }
  2115. currpt:=pt;
  2116. releasecurrpt:=false;
  2117. { retrieve current parameter definitions to compares }
  2118. eq:=te_incompatible;
  2119. def_from:=currpt.resultdef;
  2120. def_to:=currpara.vardef;
  2121. if not(assigned(def_from)) then
  2122. internalerror(200212091);
  2123. if not(
  2124. assigned(def_to) or
  2125. ((po_varargs in hp^.data.procoptions) and
  2126. (currparanr>hp^.data.minparacount))
  2127. ) then
  2128. internalerror(200212092);
  2129. { Convert tp procvars when not expecting a procvar }
  2130. if (currpt.left.resultdef.typ=procvardef) and
  2131. not(def_to.typ in [procvardef,formaldef]) and
  2132. { Only convert to call when there is no overload or the return type
  2133. is equal to the expected type. }
  2134. (
  2135. (count=1) or
  2136. equal_defs(tprocvardef(currpt.left.resultdef).returndef,def_to)
  2137. ) then
  2138. begin
  2139. releasecurrpt:=true;
  2140. currpt:=tcallparanode(pt.getcopy);
  2141. if maybe_call_procvar(currpt.left,true) then
  2142. begin
  2143. currpt.resultdef:=currpt.left.resultdef;
  2144. def_from:=currpt.left.resultdef;
  2145. end;
  2146. end;
  2147. { If we expect a procvar and the left is loadnode that
  2148. returns a procdef we need to find the correct overloaded
  2149. procdef that matches the expected procvar. The loadnode
  2150. temporary returned the first procdef (PFV) }
  2151. if (def_to.typ=procvardef) and
  2152. (currpt.left.nodetype=loadn) and
  2153. (currpt.left.resultdef.typ=procdef) then
  2154. begin
  2155. pdtemp:=tprocsym(Tloadnode(currpt.left).symtableentry).Find_procdef_byprocvardef(Tprocvardef(def_to));
  2156. if assigned(pdtemp) then
  2157. begin
  2158. tloadnode(currpt.left).setprocdef(pdtemp);
  2159. currpt.resultdef:=currpt.left.resultdef;
  2160. def_from:=currpt.left.resultdef;
  2161. end;
  2162. end;
  2163. { varargs are always equal, but not exact }
  2164. if (po_varargs in hp^.data.procoptions) and
  2165. (currparanr>hp^.data.minparacount) and
  2166. not is_array_of_const(def_from) and
  2167. not is_array_constructor(def_from) then
  2168. eq:=te_equal
  2169. else
  2170. { same definition -> exact }
  2171. if (def_from=def_to) then
  2172. eq:=te_exact
  2173. else
  2174. { for value and const parameters check if a integer is constant or
  2175. included in other integer -> equal and calc ordinal_distance }
  2176. if not(currpara.varspez in [vs_var,vs_out]) and
  2177. is_integer(def_from) and
  2178. is_integer(def_to) and
  2179. is_in_limit(def_from,def_to) then
  2180. begin
  2181. eq:=te_equal;
  2182. hp^.ordinal_distance:=hp^.ordinal_distance+
  2183. abs(bestreal(torddef(def_from).low)-bestreal(torddef(def_to).low));
  2184. rth:=bestreal(torddef(def_to).high);
  2185. rfh:=bestreal(torddef(def_from).high);
  2186. hp^.ordinal_distance:=hp^.ordinal_distance+abs(rth-rfh);
  2187. { Give wrong sign a small penalty, this is need to get a diffrence
  2188. from word->[longword,longint] }
  2189. if is_signed(def_from)<>is_signed(def_to) then
  2190. {$push}
  2191. {$r-}
  2192. {$q-}
  2193. hp^.ordinal_distance:=nextafter(hp^.ordinal_distance,inf);
  2194. {$pop}
  2195. end
  2196. else
  2197. { for value and const parameters check precision of real, give
  2198. penalty for loosing of precision. var and out parameters must match exactly }
  2199. if not(currpara.varspez in [vs_var,vs_out]) and
  2200. is_real(def_from) and
  2201. is_real(def_to) then
  2202. begin
  2203. eq:=te_equal;
  2204. if is_extended(def_to) then
  2205. rth:=4
  2206. else
  2207. if is_double (def_to) then
  2208. rth:=2
  2209. else
  2210. rth:=1;
  2211. if is_extended(def_from) then
  2212. rfh:=4
  2213. else
  2214. if is_double (def_from) then
  2215. rfh:=2
  2216. else
  2217. rfh:=1;
  2218. { penalty for shrinking of precision }
  2219. if rth<rfh then
  2220. rfh:=(rfh-rth)*16
  2221. else
  2222. rfh:=rth-rfh;
  2223. hp^.ordinal_distance:=hp^.ordinal_distance+rfh;
  2224. end
  2225. else
  2226. { related object parameters also need to determine the distance between the current
  2227. object and the object we are comparing with. var and out parameters must match exactly }
  2228. if not(currpara.varspez in [vs_var,vs_out]) and
  2229. (def_from.typ=objectdef) and
  2230. (def_to.typ=objectdef) and
  2231. (tobjectdef(def_from).objecttype=tobjectdef(def_to).objecttype) and
  2232. tobjectdef(def_from).is_related(tobjectdef(def_to)) then
  2233. begin
  2234. eq:=te_convert_l1;
  2235. objdef:=tobjectdef(def_from);
  2236. while assigned(objdef) do
  2237. begin
  2238. if objdef=def_to then
  2239. break;
  2240. hp^.ordinal_distance:=hp^.ordinal_distance+1;
  2241. objdef:=objdef.childof;
  2242. end;
  2243. end
  2244. { compare_defs_ext compares sets and array constructors very poorly because
  2245. it has too little information. So we do explicitly a detailed comparisation,
  2246. see also bug #11288 (FK)
  2247. }
  2248. else if (def_to.typ=setdef) and is_array_constructor(currpt.left.resultdef) then
  2249. begin
  2250. n:=currpt.left.getcopy;
  2251. arrayconstructor_to_set(n);
  2252. eq:=compare_defs_ext(n.resultdef,def_to,n.nodetype,convtype,pdoper,cdoptions);
  2253. n.free;
  2254. end
  2255. else
  2256. { generic type comparision }
  2257. begin
  2258. if not(po_compilerproc in hp^.data.procoptions) and
  2259. not(po_rtlproc in hp^.data.procoptions) and
  2260. is_ansistring(currpara.vardef) and
  2261. is_ansistring(currpt.left.resultdef) and
  2262. (tstringdef(currpara.vardef).encoding<>tstringdef(currpt.left.resultdef).encoding) and
  2263. ((tstringdef(currpara.vardef).encoding=globals.CP_NONE) or
  2264. (tstringdef(currpt.left.resultdef).encoding=globals.CP_NONE)
  2265. ) then
  2266. eq:=te_convert_l1
  2267. else
  2268. eq:=compare_defs_ext(def_from,def_to,currpt.left.nodetype,convtype,pdoper,cdoptions);
  2269. { when the types are not equal we need to check
  2270. some special case for parameter passing }
  2271. if (eq<te_equal) then
  2272. begin
  2273. if currpara.varspez in [vs_var,vs_out] then
  2274. begin
  2275. { para requires an equal type so the previous found
  2276. match was not good enough, reset to incompatible }
  2277. eq:=te_incompatible;
  2278. { var_para_allowed will return te_equal and te_convert_l1 to
  2279. make a difference for best matching }
  2280. var_para_allowed(eq,currpt.resultdef,currpara.vardef,currpt.left)
  2281. end
  2282. else
  2283. para_allowed(eq,currpt,def_to);
  2284. end;
  2285. end;
  2286. { univ parameters match if the size matches (don't override the
  2287. comparison result if it was ok, since a match based on the
  2288. "univ" character is the lowest possible match) }
  2289. if (eq=te_incompatible) and
  2290. currpara.univpara and
  2291. is_valid_univ_para_type(def_from) and
  2292. (def_from.size=def_to.size) then
  2293. eq:=te_convert_l5;
  2294. { when a procvar was changed to a call an exact match is
  2295. downgraded to equal. This way an overload call with the
  2296. procvar is choosen. See tb0471 (PFV) }
  2297. if (pt<>currpt) and (eq=te_exact) then
  2298. eq:=te_equal;
  2299. { increase correct counter }
  2300. case eq of
  2301. te_exact :
  2302. inc(hp^.exact_count);
  2303. te_equal :
  2304. inc(hp^.equal_count);
  2305. te_convert_l1 :
  2306. inc(hp^.cl1_count);
  2307. te_convert_l2 :
  2308. inc(hp^.cl2_count);
  2309. te_convert_l3 :
  2310. inc(hp^.cl3_count);
  2311. te_convert_l4 :
  2312. inc(hp^.cl4_count);
  2313. te_convert_l5 :
  2314. inc(hp^.cl5_count);
  2315. te_convert_operator :
  2316. inc(hp^.coper_count);
  2317. te_incompatible :
  2318. hp^.invalid:=true;
  2319. else
  2320. internalerror(200212072);
  2321. end;
  2322. { stop checking when an incompatible parameter is found }
  2323. if hp^.invalid then
  2324. begin
  2325. { store the current parameter info for
  2326. a nice error message when no procedure is found }
  2327. hp^.wrongparaidx:=paraidx;
  2328. hp^.wrongparanr:=currparanr;
  2329. break;
  2330. end;
  2331. {$ifdef EXTDEBUG}
  2332. { store equal in node tree for dump }
  2333. currpara.eqval:=eq;
  2334. {$endif EXTDEBUG}
  2335. { maybe release temp currpt }
  2336. if releasecurrpt then
  2337. currpt.free;
  2338. { next parameter in the call tree }
  2339. pt:=tcallparanode(pt.right);
  2340. { next parameter for definition, only goto next para
  2341. if we're out of the varargs }
  2342. if not(po_varargs in hp^.data.procoptions) or
  2343. (currparanr<=hp^.data.maxparacount) then
  2344. begin
  2345. { Ignore vs_hidden parameters }
  2346. repeat
  2347. dec(paraidx);
  2348. until (paraidx<0) or not(vo_is_hidden_para in tparavarsym(hp^.data.paras[paraidx]).varoptions);
  2349. end;
  2350. dec(currparanr);
  2351. end;
  2352. if not(hp^.invalid) and
  2353. (assigned(pt) or (paraidx>=0) or (currparanr<>0)) then
  2354. internalerror(200212141);
  2355. { next candidate }
  2356. hp:=hp^.next;
  2357. end;
  2358. end;
  2359. function get_variantequaltype(def: tdef): tvariantequaltype;
  2360. const
  2361. variantorddef_cl: array[tordtype] of tvariantequaltype =
  2362. (tve_incompatible,tve_byte,tve_word,tve_cardinal,tve_chari64,
  2363. tve_shortint,tve_smallint,tve_longint,tve_chari64,
  2364. tve_boolformal,tve_boolformal,tve_boolformal,tve_boolformal,
  2365. tve_boolformal,tve_boolformal,tve_boolformal,tve_boolformal,
  2366. tve_chari64,tve_chari64,tve_dblcurrency);
  2367. { TODO: fixme for 128 bit floats }
  2368. variantfloatdef_cl: array[tfloattype] of tvariantequaltype =
  2369. (tve_single,tve_dblcurrency,tve_extended,tve_extended,
  2370. tve_dblcurrency,tve_dblcurrency,tve_extended);
  2371. variantstringdef_cl: array[tstringtype] of tvariantequaltype =
  2372. (tve_sstring,tve_astring,tve_astring,tve_wstring,tve_ustring);
  2373. begin
  2374. case def.typ of
  2375. orddef:
  2376. begin
  2377. result:=variantorddef_cl[torddef(def).ordtype];
  2378. end;
  2379. floatdef:
  2380. begin
  2381. result:=variantfloatdef_cl[tfloatdef(def).floattype];
  2382. end;
  2383. stringdef:
  2384. begin
  2385. result:=variantstringdef_cl[tstringdef(def).stringtype];
  2386. end;
  2387. formaldef:
  2388. begin
  2389. result:=tve_boolformal;
  2390. end;
  2391. else
  2392. begin
  2393. result:=tve_incompatible;
  2394. end;
  2395. end
  2396. end;
  2397. function is_better_candidate(currpd,bestpd:pcandidate):integer;
  2398. var
  2399. res : integer;
  2400. begin
  2401. {
  2402. Return values:
  2403. > 0 when currpd is better than bestpd
  2404. < 0 when bestpd is better than currpd
  2405. = 0 when both are equal
  2406. To choose the best candidate we use the following order:
  2407. - Incompatible flag
  2408. - (Smaller) Number of convert operator parameters.
  2409. - (Smaller) Number of convertlevel 2 parameters.
  2410. - (Smaller) Number of convertlevel 1 parameters.
  2411. - (Bigger) Number of exact parameters.
  2412. - (Smaller) Number of equal parameters.
  2413. - (Smaller) Total of ordinal distance. For example, the distance of a word
  2414. to a byte is 65535-255=65280.
  2415. }
  2416. if bestpd^.invalid then
  2417. begin
  2418. if currpd^.invalid then
  2419. res:=0
  2420. else
  2421. res:=1;
  2422. end
  2423. else
  2424. if currpd^.invalid then
  2425. res:=-1
  2426. else
  2427. begin
  2428. { less operator parameters? }
  2429. res:=(bestpd^.coper_count-currpd^.coper_count);
  2430. if (res=0) then
  2431. begin
  2432. { less cl5 parameters? }
  2433. res:=(bestpd^.cl5_count-currpd^.cl5_count);
  2434. if (res=0) then
  2435. begin
  2436. { less cl4 parameters? }
  2437. res:=(bestpd^.cl4_count-currpd^.cl4_count);
  2438. if (res=0) then
  2439. begin
  2440. { less cl3 parameters? }
  2441. res:=(bestpd^.cl3_count-currpd^.cl3_count);
  2442. if (res=0) then
  2443. begin
  2444. { less cl2 parameters? }
  2445. res:=(bestpd^.cl2_count-currpd^.cl2_count);
  2446. if (res=0) then
  2447. begin
  2448. { less cl1 parameters? }
  2449. res:=(bestpd^.cl1_count-currpd^.cl1_count);
  2450. if (res=0) then
  2451. begin
  2452. { more exact parameters? }
  2453. res:=(currpd^.exact_count-bestpd^.exact_count);
  2454. if (res=0) then
  2455. begin
  2456. { less equal parameters? }
  2457. res:=(bestpd^.equal_count-currpd^.equal_count);
  2458. if (res=0) then
  2459. begin
  2460. { smaller ordinal distance? }
  2461. if (currpd^.ordinal_distance<bestpd^.ordinal_distance) then
  2462. res:=1
  2463. else
  2464. if (currpd^.ordinal_distance>bestpd^.ordinal_distance) then
  2465. res:=-1
  2466. else
  2467. res:=0;
  2468. end;
  2469. end;
  2470. end;
  2471. end;
  2472. end;
  2473. end;
  2474. end;
  2475. end;
  2476. end;
  2477. is_better_candidate:=res;
  2478. end;
  2479. { Delphi precedence rules extracted from test programs. Only valid if passing
  2480. a variant parameter to overloaded procedures expecting exactly one parameter.
  2481. single > (char, currency, int64, shortstring, ansistring, widestring, extended, double)
  2482. double/currency > (char, int64, shortstring, ansistring, widestring, extended)
  2483. extended > (char, int64, shortstring, ansistring, widestring)
  2484. longint/cardinal > (int64, shortstring, ansistring, widestring, extended, double, single, char, currency)
  2485. smallint > (longint, int64, shortstring, ansistring, widestring, extended, double single, char, currency);
  2486. word > (longint, cardinal, int64, shortstring, ansistring, widestring, extended, double single, char, currency);
  2487. shortint > (longint, smallint, int64, shortstring, ansistring, widestring, extended, double, single, char, currency)
  2488. byte > (longint, cardinal, word, smallint, int64, shortstring, ansistring, widestring, extended, double, single, char, currency);
  2489. boolean/formal > (char, int64, shortstring, ansistring, widestring)
  2490. shortstring > (char, int64, ansistring, widestring)
  2491. ansistring > (char, int64, widestring)
  2492. widestring > (char, int64)
  2493. Relations not mentioned mean that they conflict: no decision possible }
  2494. function is_better_candidate_single_variant(currpd,bestpd:pcandidate):integer;
  2495. function calculate_relation(const currvcl, bestvcl, testvcl:
  2496. tvariantequaltype; const conflictvcls: tvariantequaltypes):integer;
  2497. begin
  2498. { if (bestvcl=conflictvcl) or
  2499. (currvcl=conflictvcl) then
  2500. result:=0
  2501. else if (bestvcl=testvcl) then
  2502. result:=-1
  2503. else result:=1 }
  2504. result:=1-2*ord(bestvcl=testvcl)+
  2505. ord(currvcl in conflictvcls)-ord(bestvcl in conflictvcls);
  2506. end;
  2507. function getfirstrealparaidx(pd: pcandidate): integer;
  2508. begin
  2509. { can be different for currpd and bestpd in case of overloaded }
  2510. { functions, e.g. lowercase():char and lowercase():shortstring }
  2511. { (depending on the calling convention and parameter order) }
  2512. result:=pd^.firstparaidx;
  2513. while (result>=0) and (vo_is_hidden_para in tparavarsym(pd^.data.paras[result]).varoptions) do
  2514. dec(result);
  2515. if (vo_is_hidden_para in tparavarsym(pd^.data.paras[result]).varoptions) then
  2516. internalerror(2006122803);
  2517. end;
  2518. var
  2519. currpara, bestpara: tparavarsym;
  2520. currvcl, bestvcl: tvariantequaltype;
  2521. begin
  2522. {
  2523. Return values:
  2524. > 0 when currpd is better than bestpd
  2525. < 0 when bestpd is better than currpd
  2526. = 0 when both are equal
  2527. }
  2528. currpara:=tparavarsym(currpd^.data.paras[getfirstrealparaidx(currpd)]);
  2529. bestpara:=tparavarsym(bestpd^.data.paras[getfirstrealparaidx(bestpd)]);
  2530. { if one of the parameters is a regular variant, fall back to the }
  2531. { default algorithm }
  2532. if (currpara.vardef.typ = variantdef) or
  2533. (bestpara.vardef.typ = variantdef) then
  2534. begin
  2535. result:=is_better_candidate(currpd,bestpd);
  2536. exit;
  2537. end;
  2538. currvcl:=get_variantequaltype(currpara.vardef);
  2539. bestvcl:=get_variantequaltype(bestpara.vardef);
  2540. { sanity check }
  2541. result:=-5;
  2542. { if both are the same, there is a conflict }
  2543. if (currvcl=bestvcl) then
  2544. result:=0
  2545. { if one of the two cannot be used as variant, the other is better }
  2546. else if (bestvcl=tve_incompatible) then
  2547. result:=1
  2548. else if (currvcl=tve_incompatible) then
  2549. result:=-1
  2550. { boolean and formal are better than chari64str, but conflict with }
  2551. { everything else }
  2552. else if (currvcl=tve_boolformal) or
  2553. (bestvcl=tve_boolformal) then
  2554. if (currvcl=tve_boolformal) then
  2555. result:=ord(bestvcl in [tve_chari64,tve_sstring,tve_astring,tve_wstring,tve_ustring])
  2556. else
  2557. result:=-ord(currvcl in [tve_chari64,tve_sstring,tve_astring,tve_wstring,tve_ustring])
  2558. { byte is better than everything else (we assume both aren't byte, }
  2559. { since there's only one parameter and that one can't be the same) }
  2560. else if (currvcl=tve_byte) or
  2561. (bestvcl=tve_byte) then
  2562. result:=calculate_relation(currvcl,bestvcl,tve_byte,[tve_shortint])
  2563. { shortint conflicts with word and cardinal, but is better than }
  2564. { everything else but byte (which has already been handled) }
  2565. else if (currvcl=tve_shortint) or
  2566. (bestvcl=tve_shortint) then
  2567. result:=calculate_relation(currvcl,bestvcl,tve_shortint,[tve_word, tve_cardinal])
  2568. { word conflicts with smallint, but is better than everything else }
  2569. { but shortint and byte (which has already been handled) }
  2570. else if (currvcl=tve_word) or
  2571. (bestvcl=tve_word) then
  2572. result:=calculate_relation(currvcl,bestvcl,tve_word,[tve_smallint])
  2573. { smallint conflicts with cardinal, but is better than everything }
  2574. { which has not yet been tested }
  2575. else if (currvcl=tve_smallint) or
  2576. (bestvcl=tve_smallint) then
  2577. result:=calculate_relation(currvcl,bestvcl,tve_smallint,[tve_cardinal])
  2578. { cardinal conflicts with each longint and is better than everything }
  2579. { which has not yet been tested }
  2580. else if (currvcl=tve_cardinal) or
  2581. (bestvcl=tve_cardinal) then
  2582. result:=calculate_relation(currvcl,bestvcl,tve_cardinal,[tve_longint])
  2583. { longint is better than everything which has not yet been tested }
  2584. else if (currvcl=tve_longint) or
  2585. (bestvcl=tve_longint) then
  2586. { if bestvcl=tve_longint then
  2587. result:=-1
  2588. else
  2589. result:=1 }
  2590. result:=1-2*ord(bestvcl=tve_longint)
  2591. { single is better than everything left }
  2592. else if (currvcl=tve_single) or
  2593. (bestvcl=tve_single) then
  2594. result:=1-2*ord(bestvcl=tve_single)
  2595. { double/comp/currency are better than everything left, and conflict }
  2596. { with each other (but that's already tested) }
  2597. else if (currvcl=tve_dblcurrency) or
  2598. (bestvcl=tve_dblcurrency) then
  2599. result:=1-2*ord(bestvcl=tve_dblcurrency)
  2600. { extended is better than everything left }
  2601. else if (currvcl=tve_extended) or
  2602. (bestvcl=tve_extended) then
  2603. result:=1-2*ord(bestvcl=tve_extended)
  2604. { shortstring is better than everything left }
  2605. else if (currvcl=tve_sstring) or
  2606. (bestvcl=tve_sstring) then
  2607. result:=1-2*ord(bestvcl=tve_sstring)
  2608. { ansistring is better than everything left }
  2609. else if (currvcl=tve_astring) or
  2610. (bestvcl=tve_astring) then
  2611. result:=1-2*ord(bestvcl=tve_astring)
  2612. { widestring is better than everything left }
  2613. else if (currvcl=tve_wstring) or
  2614. (bestvcl=tve_wstring) then
  2615. result:=1-2*ord(bestvcl=tve_wstring)
  2616. { unicodestring is better than everything left }
  2617. else if (currvcl=tve_ustring) or
  2618. (bestvcl=tve_ustring) then
  2619. result:=1-2*ord(bestvcl=tve_ustring);
  2620. { all possibilities should have been checked now }
  2621. if (result=-5) then
  2622. internalerror(2006122805);
  2623. end;
  2624. function tcallcandidates.choose_best(var bestpd:tabstractprocdef; singlevariant: boolean):integer;
  2625. var
  2626. besthpstart,
  2627. hp : pcandidate;
  2628. cntpd,
  2629. res : integer;
  2630. begin
  2631. {
  2632. Returns the number of candidates left and the
  2633. first candidate is returned in pdbest
  2634. }
  2635. { Setup the first procdef as best, only count it as a result
  2636. when it is valid }
  2637. bestpd:=FCandidateProcs^.data;
  2638. if FCandidateProcs^.invalid then
  2639. cntpd:=0
  2640. else
  2641. cntpd:=1;
  2642. if assigned(FCandidateProcs^.next) then
  2643. begin
  2644. besthpstart:=FCandidateProcs;
  2645. hp:=FCandidateProcs^.next;
  2646. while assigned(hp) do
  2647. begin
  2648. if not singlevariant then
  2649. res:=is_better_candidate(hp,besthpstart)
  2650. else
  2651. res:=is_better_candidate_single_variant(hp,besthpstart);
  2652. if (res>0) then
  2653. begin
  2654. { hp is better, flag all procs to be incompatible }
  2655. while (besthpstart<>hp) do
  2656. begin
  2657. besthpstart^.invalid:=true;
  2658. besthpstart:=besthpstart^.next;
  2659. end;
  2660. { besthpstart is already set to hp }
  2661. bestpd:=besthpstart^.data;
  2662. cntpd:=1;
  2663. end
  2664. else
  2665. if (res<0) then
  2666. begin
  2667. { besthpstart is better, flag current hp to be incompatible }
  2668. hp^.invalid:=true;
  2669. end
  2670. else
  2671. begin
  2672. { res=0, both are valid }
  2673. if not hp^.invalid then
  2674. inc(cntpd);
  2675. end;
  2676. hp:=hp^.next;
  2677. end;
  2678. end;
  2679. result:=cntpd;
  2680. end;
  2681. procedure tcallcandidates.find_wrong_para;
  2682. var
  2683. currparanr : smallint;
  2684. hp : pcandidate;
  2685. pt : tcallparanode;
  2686. wrongpara : tparavarsym;
  2687. begin
  2688. { Only process the first overloaded procdef }
  2689. hp:=FCandidateProcs;
  2690. { Find callparanode corresponding to the argument }
  2691. pt:=tcallparanode(FParanode);
  2692. currparanr:=FParalength;
  2693. while assigned(pt) and
  2694. (currparanr>hp^.wrongparanr) do
  2695. begin
  2696. pt:=tcallparanode(pt.right);
  2697. dec(currparanr);
  2698. end;
  2699. if (currparanr<>hp^.wrongparanr) or
  2700. not assigned(pt) then
  2701. internalerror(200212094);
  2702. { Show error message, when it was a var or out parameter
  2703. guess that it is a missing typeconv }
  2704. wrongpara:=tparavarsym(hp^.data.paras[hp^.wrongparaidx]);
  2705. if wrongpara.varspez in [vs_var,vs_out] then
  2706. begin
  2707. { Maybe passing the correct type but passing a const to var parameter }
  2708. if (compare_defs(pt.resultdef,wrongpara.vardef,pt.nodetype)<>te_incompatible) and
  2709. not valid_for_var(pt.left,true) then
  2710. CGMessagePos(pt.left.fileinfo,type_e_variable_id_expected)
  2711. else
  2712. CGMessagePos3(pt.left.fileinfo,parser_e_call_by_ref_without_typeconv,tostr(hp^.wrongparanr),
  2713. FullTypeName(pt.left.resultdef,wrongpara.vardef),
  2714. FullTypeName(wrongpara.vardef,pt.left.resultdef))
  2715. end
  2716. else
  2717. CGMessagePos3(pt.left.fileinfo,type_e_wrong_parameter_type,tostr(hp^.wrongparanr),
  2718. FullTypeName(pt.left.resultdef,wrongpara.vardef),
  2719. FullTypeName(wrongpara.vardef,pt.left.resultdef));
  2720. end;
  2721. procedure check_hints(const srsym: tsym; const symoptions: tsymoptions; const deprecatedmsg : pshortstring);
  2722. begin
  2723. if not assigned(srsym) then
  2724. internalerror(200602051);
  2725. if sp_hint_deprecated in symoptions then
  2726. if (sp_has_deprecated_msg in symoptions) and (deprecatedmsg <> nil) then
  2727. Message2(sym_w_deprecated_symbol_with_msg,srsym.realname,deprecatedmsg^)
  2728. else
  2729. Message1(sym_w_deprecated_symbol,srsym.realname);
  2730. if sp_hint_experimental in symoptions then
  2731. Message1(sym_w_experimental_symbol,srsym.realname);
  2732. if sp_hint_platform in symoptions then
  2733. Message1(sym_w_non_portable_symbol,srsym.realname);
  2734. if sp_hint_library in symoptions then
  2735. Message1(sym_w_library_symbol,srsym.realname);
  2736. if sp_hint_unimplemented in symoptions then
  2737. Message1(sym_w_non_implemented_symbol,srsym.realname);
  2738. end;
  2739. procedure check_ranges(const location: tfileposinfo; source: tnode; destdef: tdef);
  2740. begin
  2741. if not(cs_check_ordinal_size in current_settings.localswitches) then
  2742. exit;
  2743. { check if the assignment may cause a range check error }
  2744. { if its not explicit, and only if the values are }
  2745. { ordinals, enumdef and floatdef }
  2746. if assigned(destdef) and
  2747. (destdef.typ in [enumdef,orddef,floatdef]) and
  2748. not is_boolean(destdef) and
  2749. assigned(source.resultdef) and
  2750. (source.resultdef.typ in [enumdef,orddef,floatdef]) and
  2751. not is_boolean(source.resultdef) and
  2752. not is_constrealnode(source) then
  2753. begin
  2754. if ((destdef.size < source.resultdef.size) and
  2755. { s80real and sc80real have a different size but the same precision }
  2756. not((destdef.typ=floatdef) and
  2757. (source.resultdef.typ=floatdef) and
  2758. (tfloatdef(source.resultdef).floattype in [s80real,sc80real]) and
  2759. (tfloatdef(destdef).floattype in [s80real,sc80real]))) or
  2760. ((destdef.typ<>floatdef) and
  2761. (source.resultdef.typ<>floatdef) and
  2762. not is_in_limit(source.resultdef,destdef)) then
  2763. begin
  2764. if (cs_check_range in current_settings.localswitches) then
  2765. MessagePos(location,type_w_smaller_possible_range_check)
  2766. else
  2767. MessagePos(location,type_h_smaller_possible_range_check);
  2768. end;
  2769. end;
  2770. end;
  2771. end.