htypechk.pas 100 KB

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