htypechk.pas 115 KB

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