htypechk.pas 85 KB

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