htypechk.pas 103 KB

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