htypechk.pas 101 KB

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