htypechk.pas 118 KB

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