htypechk.pas 117 KB

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