2
0

htypechk.pas 84 KB

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