htypechk.pas 111 KB

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