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