htypechk.pas 111 KB

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