htypechk.pas 117 KB

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