htypechk.pas 101 KB

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