htypechk.pas 118 KB

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