htypechk.pas 81 KB

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