htypechk.pas 119 KB

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