htypechk.pas 117 KB

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