htypechk.pas 82 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218
  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. procedure check_hints(const srsym: tsym; const symoptions: tsymoptions);
  132. implementation
  133. uses
  134. globtype,systems,
  135. cutils,verbose,globals,
  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_written,vs_readwritten),
  665. { vs_declared -> ... }
  666. (vs_none,vs_declared,vs_initialised,vs_read,vs_written,vs_readwritten),
  667. { vs_initialised -> ... }
  668. (vs_none,vs_initialised,vs_initialised,vs_read,vs_written,vs_readwritten),
  669. { vs_read -> ... }
  670. (vs_none,vs_read,vs_read,vs_read,vs_readwritten,vs_readwritten),
  671. { vs_written -> ... }
  672. (vs_none,vs_written,vs_written,vs_readwritten,vs_written,vs_readwritten),
  673. { vs_readwritten -> ... }
  674. (vs_none,vs_readwritten,vs_readwritten,vs_readwritten,vs_readwritten,vs_readwritten));
  675. var
  676. hsym : tabstractvarsym;
  677. begin
  678. while assigned(p) do
  679. begin
  680. case p.nodetype of
  681. typeconvn :
  682. begin
  683. case ttypeconvnode(p).convtype of
  684. tc_cchar_2_pchar,
  685. tc_cstring_2_pchar,
  686. tc_array_2_pointer :
  687. exclude(varstateflags,vsf_must_be_valid);
  688. tc_pchar_2_string,
  689. tc_pointer_2_array :
  690. include(varstateflags,vsf_must_be_valid);
  691. end;
  692. p:=tunarynode(p).left;
  693. end;
  694. subscriptn :
  695. p:=tunarynode(p).left;
  696. vecn:
  697. begin
  698. set_varstate(tbinarynode(p).right,vs_read,[vsf_must_be_valid]);
  699. if (newstate in [vs_read,vs_readwritten]) or
  700. not(tunarynode(p).left.resulttype.def.deftype in [stringdef,arraydef]) then
  701. include(varstateflags,vsf_must_be_valid)
  702. else if (newstate = vs_written) then
  703. exclude(varstateflags,vsf_must_be_valid);
  704. p:=tunarynode(p).left;
  705. end;
  706. { do not parse calln }
  707. calln :
  708. break;
  709. loadn :
  710. begin
  711. if (tloadnode(p).symtableentry.typ in [localvarsym,paravarsym,globalvarsym]) then
  712. begin
  713. hsym:=tabstractvarsym(tloadnode(p).symtableentry);
  714. if (vsf_must_be_valid in varstateflags) and (hsym.varstate=vs_declared) then
  715. begin
  716. { Give warning/note for uninitialized locals }
  717. if assigned(hsym.owner) and
  718. not(vo_is_external in hsym.varoptions) and
  719. (hsym.owner.symtabletype in [parasymtable,localsymtable,staticsymtable]) and
  720. ((hsym.owner=current_procinfo.procdef.localst) or
  721. ((hsym.owner=current_procinfo.procdef.parast) and
  722. (vo_is_funcret in hsym.varoptions))) then
  723. begin
  724. if (vo_is_funcret in hsym.varoptions) then
  725. begin
  726. if (vsf_use_hints in varstateflags) then
  727. CGMessage(sym_h_function_result_uninitialized)
  728. else
  729. CGMessage(sym_w_function_result_uninitialized)
  730. end
  731. else
  732. begin
  733. if tloadnode(p).symtable.symtabletype=localsymtable then
  734. begin
  735. if (vsf_use_hints in varstateflags) then
  736. CGMessage1(sym_h_uninitialized_local_variable,hsym.realname)
  737. else
  738. CGMessage1(sym_w_uninitialized_local_variable,hsym.realname);
  739. end
  740. else
  741. begin
  742. if (vsf_use_hints in varstateflags) then
  743. CGMessage1(sym_h_uninitialized_variable,hsym.realname)
  744. else
  745. CGMessage1(sym_w_uninitialized_variable,hsym.realname);
  746. end;
  747. end;
  748. end;
  749. end;
  750. { don't override vs_readwritten with vs_initialised }
  751. hsym.varstate := vstrans[hsym.varstate,newstate];
  752. end;
  753. break;
  754. end;
  755. callparan :
  756. internalerror(200310081);
  757. else
  758. break;
  759. end;{case }
  760. end;
  761. end;
  762. procedure set_unique(p : tnode);
  763. begin
  764. while assigned(p) do
  765. begin
  766. case p.nodetype of
  767. vecn:
  768. begin
  769. include(p.flags,nf_callunique);
  770. break;
  771. end;
  772. typeconvn,
  773. subscriptn,
  774. derefn:
  775. p:=tunarynode(p).left;
  776. else
  777. break;
  778. end;
  779. end;
  780. end;
  781. function valid_for_assign(p:tnode;opts:TValidAssigns; report_errors: boolean):boolean;
  782. var
  783. hp : tnode;
  784. gotstring,
  785. gotwith,
  786. gotsubscript,
  787. gotrecord,
  788. gotpointer,
  789. gotvec,
  790. gotclass,
  791. gotdynarray,
  792. gotderef : boolean;
  793. fromdef,
  794. todef : tdef;
  795. errmsg : longint;
  796. begin
  797. if valid_const in opts then
  798. errmsg:=type_e_variable_id_expected
  799. else
  800. errmsg:=type_e_argument_cant_be_assigned;
  801. result:=false;
  802. gotsubscript:=false;
  803. gotvec:=false;
  804. gotderef:=false;
  805. gotrecord:=false;
  806. gotclass:=false;
  807. gotpointer:=false;
  808. gotwith:=false;
  809. gotdynarray:=false;
  810. gotstring:=false;
  811. hp:=p;
  812. if not(valid_void in opts) and
  813. is_void(hp.resulttype.def) then
  814. begin
  815. if report_errors then
  816. CGMessagePos(hp.fileinfo,errmsg);
  817. exit;
  818. end;
  819. while assigned(hp) do
  820. begin
  821. { property allowed? calln has a property check itself }
  822. if (nf_isproperty in hp.flags) then
  823. begin
  824. if (hp.nodetype=calln) then
  825. begin
  826. { check return type }
  827. case hp.resulttype.def.deftype of
  828. pointerdef :
  829. gotpointer:=true;
  830. objectdef :
  831. gotclass:=is_class_or_interface(hp.resulttype.def);
  832. recorddef :
  833. gotrecord:=true;
  834. classrefdef :
  835. gotclass:=true;
  836. stringdef :
  837. gotstring:=true;
  838. end;
  839. if (valid_property in opts) then
  840. begin
  841. { don't allow writing to calls that will create
  842. temps like calls that return a structure and we
  843. are assigning to a member }
  844. if (valid_const in opts) or
  845. not(
  846. (gotsubscript and gotrecord) or
  847. (gotstring and gotvec)
  848. ) then
  849. result:=true
  850. else
  851. if report_errors then
  852. CGMessagePos(hp.fileinfo,errmsg);
  853. end
  854. else
  855. begin
  856. { 1. if it returns a pointer and we've found a deref,
  857. 2. if it returns a class or record and a subscription or with is found
  858. 3. if the address is needed of a field (subscriptn) }
  859. if (gotpointer and gotderef) or
  860. (gotstring and gotvec) or
  861. (
  862. (gotclass or gotrecord) and
  863. (gotsubscript or gotwith)
  864. ) or
  865. (
  866. (gotvec and gotdynarray)
  867. ) or
  868. (
  869. (Valid_Addr in opts) and
  870. (hp.nodetype=subscriptn)
  871. ) then
  872. result:=true
  873. else
  874. if report_errors then
  875. CGMessagePos(hp.fileinfo,errmsg);
  876. end;
  877. end
  878. else
  879. result:=true;
  880. exit;
  881. end;
  882. if (Valid_Const in opts) and is_constnode(hp) then
  883. begin
  884. result:=true;
  885. exit;
  886. end;
  887. case hp.nodetype of
  888. temprefn :
  889. begin
  890. valid_for_assign := true;
  891. exit;
  892. end;
  893. derefn :
  894. begin
  895. gotderef:=true;
  896. hp:=tderefnode(hp).left;
  897. end;
  898. typeconvn :
  899. begin
  900. { typecast sizes must match, exceptions:
  901. - implicit typecast made by absolute
  902. - from formaldef
  903. - from void
  904. - from/to open array
  905. - typecast from pointer to array }
  906. fromdef:=ttypeconvnode(hp).left.resulttype.def;
  907. todef:=hp.resulttype.def;
  908. if not((nf_absolute in ttypeconvnode(hp).flags) or
  909. (fromdef.deftype=formaldef) or
  910. is_void(fromdef) or
  911. is_open_array(fromdef) or
  912. is_open_array(todef) or
  913. ((fromdef.deftype=pointerdef) and (todef.deftype=arraydef)) or
  914. ((fromdef.deftype = objectdef) and (todef.deftype = objectdef) and
  915. (tobjectdef(fromdef).is_related(tobjectdef(todef))))) and
  916. (fromdef.size<>todef.size) then
  917. begin
  918. { in TP it is allowed to typecast to smaller types. But the variable can't
  919. be in a register }
  920. if (m_tp7 in aktmodeswitches) or
  921. (todef.size<fromdef.size) then
  922. make_not_regable(hp)
  923. else
  924. if report_errors then
  925. CGMessagePos2(hp.fileinfo,type_e_typecast_wrong_size_for_assignment,tostr(fromdef.size),tostr(todef.size));
  926. end;
  927. { don't allow assignments to typeconvs that need special code }
  928. if not(gotsubscript or gotvec or gotderef) and
  929. not(ttypeconvnode(hp).assign_allowed) then
  930. begin
  931. if report_errors then
  932. CGMessagePos(hp.fileinfo,errmsg);
  933. exit;
  934. end;
  935. case hp.resulttype.def.deftype of
  936. pointerdef :
  937. gotpointer:=true;
  938. objectdef :
  939. gotclass:=is_class_or_interface(hp.resulttype.def);
  940. classrefdef :
  941. gotclass:=true;
  942. arraydef :
  943. begin
  944. { pointer -> array conversion is done then we need to see it
  945. as a deref, because a ^ is then not required anymore }
  946. if (ttypeconvnode(hp).left.resulttype.def.deftype=pointerdef) then
  947. gotderef:=true;
  948. end;
  949. end;
  950. hp:=ttypeconvnode(hp).left;
  951. end;
  952. vecn :
  953. begin
  954. gotvec:=true;
  955. { accesses to dyn. arrays override read only access in delphi }
  956. if (m_delphi in aktmodeswitches) and is_dynamic_array(tunarynode(hp).left.resulttype.def) then
  957. gotdynarray:=true;
  958. hp:=tunarynode(hp).left;
  959. end;
  960. asn :
  961. begin
  962. { asn can't be assigned directly, it returns the value in a register instead
  963. of reference. }
  964. if not(gotsubscript or gotderef or gotvec) then
  965. begin
  966. if report_errors then
  967. CGMessagePos(hp.fileinfo,errmsg);
  968. exit;
  969. end;
  970. hp:=tunarynode(hp).left;
  971. end;
  972. subscriptn :
  973. begin
  974. gotsubscript:=true;
  975. { loop counter? }
  976. if not(Valid_Const in opts) and
  977. (vo_is_loop_counter in tsubscriptnode(hp).vs.varoptions) then
  978. begin
  979. if report_errors then
  980. CGMessage1(parser_e_illegal_assignment_to_count_var,tsubscriptnode(hp).vs.realname)
  981. else
  982. exit;
  983. end;
  984. { a class/interface access is an implicit }
  985. { dereferencing }
  986. hp:=tsubscriptnode(hp).left;
  987. if is_class_or_interface(hp.resulttype.def) then
  988. gotderef:=true;
  989. end;
  990. muln,
  991. divn,
  992. andn,
  993. xorn,
  994. orn,
  995. notn,
  996. subn,
  997. addn :
  998. begin
  999. { Allow operators on a pointer, or an integer
  1000. and a pointer typecast and deref has been found }
  1001. if ((hp.resulttype.def.deftype=pointerdef) or
  1002. (is_integer(hp.resulttype.def) and gotpointer)) and
  1003. gotderef then
  1004. result:=true
  1005. else
  1006. { Temp strings are stored in memory, for compatibility with
  1007. delphi only }
  1008. if (m_delphi in aktmodeswitches) and
  1009. ((valid_addr in opts) or
  1010. (valid_const in opts)) and
  1011. (hp.resulttype.def.deftype=stringdef) then
  1012. result:=true
  1013. else
  1014. if report_errors then
  1015. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  1016. exit;
  1017. end;
  1018. niln,
  1019. pointerconstn :
  1020. begin
  1021. { to support e.g. @tmypointer(0)^.data; see tests/tbs/tb0481 }
  1022. if gotderef then
  1023. result:=true
  1024. else
  1025. if report_errors then
  1026. CGMessagePos(hp.fileinfo,type_e_no_assign_to_addr);
  1027. exit;
  1028. end;
  1029. addrn :
  1030. begin
  1031. if gotderef then
  1032. result:=true
  1033. else
  1034. if report_errors then
  1035. CGMessagePos(hp.fileinfo,type_e_no_assign_to_addr);
  1036. exit;
  1037. end;
  1038. calln :
  1039. begin
  1040. { check return type }
  1041. case hp.resulttype.def.deftype of
  1042. arraydef :
  1043. begin
  1044. { dynamic arrays are allowed when there is also a
  1045. vec node }
  1046. if is_dynamic_array(hp.resulttype.def) and
  1047. gotvec then
  1048. begin
  1049. gotderef:=true;
  1050. gotpointer:=true;
  1051. end;
  1052. end;
  1053. pointerdef :
  1054. gotpointer:=true;
  1055. objectdef :
  1056. gotclass:=is_class_or_interface(hp.resulttype.def);
  1057. recorddef, { handle record like class it needs a subscription }
  1058. classrefdef :
  1059. gotclass:=true;
  1060. stringdef :
  1061. gotstring:=true;
  1062. end;
  1063. { 1. if it returns a pointer and we've found a deref,
  1064. 2. if it returns a class or record and a subscription or with is found
  1065. 3. string is returned }
  1066. if (gotstring and gotvec) or
  1067. (gotpointer and gotderef) or
  1068. (gotclass and (gotsubscript or gotwith)) then
  1069. result:=true
  1070. else
  1071. { Temp strings are stored in memory, for compatibility with
  1072. delphi only }
  1073. if (m_delphi in aktmodeswitches) and
  1074. (valid_addr in opts) and
  1075. (hp.resulttype.def.deftype=stringdef) then
  1076. result:=true
  1077. else
  1078. if ([valid_const,valid_addr] * opts = [valid_const]) then
  1079. result:=true
  1080. else
  1081. if report_errors then
  1082. CGMessagePos(hp.fileinfo,errmsg);
  1083. exit;
  1084. end;
  1085. inlinen :
  1086. begin
  1087. if (valid_const in opts) and
  1088. (tinlinenode(hp).inlinenumber in [in_typeof_x]) then
  1089. result:=true
  1090. else
  1091. if report_errors then
  1092. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  1093. exit;
  1094. end;
  1095. loadn :
  1096. begin
  1097. case tloadnode(hp).symtableentry.typ of
  1098. absolutevarsym,
  1099. globalvarsym,
  1100. localvarsym,
  1101. paravarsym :
  1102. begin
  1103. { loop counter? }
  1104. if not(Valid_Const in opts) and
  1105. not gotderef and
  1106. (vo_is_loop_counter in tabstractvarsym(tloadnode(hp).symtableentry).varoptions) then
  1107. if report_errors then
  1108. CGMessage1(parser_e_illegal_assignment_to_count_var,tloadnode(hp).symtableentry.realname)
  1109. else
  1110. exit;
  1111. { derefed pointer }
  1112. if (tabstractvarsym(tloadnode(hp).symtableentry).varspez=vs_const) then
  1113. begin
  1114. { allow p^:= constructions with p is const parameter }
  1115. if gotderef or gotdynarray or (Valid_Const in opts) then
  1116. result:=true
  1117. else
  1118. if report_errors then
  1119. CGMessagePos(tloadnode(hp).fileinfo,type_e_no_assign_to_const);
  1120. exit;
  1121. end;
  1122. { Are we at a with symtable, then we need to process the
  1123. withrefnode also to check for maybe a const load }
  1124. if (tloadnode(hp).symtable.symtabletype=withsymtable) then
  1125. begin
  1126. { continue with processing the withref node }
  1127. hp:=tnode(twithsymtable(tloadnode(hp).symtable).withrefnode);
  1128. gotwith:=true;
  1129. end
  1130. else
  1131. begin
  1132. result:=true;
  1133. exit;
  1134. end;
  1135. end;
  1136. typedconstsym :
  1137. begin
  1138. if ttypedconstsym(tloadnode(hp).symtableentry).is_writable or
  1139. (valid_addr in opts) or
  1140. (valid_const in opts) then
  1141. result:=true
  1142. else
  1143. if report_errors then
  1144. CGMessagePos(hp.fileinfo,type_e_no_assign_to_const);
  1145. exit;
  1146. end;
  1147. procsym :
  1148. begin
  1149. if (Valid_Const in opts) then
  1150. result:=true
  1151. else
  1152. if report_errors then
  1153. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  1154. exit;
  1155. end;
  1156. labelsym :
  1157. begin
  1158. if (Valid_Addr in opts) then
  1159. result:=true
  1160. else
  1161. if report_errors then
  1162. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  1163. exit;
  1164. end;
  1165. constsym:
  1166. begin
  1167. if (tconstsym(tloadnode(hp).symtableentry).consttyp=constresourcestring) and
  1168. (valid_addr in opts) then
  1169. result:=true
  1170. else
  1171. if report_errors then
  1172. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  1173. exit;
  1174. end;
  1175. else
  1176. begin
  1177. if report_errors then
  1178. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  1179. exit;
  1180. end;
  1181. end;
  1182. end;
  1183. else
  1184. begin
  1185. if report_errors then
  1186. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  1187. exit;
  1188. end;
  1189. end;
  1190. end;
  1191. end;
  1192. function valid_for_var(p:tnode; report_errors: boolean):boolean;
  1193. begin
  1194. valid_for_var:=valid_for_assign(p,[],report_errors);
  1195. end;
  1196. function valid_for_formal_var(p : tnode; report_errors: boolean) : boolean;
  1197. begin
  1198. valid_for_formal_var:=valid_for_assign(p,[valid_void],report_errors);
  1199. end;
  1200. function valid_for_formal_const(p : tnode; report_errors: boolean) : boolean;
  1201. begin
  1202. valid_for_formal_const:=(p.resulttype.def.deftype=formaldef) or
  1203. valid_for_assign(p,[valid_void,valid_const],report_errors);
  1204. end;
  1205. function valid_for_assignment(p:tnode; report_errors: boolean):boolean;
  1206. begin
  1207. valid_for_assignment:=valid_for_assign(p,[valid_property],report_errors);
  1208. end;
  1209. function valid_for_addr(p : tnode; report_errors: boolean) : boolean;
  1210. begin
  1211. result:=valid_for_assign(p,[valid_const,valid_addr,valid_void],report_errors);
  1212. end;
  1213. procedure var_para_allowed(var eq:tequaltype;def_from,def_to:Tdef);
  1214. begin
  1215. { Note: eq must be already valid, it will only be updated! }
  1216. case def_to.deftype of
  1217. formaldef :
  1218. begin
  1219. { all types can be passed to a formaldef }
  1220. eq:=te_equal;
  1221. end;
  1222. orddef :
  1223. begin
  1224. { allows conversion from word to integer and
  1225. byte to shortint, but only for TP7 compatibility }
  1226. if (m_tp7 in aktmodeswitches) and
  1227. (def_from.deftype=orddef) and
  1228. (def_from.size=def_to.size) then
  1229. eq:=te_convert_l1;
  1230. end;
  1231. arraydef :
  1232. begin
  1233. if is_open_array(def_to) and
  1234. is_dynamic_array(def_from) and
  1235. equal_defs(tarraydef(def_from).elementtype.def,tarraydef(def_to).elementtype.def) then
  1236. eq:=te_convert_l2;
  1237. end;
  1238. pointerdef :
  1239. begin
  1240. { an implicit pointer conversion is allowed }
  1241. if (def_from.deftype=pointerdef) then
  1242. eq:=te_convert_l1;
  1243. end;
  1244. stringdef :
  1245. begin
  1246. { all shortstrings are allowed, size is not important }
  1247. if is_shortstring(def_from) and
  1248. is_shortstring(def_to) then
  1249. eq:=te_equal;
  1250. end;
  1251. objectdef :
  1252. begin
  1253. { child objects can be also passed }
  1254. { in non-delphi mode, otherwise }
  1255. { they must match exactly, except }
  1256. { if they are objects }
  1257. if (def_from.deftype=objectdef) and
  1258. (
  1259. not(m_delphi in aktmodeswitches) or
  1260. (
  1261. (tobjectdef(def_from).objecttype=odt_object) and
  1262. (tobjectdef(def_to).objecttype=odt_object)
  1263. )
  1264. ) and
  1265. (tobjectdef(def_from).is_related(tobjectdef(def_to))) then
  1266. eq:=te_convert_l1;
  1267. end;
  1268. filedef :
  1269. begin
  1270. { an implicit file conversion is also allowed }
  1271. { from a typed file to an untyped one }
  1272. if (def_from.deftype=filedef) and
  1273. (tfiledef(def_from).filetyp = ft_typed) and
  1274. (tfiledef(def_to).filetyp = ft_untyped) then
  1275. eq:=te_convert_l1;
  1276. end;
  1277. end;
  1278. end;
  1279. procedure para_allowed(var eq:tequaltype;p:tcallparanode;def_to:tdef);
  1280. begin
  1281. { Note: eq must be already valid, it will only be updated! }
  1282. case def_to.deftype of
  1283. formaldef :
  1284. begin
  1285. { all types can be passed to a formaldef }
  1286. eq:=te_equal;
  1287. end;
  1288. stringdef :
  1289. begin
  1290. { to support ansi/long/wide strings in a proper way }
  1291. { string and string[10] are assumed as equal }
  1292. { when searching the correct overloaded procedure }
  1293. if (p.resulttype.def.deftype=stringdef) and
  1294. (tstringdef(def_to).string_typ=tstringdef(p.resulttype.def).string_typ) then
  1295. eq:=te_equal
  1296. else
  1297. { Passing a constant char to ansistring or shortstring or
  1298. a widechar to widestring then handle it as equal. }
  1299. if (p.left.nodetype=ordconstn) and
  1300. (
  1301. is_char(p.resulttype.def) and
  1302. (is_shortstring(def_to) or is_ansistring(def_to))
  1303. ) or
  1304. (
  1305. is_widechar(p.resulttype.def) and
  1306. is_widestring(def_to)
  1307. ) then
  1308. eq:=te_equal
  1309. end;
  1310. setdef :
  1311. begin
  1312. { set can also be a not yet converted array constructor }
  1313. if (p.resulttype.def.deftype=arraydef) and
  1314. (tarraydef(p.resulttype.def).IsConstructor) and
  1315. not(tarraydef(p.resulttype.def).IsVariant) then
  1316. eq:=te_equal;
  1317. end;
  1318. procvardef :
  1319. begin
  1320. { in tp7 mode proc -> procvar is allowed }
  1321. if ((m_tp_procvar in aktmodeswitches) or
  1322. (m_mac_procvar in aktmodeswitches)) and
  1323. (p.left.nodetype=calln) and
  1324. (proc_to_procvar_equal(tprocdef(tcallnode(p.left).procdefinition),tprocvardef(def_to))>=te_equal) then
  1325. eq:=te_equal
  1326. else
  1327. if (m_mac_procvar in aktmodeswitches) and
  1328. is_procvar_load(p.left) then
  1329. eq:=te_convert_l2;
  1330. end;
  1331. end;
  1332. end;
  1333. function allowenumop(nt:tnodetype):boolean;
  1334. begin
  1335. result:=(nt in [equaln,unequaln,ltn,lten,gtn,gten]) or
  1336. ((cs_allow_enum_calc in aktlocalswitches) and
  1337. (nt in [addn,subn]));
  1338. end;
  1339. {****************************************************************************
  1340. TCallCandidates
  1341. ****************************************************************************}
  1342. constructor tcallcandidates.create(sym:tprocsym;st:tsymtable;ppn:tnode;isprop,ignorevis : boolean);
  1343. var
  1344. j : integer;
  1345. pd : tprocdef;
  1346. hp : pcandidate;
  1347. found,
  1348. has_overload_directive : boolean;
  1349. topclassh : tobjectdef;
  1350. srsymtable : tsymtable;
  1351. srprocsym : tprocsym;
  1352. pt : tcallparanode;
  1353. begin
  1354. if not assigned(sym) then
  1355. internalerror(200411015);
  1356. FProcSym:=sym;
  1357. FProcs:=nil;
  1358. FProccnt:=0;
  1359. FProcvisiblecnt:=0;
  1360. FParanode:=ppn;
  1361. FAllowVariant:=true;
  1362. { determine length of parameter list }
  1363. pt:=tcallparanode(ppn);
  1364. FParalength:=0;
  1365. while assigned(pt) do
  1366. begin
  1367. inc(FParalength);
  1368. pt:=tcallparanode(pt.right);
  1369. end;
  1370. { when the definition has overload directive set, we search for
  1371. overloaded definitions in the class, this only needs to be done once
  1372. for class entries as the tree keeps always the same }
  1373. if (not sym.overloadchecked) and
  1374. (sym.owner.symtabletype=objectsymtable) and
  1375. (po_overload in sym.first_procdef.procoptions) then
  1376. search_class_overloads(sym);
  1377. { when the class passed is defined in this unit we
  1378. need to use the scope of that class. This is a trick
  1379. that can be used to access protected members in other
  1380. units. At least kylix supports it this way (PFV) }
  1381. if assigned(st) and
  1382. (
  1383. (st.symtabletype=objectsymtable) or
  1384. ((st.symtabletype=withsymtable) and
  1385. (st.defowner.deftype=objectdef))
  1386. ) and
  1387. (st.defowner.owner.symtabletype in [globalsymtable,staticsymtable]) and
  1388. st.defowner.owner.iscurrentunit then
  1389. topclassh:=tobjectdef(st.defowner)
  1390. else
  1391. begin
  1392. if assigned(current_procinfo) then
  1393. topclassh:=current_procinfo.procdef._class
  1394. else
  1395. topclassh:=nil;
  1396. end;
  1397. { link all procedures which have the same # of parameters }
  1398. for j:=1 to sym.procdef_count do
  1399. begin
  1400. pd:=sym.procdef[j];
  1401. { Is the procdef visible? This needs to be checked on
  1402. procdef level since a symbol can contain both private and
  1403. public declarations. But the check should not be done
  1404. when the callnode is generated by a property
  1405. inherited overrides invisible anonymous inherited (FK) }
  1406. if isprop or ignorevis or
  1407. (pd.owner.symtabletype<>objectsymtable) or
  1408. pd.is_visible_for_object(topclassh) then
  1409. begin
  1410. { we have at least one procedure that is visible }
  1411. inc(FProcvisiblecnt);
  1412. { only when the # of parameter are supported by the
  1413. procedure }
  1414. if (FParalength>=pd.minparacount) and
  1415. ((po_varargs in pd.procoptions) or { varargs }
  1416. (FParalength<=pd.maxparacount)) then
  1417. proc_add(pd);
  1418. end;
  1419. end;
  1420. { remember if the procedure is declared with the overload directive,
  1421. it's information is still needed also after all procs are removed }
  1422. has_overload_directive:=(po_overload in sym.first_procdef.procoptions);
  1423. { when the definition has overload directive set, we search for
  1424. overloaded definitions in the symtablestack. The found
  1425. entries are only added to the procs list and not the procsym, because
  1426. the list can change in every situation }
  1427. if has_overload_directive and
  1428. (sym.owner.symtabletype<>objectsymtable) then
  1429. begin
  1430. srsymtable:=sym.owner.next;
  1431. while assigned(srsymtable) do
  1432. begin
  1433. if srsymtable.symtabletype in [localsymtable,staticsymtable,globalsymtable] then
  1434. begin
  1435. srprocsym:=tprocsym(srsymtable.speedsearch(sym.name,sym.speedvalue));
  1436. if assigned(srprocsym) and
  1437. (srprocsym.typ=procsym) then
  1438. begin
  1439. { if this visible procedure doesn't have overload we can stop
  1440. searching }
  1441. if not(po_overload in srprocsym.first_procdef.procoptions) and
  1442. srprocsym.first_procdef.is_visible_for_object(topclassh) then
  1443. break;
  1444. { process all overloaded definitions }
  1445. for j:=1 to srprocsym.procdef_count do
  1446. begin
  1447. pd:=srprocsym.procdef[j];
  1448. { only visible procedures need to be added }
  1449. if pd.is_visible_for_object(topclassh) then
  1450. begin
  1451. { only when the # of parameter are supported by the
  1452. procedure }
  1453. if (FParalength>=pd.minparacount) and
  1454. ((po_varargs in pd.procoptions) or { varargs }
  1455. (FParalength<=pd.maxparacount)) then
  1456. begin
  1457. found:=false;
  1458. hp:=FProcs;
  1459. while assigned(hp) do
  1460. begin
  1461. { Only compare visible parameters for the user }
  1462. if compare_paras(hp^.data.paras,pd.paras,cp_value_equal_const,[cpo_ignorehidden])>=te_equal then
  1463. begin
  1464. found:=true;
  1465. break;
  1466. end;
  1467. hp:=hp^.next;
  1468. end;
  1469. if not found then
  1470. proc_add(pd);
  1471. end;
  1472. end;
  1473. end;
  1474. end;
  1475. end;
  1476. srsymtable:=srsymtable.next;
  1477. end;
  1478. end;
  1479. end;
  1480. constructor tcallcandidates.create_operator(op:ttoken;ppn:tnode);
  1481. var
  1482. j : integer;
  1483. pd : tprocdef;
  1484. hp : pcandidate;
  1485. found : boolean;
  1486. srsymtable : tsymtable;
  1487. srprocsym : tprocsym;
  1488. pt : tcallparanode;
  1489. sv : cardinal;
  1490. begin
  1491. FProcSym:=nil;
  1492. FProcs:=nil;
  1493. FProccnt:=0;
  1494. FProcvisiblecnt:=0;
  1495. FParanode:=ppn;
  1496. FAllowVariant:=false;
  1497. { determine length of parameter list }
  1498. pt:=tcallparanode(ppn);
  1499. FParalength:=0;
  1500. while assigned(pt) do
  1501. begin
  1502. if pt.resulttype.def.deftype=variantdef then
  1503. FAllowVariant:=true;
  1504. inc(FParalength);
  1505. pt:=tcallparanode(pt.right);
  1506. end;
  1507. { we search all overloaded operator definitions in the symtablestack. The found
  1508. entries are only added to the procs list and not the procsym, because
  1509. the list can change in every situation }
  1510. sv:=getspeedvalue(overloaded_names[op]);
  1511. srsymtable:=symtablestack;
  1512. while assigned(srsymtable) do
  1513. begin
  1514. if srsymtable.symtabletype in [localsymtable,staticsymtable,globalsymtable] then
  1515. begin
  1516. srprocsym:=tprocsym(srsymtable.speedsearch(overloaded_names[op],sv));
  1517. if assigned(srprocsym) and
  1518. (srprocsym.typ=procsym) then
  1519. begin
  1520. { Store first procsym found }
  1521. if not assigned(FProcsym) then
  1522. FProcsym:=srprocsym;
  1523. { process all overloaded definitions }
  1524. for j:=1 to srprocsym.procdef_count do
  1525. begin
  1526. pd:=srprocsym.procdef[j];
  1527. { only when the # of parameter are supported by the
  1528. procedure }
  1529. if (FParalength>=pd.minparacount) and
  1530. (FParalength<=pd.maxparacount) then
  1531. begin
  1532. found:=false;
  1533. hp:=FProcs;
  1534. while assigned(hp) do
  1535. begin
  1536. { Only compare visible parameters for the user }
  1537. if compare_paras(hp^.data.paras,pd.paras,cp_value_equal_const,[cpo_ignorehidden])>=te_equal then
  1538. begin
  1539. found:=true;
  1540. break;
  1541. end;
  1542. hp:=hp^.next;
  1543. end;
  1544. if not found then
  1545. proc_add(pd);
  1546. end;
  1547. end;
  1548. end;
  1549. end;
  1550. srsymtable:=srsymtable.next;
  1551. end;
  1552. end;
  1553. destructor tcallcandidates.destroy;
  1554. var
  1555. hpnext,
  1556. hp : pcandidate;
  1557. begin
  1558. hp:=FProcs;
  1559. while assigned(hp) do
  1560. begin
  1561. hpnext:=hp^.next;
  1562. dispose(hp);
  1563. hp:=hpnext;
  1564. end;
  1565. end;
  1566. function tcallcandidates.proc_add(pd:tprocdef):pcandidate;
  1567. var
  1568. defaultparacnt : integer;
  1569. begin
  1570. { generate new candidate entry }
  1571. new(result);
  1572. fillchar(result^,sizeof(tcandidate),0);
  1573. result^.data:=pd;
  1574. result^.next:=FProcs;
  1575. FProcs:=result;
  1576. inc(FProccnt);
  1577. { Find last parameter, skip all default parameters
  1578. that are not passed. Ignore this skipping for varargs }
  1579. result^.firstparaidx:=pd.paras.count-1;
  1580. if not(po_varargs in pd.procoptions) then
  1581. begin
  1582. { ignore hidden parameters }
  1583. while (result^.firstparaidx>=0) and (vo_is_hidden_para in tparavarsym(pd.paras[result^.firstparaidx]).varoptions) do
  1584. dec(result^.firstparaidx);
  1585. defaultparacnt:=pd.maxparacount-FParalength;
  1586. if defaultparacnt>0 then
  1587. begin
  1588. if defaultparacnt>result^.firstparaidx+1 then
  1589. internalerror(200401141);
  1590. dec(result^.firstparaidx,defaultparacnt);
  1591. end;
  1592. end;
  1593. end;
  1594. procedure tcallcandidates.list(all:boolean);
  1595. var
  1596. hp : pcandidate;
  1597. begin
  1598. hp:=FProcs;
  1599. while assigned(hp) do
  1600. begin
  1601. if all or
  1602. (not hp^.invalid) then
  1603. MessagePos1(hp^.data.fileinfo,sym_h_param_list,hp^.data.fullprocname(false));
  1604. hp:=hp^.next;
  1605. end;
  1606. end;
  1607. {$ifdef EXTDEBUG}
  1608. procedure tcallcandidates.dump_info(lvl:longint);
  1609. function ParaTreeStr(p:tcallparanode):string;
  1610. begin
  1611. result:='';
  1612. while assigned(p) do
  1613. begin
  1614. if result<>'' then
  1615. result:=','+result;
  1616. result:=p.resulttype.def.typename+result;
  1617. p:=tcallparanode(p.right);
  1618. end;
  1619. end;
  1620. var
  1621. hp : pcandidate;
  1622. i : integer;
  1623. currpara : tparavarsym;
  1624. begin
  1625. if not CheckVerbosity(lvl) then
  1626. exit;
  1627. Comment(lvl+V_LineInfo,'Overloaded callnode: '+FProcSym.name+'('+ParaTreeStr(tcallparanode(FParaNode))+')');
  1628. hp:=FProcs;
  1629. while assigned(hp) do
  1630. begin
  1631. Comment(lvl,' '+hp^.data.fullprocname(false));
  1632. if (hp^.invalid) then
  1633. Comment(lvl,' invalid')
  1634. else
  1635. begin
  1636. Comment(lvl,' ex: '+tostr(hp^.exact_count)+
  1637. ' eq: '+tostr(hp^.equal_count)+
  1638. ' l1: '+tostr(hp^.cl1_count)+
  1639. ' l2: '+tostr(hp^.cl2_count)+
  1640. ' l3: '+tostr(hp^.cl3_count)+
  1641. ' oper: '+tostr(hp^.coper_count)+
  1642. ' ord: '+realtostr(hp^.ordinal_distance));
  1643. { Print parameters in left-right order }
  1644. for i:=0 to hp^.data.paras.count-1 do
  1645. begin
  1646. currpara:=tparavarsym(hp^.data.paras[i]);
  1647. if (vo_is_hidden_para in currpara.varoptions) then
  1648. Comment(lvl,' - '+currpara.vartype.def.typename+' : '+EqualTypeName[currpara.eqval]);
  1649. end;
  1650. end;
  1651. hp:=hp^.next;
  1652. end;
  1653. end;
  1654. {$endif EXTDEBUG}
  1655. procedure tcallcandidates.get_information;
  1656. var
  1657. hp : pcandidate;
  1658. currpara : tparavarsym;
  1659. paraidx : integer;
  1660. currparanr : byte;
  1661. rfh,rth : bestreal;
  1662. objdef : tobjectdef;
  1663. def_from,
  1664. def_to : tdef;
  1665. currpt,
  1666. pt : tcallparanode;
  1667. eq : tequaltype;
  1668. convtype : tconverttype;
  1669. pdoper : tprocdef;
  1670. releasecurrpt : boolean;
  1671. cdoptions : tcompare_defs_options;
  1672. begin
  1673. cdoptions:=[cdo_check_operator];
  1674. if FAllowVariant then
  1675. include(cdoptions,cdo_allow_variant);
  1676. { process all procs }
  1677. hp:=FProcs;
  1678. while assigned(hp) do
  1679. begin
  1680. { We compare parameters in reverse order (right to left),
  1681. the firstpara is already pointing to the last parameter
  1682. were we need to start comparing }
  1683. currparanr:=FParalength;
  1684. paraidx:=hp^.firstparaidx;
  1685. while (paraidx>=0) and (vo_is_hidden_para in tparavarsym(hp^.data.paras[paraidx]).varoptions) do
  1686. dec(paraidx);
  1687. pt:=tcallparanode(FParaNode);
  1688. while assigned(pt) and (paraidx>=0) do
  1689. begin
  1690. currpara:=tparavarsym(hp^.data.paras[paraidx]);
  1691. { currpt can be changed from loadn to calln when a procvar
  1692. is passed. This is to prevent that the change is permanent }
  1693. currpt:=pt;
  1694. releasecurrpt:=false;
  1695. { retrieve current parameter definitions to compares }
  1696. eq:=te_incompatible;
  1697. def_from:=currpt.resulttype.def;
  1698. def_to:=currpara.vartype.def;
  1699. if not(assigned(def_from)) then
  1700. internalerror(200212091);
  1701. if not(
  1702. assigned(def_to) or
  1703. ((po_varargs in hp^.data.procoptions) and
  1704. (currparanr>hp^.data.minparacount))
  1705. ) then
  1706. internalerror(200212092);
  1707. { Convert tp procvars when not expecting a procvar }
  1708. if (def_to.deftype<>procvardef) and
  1709. (currpt.left.resulttype.def.deftype=procvardef) then
  1710. begin
  1711. releasecurrpt:=true;
  1712. currpt:=tcallparanode(pt.getcopy);
  1713. if maybe_call_procvar(currpt.left,true) then
  1714. begin
  1715. currpt.resulttype:=currpt.left.resulttype;
  1716. def_from:=currpt.left.resulttype.def;
  1717. end;
  1718. end;
  1719. { varargs are always equal, but not exact }
  1720. if (po_varargs in hp^.data.procoptions) and
  1721. (currparanr>hp^.data.minparacount) then
  1722. begin
  1723. eq:=te_equal;
  1724. end
  1725. else
  1726. { same definition -> exact }
  1727. if (def_from=def_to) then
  1728. begin
  1729. eq:=te_exact;
  1730. end
  1731. else
  1732. { for value and const parameters check if a integer is constant or
  1733. included in other integer -> equal and calc ordinal_distance }
  1734. if not(currpara.varspez in [vs_var,vs_out]) and
  1735. is_integer(def_from) and
  1736. is_integer(def_to) and
  1737. is_in_limit(def_from,def_to) then
  1738. begin
  1739. eq:=te_equal;
  1740. hp^.ordinal_distance:=hp^.ordinal_distance+
  1741. abs(bestreal(torddef(def_from).low)-bestreal(torddef(def_to).low));
  1742. if (torddef(def_to).typ=u64bit) then
  1743. rth:=bestreal(qword(torddef(def_to).high))
  1744. else
  1745. rth:=bestreal(torddef(def_to).high);
  1746. if (torddef(def_from).typ=u64bit) then
  1747. rfh:=bestreal(qword(torddef(def_from).high))
  1748. else
  1749. rfh:=bestreal(torddef(def_from).high);
  1750. hp^.ordinal_distance:=hp^.ordinal_distance+abs(rth-rfh);
  1751. { Give wrong sign a small penalty, this is need to get a diffrence
  1752. from word->[longword,longint] }
  1753. if is_signed(def_from)<>is_signed(def_to) then
  1754. hp^.ordinal_distance:=hp^.ordinal_distance+1.0;
  1755. end
  1756. else
  1757. { for value and const parameters check precision of real, give
  1758. penalty for loosing of precision. var and out parameters must match exactly }
  1759. if not(currpara.varspez in [vs_var,vs_out]) and
  1760. is_real(def_from) and
  1761. is_real(def_to) then
  1762. begin
  1763. eq:=te_equal;
  1764. if is_extended(def_to) then
  1765. rth:=bestreal(4)
  1766. else
  1767. if is_double (def_to) then
  1768. rth:=bestreal(2)
  1769. else
  1770. rth:=bestreal(1);
  1771. if is_extended(def_from) then
  1772. rfh:=bestreal(4)
  1773. else
  1774. if is_double (def_from) then
  1775. rfh:=bestreal(2)
  1776. else
  1777. rfh:=bestreal(1);
  1778. { penalty for shrinking of precision }
  1779. if rth<rfh then
  1780. rfh:=(rfh-rth)*16
  1781. else
  1782. rfh:=rth-rfh;
  1783. hp^.ordinal_distance:=hp^.ordinal_distance+rfh;
  1784. end
  1785. else
  1786. { related object parameters also need to determine the distance between the current
  1787. object and the object we are comparing with. var and out parameters must match exactly }
  1788. if not(currpara.varspez in [vs_var,vs_out]) and
  1789. (def_from.deftype=objectdef) and
  1790. (def_to.deftype=objectdef) and
  1791. (tobjectdef(def_from).objecttype=tobjectdef(def_to).objecttype) and
  1792. tobjectdef(def_from).is_related(tobjectdef(def_to)) then
  1793. begin
  1794. eq:=te_convert_l1;
  1795. objdef:=tobjectdef(def_from);
  1796. while assigned(objdef) do
  1797. begin
  1798. if objdef=def_to then
  1799. break;
  1800. hp^.ordinal_distance:=hp^.ordinal_distance+1;
  1801. objdef:=objdef.childof;
  1802. end;
  1803. end
  1804. else
  1805. { generic type comparision }
  1806. begin
  1807. eq:=compare_defs_ext(def_from,def_to,currpt.left.nodetype,convtype,pdoper,cdoptions);
  1808. { when the types are not equal we need to check
  1809. some special case for parameter passing }
  1810. if (eq<te_equal) then
  1811. begin
  1812. if currpara.varspez in [vs_var,vs_out] then
  1813. begin
  1814. { para requires an equal type so the previous found
  1815. match was not good enough, reset to incompatible }
  1816. eq:=te_incompatible;
  1817. { var_para_allowed will return te_equal and te_convert_l1 to
  1818. make a difference for best matching }
  1819. var_para_allowed(eq,currpt.resulttype.def,currpara.vartype.def)
  1820. end
  1821. else
  1822. para_allowed(eq,currpt,def_to);
  1823. end;
  1824. end;
  1825. { when a procvar was changed to a call an exact much is
  1826. downgraded to equal. This way an overload call with the
  1827. procvar is choosen. See tb0471 (PFV) }
  1828. if (pt<>currpt) and (eq=te_exact) then
  1829. eq:=te_equal;
  1830. { increase correct counter }
  1831. case eq of
  1832. te_exact :
  1833. inc(hp^.exact_count);
  1834. te_equal :
  1835. inc(hp^.equal_count);
  1836. te_convert_l1 :
  1837. inc(hp^.cl1_count);
  1838. te_convert_l2 :
  1839. inc(hp^.cl2_count);
  1840. te_convert_l3 :
  1841. inc(hp^.cl3_count);
  1842. te_convert_operator :
  1843. inc(hp^.coper_count);
  1844. te_incompatible :
  1845. hp^.invalid:=true;
  1846. else
  1847. internalerror(200212072);
  1848. end;
  1849. { stop checking when an incompatible parameter is found }
  1850. if hp^.invalid then
  1851. begin
  1852. { store the current parameter info for
  1853. a nice error message when no procedure is found }
  1854. hp^.wrongparaidx:=paraidx;
  1855. hp^.wrongparanr:=currparanr;
  1856. break;
  1857. end;
  1858. {$ifdef EXTDEBUG}
  1859. { store equal in node tree for dump }
  1860. currpara.eqval:=eq;
  1861. {$endif EXTDEBUG}
  1862. { maybe release temp currpt }
  1863. if releasecurrpt then
  1864. currpt.free;
  1865. { next parameter in the call tree }
  1866. pt:=tcallparanode(pt.right);
  1867. { next parameter for definition, only goto next para
  1868. if we're out of the varargs }
  1869. if not(po_varargs in hp^.data.procoptions) or
  1870. (currparanr<=hp^.data.maxparacount) then
  1871. begin
  1872. { Ignore vs_hidden parameters }
  1873. repeat
  1874. dec(paraidx);
  1875. until (paraidx<0) or not(vo_is_hidden_para in tparavarsym(hp^.data.paras[paraidx]).varoptions);
  1876. end;
  1877. dec(currparanr);
  1878. end;
  1879. if not(hp^.invalid) and
  1880. (assigned(pt) or (paraidx>=0) or (currparanr<>0)) then
  1881. internalerror(200212141);
  1882. { next candidate }
  1883. hp:=hp^.next;
  1884. end;
  1885. end;
  1886. function is_better_candidate(currpd,bestpd:pcandidate):integer;
  1887. var
  1888. res : integer;
  1889. begin
  1890. {
  1891. Return values:
  1892. > 0 when currpd is better than bestpd
  1893. < 0 when bestpd is better than currpd
  1894. = 0 when both are equal
  1895. To choose the best candidate we use the following order:
  1896. - Incompatible flag
  1897. - (Smaller) Number of convert operator parameters.
  1898. - (Smaller) Number of convertlevel 2 parameters.
  1899. - (Smaller) Number of convertlevel 1 parameters.
  1900. - (Bigger) Number of exact parameters.
  1901. - (Smaller) Number of equal parameters.
  1902. - (Smaller) Total of ordinal distance. For example, the distance of a word
  1903. to a byte is 65535-255=65280.
  1904. }
  1905. if bestpd^.invalid then
  1906. begin
  1907. if currpd^.invalid then
  1908. res:=0
  1909. else
  1910. res:=1;
  1911. end
  1912. else
  1913. if currpd^.invalid then
  1914. res:=-1
  1915. else
  1916. begin
  1917. { less operator parameters? }
  1918. res:=(bestpd^.coper_count-currpd^.coper_count);
  1919. if (res=0) then
  1920. begin
  1921. { less cl3 parameters? }
  1922. res:=(bestpd^.cl3_count-currpd^.cl3_count);
  1923. if (res=0) then
  1924. begin
  1925. { less cl2 parameters? }
  1926. res:=(bestpd^.cl2_count-currpd^.cl2_count);
  1927. if (res=0) then
  1928. begin
  1929. { less cl1 parameters? }
  1930. res:=(bestpd^.cl1_count-currpd^.cl1_count);
  1931. if (res=0) then
  1932. begin
  1933. { more exact parameters? }
  1934. res:=(currpd^.exact_count-bestpd^.exact_count);
  1935. if (res=0) then
  1936. begin
  1937. { less equal parameters? }
  1938. res:=(bestpd^.equal_count-currpd^.equal_count);
  1939. if (res=0) then
  1940. begin
  1941. { smaller ordinal distance? }
  1942. if (currpd^.ordinal_distance<bestpd^.ordinal_distance) then
  1943. res:=1
  1944. else
  1945. if (currpd^.ordinal_distance>bestpd^.ordinal_distance) then
  1946. res:=-1
  1947. else
  1948. res:=0;
  1949. end;
  1950. end;
  1951. end;
  1952. end;
  1953. end;
  1954. end;
  1955. end;
  1956. is_better_candidate:=res;
  1957. end;
  1958. function tcallcandidates.choose_best(var bestpd:tabstractprocdef):integer;
  1959. var
  1960. besthpstart,
  1961. hp : pcandidate;
  1962. cntpd,
  1963. res : integer;
  1964. begin
  1965. {
  1966. Returns the number of candidates left and the
  1967. first candidate is returned in pdbest
  1968. }
  1969. { Setup the first procdef as best, only count it as a result
  1970. when it is valid }
  1971. bestpd:=FProcs^.data;
  1972. if FProcs^.invalid then
  1973. cntpd:=0
  1974. else
  1975. cntpd:=1;
  1976. if assigned(FProcs^.next) then
  1977. begin
  1978. besthpstart:=FProcs;
  1979. hp:=FProcs^.next;
  1980. while assigned(hp) do
  1981. begin
  1982. res:=is_better_candidate(hp,besthpstart);
  1983. if (res>0) then
  1984. begin
  1985. { hp is better, flag all procs to be incompatible }
  1986. while (besthpstart<>hp) do
  1987. begin
  1988. besthpstart^.invalid:=true;
  1989. besthpstart:=besthpstart^.next;
  1990. end;
  1991. { besthpstart is already set to hp }
  1992. bestpd:=besthpstart^.data;
  1993. cntpd:=1;
  1994. end
  1995. else
  1996. if (res<0) then
  1997. begin
  1998. { besthpstart is better, flag current hp to be incompatible }
  1999. hp^.invalid:=true;
  2000. end
  2001. else
  2002. begin
  2003. { res=0, both are valid }
  2004. if not hp^.invalid then
  2005. inc(cntpd);
  2006. end;
  2007. hp:=hp^.next;
  2008. end;
  2009. end;
  2010. result:=cntpd;
  2011. end;
  2012. procedure tcallcandidates.find_wrong_para;
  2013. var
  2014. currparanr : smallint;
  2015. hp : pcandidate;
  2016. pt : tcallparanode;
  2017. wrongpara : tparavarsym;
  2018. begin
  2019. { Only process the first overloaded procdef }
  2020. hp:=FProcs;
  2021. { Find callparanode corresponding to the argument }
  2022. pt:=tcallparanode(FParanode);
  2023. currparanr:=FParalength;
  2024. while assigned(pt) and
  2025. (currparanr>hp^.wrongparanr) do
  2026. begin
  2027. pt:=tcallparanode(pt.right);
  2028. dec(currparanr);
  2029. end;
  2030. if (currparanr<>hp^.wrongparanr) or
  2031. not assigned(pt) then
  2032. internalerror(200212094);
  2033. { Show error message, when it was a var or out parameter
  2034. guess that it is a missing typeconv }
  2035. wrongpara:=tparavarsym(hp^.data.paras[hp^.wrongparaidx]);
  2036. if wrongpara.varspez in [vs_var,vs_out] then
  2037. begin
  2038. { Maybe passing the correct type but passing a const to var parameter }
  2039. if (compare_defs(pt.resulttype.def,wrongpara.vartype.def,pt.nodetype)<>te_incompatible) and
  2040. not valid_for_var(pt.left,true) then
  2041. CGMessagePos(pt.left.fileinfo,type_e_variable_id_expected)
  2042. else
  2043. CGMessagePos2(pt.left.fileinfo,parser_e_call_by_ref_without_typeconv,
  2044. FullTypeName(pt.left.resulttype.def,wrongpara.vartype.def),
  2045. FullTypeName(wrongpara.vartype.def,pt.left.resulttype.def))
  2046. end
  2047. else
  2048. CGMessagePos3(pt.left.fileinfo,type_e_wrong_parameter_type,tostr(hp^.wrongparanr),
  2049. FullTypeName(pt.left.resulttype.def,wrongpara.vartype.def),
  2050. FullTypeName(wrongpara.vartype.def,pt.left.resulttype.def));
  2051. end;
  2052. procedure check_hints(const srsym: tsym; const symoptions: tsymoptions);
  2053. begin
  2054. if not assigned(srsym) then
  2055. exit;
  2056. if sp_hint_deprecated in symoptions then
  2057. Message1(sym_w_deprecated_symbol,srsym.realname);
  2058. if sp_hint_platform in symoptions then
  2059. Message1(sym_w_non_portable_symbol,srsym.realname);
  2060. if sp_hint_unimplemented in symoptions then
  2061. Message1(sym_w_non_implemented_symbol,srsym.realname);
  2062. end;
  2063. end.