htypechk.pas 107 KB

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