htypechk.pas 108 KB

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