htypechk.pas 100 KB

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