htypechk.pas 74 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064
  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,
  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. coper_count : integer; { should be signed }
  42. ordinal_distance : bestreal;
  43. invalid : boolean;
  44. wrongparanr : byte;
  45. end;
  46. tcallcandidates = class
  47. private
  48. FProcSym : tprocsym;
  49. FProcs : pcandidate;
  50. FProcVisibleCnt,
  51. FProcCnt : integer;
  52. FParaNode : tnode;
  53. FParaLength : smallint;
  54. FAllowVariant : boolean;
  55. function proc_add(pd:tprocdef):pcandidate;
  56. public
  57. constructor create(sym:tprocsym;st:tsymtable;ppn:tnode;isprop,ignorevis : boolean);
  58. constructor create_operator(op:ttoken;ppn:tnode);
  59. destructor destroy;override;
  60. procedure list(all:boolean);
  61. {$ifdef EXTDEBUG}
  62. procedure dump_info(lvl:longint);
  63. {$endif EXTDEBUG}
  64. procedure get_information;
  65. function choose_best(var bestpd:tabstractprocdef):integer;
  66. procedure find_wrong_para;
  67. property Count:integer read FProcCnt;
  68. property VisibleCount:integer read FProcVisibleCnt;
  69. end;
  70. const
  71. tok2nodes=25;
  72. tok2node:array[1..tok2nodes] of ttok2noderec=(
  73. (tok:_PLUS ;nod:addn;op_overloading_supported:true), { binary overloading supported }
  74. (tok:_MINUS ;nod:subn;op_overloading_supported:true), { binary and unary overloading supported }
  75. (tok:_STAR ;nod:muln;op_overloading_supported:true), { binary overloading supported }
  76. (tok:_SLASH ;nod:slashn;op_overloading_supported:true), { binary overloading supported }
  77. (tok:_EQUAL ;nod:equaln;op_overloading_supported:true), { binary overloading supported }
  78. (tok:_GT ;nod:gtn;op_overloading_supported:true), { binary overloading supported }
  79. (tok:_LT ;nod:ltn;op_overloading_supported:true), { binary overloading supported }
  80. (tok:_GTE ;nod:gten;op_overloading_supported:true), { binary overloading supported }
  81. (tok:_LTE ;nod:lten;op_overloading_supported:true), { binary overloading supported }
  82. (tok:_SYMDIF ;nod:symdifn;op_overloading_supported:true), { binary overloading supported }
  83. (tok:_STARSTAR;nod:starstarn;op_overloading_supported:true), { binary overloading supported }
  84. (tok:_OP_AS ;nod:asn;op_overloading_supported:false), { binary overloading NOT supported }
  85. (tok:_OP_IN ;nod:inn;op_overloading_supported:false), { binary overloading NOT supported }
  86. (tok:_OP_IS ;nod:isn;op_overloading_supported:false), { binary overloading NOT supported }
  87. (tok:_OP_OR ;nod:orn;op_overloading_supported:true), { binary overloading supported }
  88. (tok:_OP_AND ;nod:andn;op_overloading_supported:true), { binary overloading supported }
  89. (tok:_OP_DIV ;nod:divn;op_overloading_supported:true), { binary overloading supported }
  90. (tok:_OP_NOT ;nod:notn;op_overloading_supported:true), { unary overloading supported }
  91. (tok:_OP_MOD ;nod:modn;op_overloading_supported:true), { binary overloading supported }
  92. (tok:_OP_SHL ;nod:shln;op_overloading_supported:true), { binary overloading supported }
  93. (tok:_OP_SHR ;nod:shrn;op_overloading_supported:true), { binary overloading supported }
  94. (tok:_OP_XOR ;nod:xorn;op_overloading_supported:true), { binary overloading supported }
  95. (tok:_ASSIGNMENT;nod:assignn;op_overloading_supported:true), { unary overloading supported }
  96. (tok:_CARET ;nod:caretn;op_overloading_supported:false), { binary overloading NOT supported }
  97. (tok:_UNEQUAL ;nod:unequaln;op_overloading_supported:false) { binary overloading NOT supported overload = instead }
  98. );
  99. const
  100. { firstcallparan without varspez we don't count the ref }
  101. {$ifdef extdebug}
  102. count_ref : boolean = true;
  103. {$endif def extdebug}
  104. allow_array_constructor : boolean = false;
  105. function node2opstr(nt:tnodetype):string;
  106. { check operator args and result type }
  107. function isbinaryoperatoroverloadable(treetyp:tnodetype;ld:tdef;lt:tnodetype;rd:tdef;rt:tnodetype) : boolean;
  108. function isoperatoracceptable(pf : tprocdef; optoken : ttoken) : boolean;
  109. function isunaryoverloaded(var t : tnode) : boolean;
  110. function isbinaryoverloaded(var t : tnode) : boolean;
  111. { Register Allocation }
  112. procedure make_not_regable(p : tnode);
  113. procedure calcregisters(p : tbinarynode;r32,fpu,mmx : word);
  114. { procvar handling }
  115. function is_procvar_load(p:tnode):boolean;
  116. procedure test_local_to_procvar(from_def:tprocvardef;to_def:tdef);
  117. { sets varsym varstate field correctly }
  118. type
  119. tvarstateflag = (vsf_must_be_valid,vsf_use_hints);
  120. tvarstateflags = set of tvarstateflag;
  121. procedure set_varstate(p:tnode;newstate:tvarstate;varstateflags:tvarstateflags);
  122. { sets the callunique flag, if the node is a vecn, }
  123. { takes care of type casts etc. }
  124. procedure set_unique(p : tnode);
  125. function valid_for_formal_var(p : tnode) : boolean;
  126. function valid_for_formal_const(p : tnode) : boolean;
  127. function valid_for_var(p:tnode):boolean;
  128. function valid_for_assignment(p:tnode):boolean;
  129. function valid_for_addr(p : tnode) : boolean;
  130. implementation
  131. uses
  132. globtype,systems,
  133. cutils,verbose,globals,
  134. symtable,
  135. defutil,defcmp,
  136. nbas,ncnv,nld,nmem,ncal,nmat,ninl,nutils,
  137. cgbase,procinfo
  138. ;
  139. type
  140. TValidAssign=(Valid_Property,Valid_Void,Valid_Const,Valid_Addr);
  141. TValidAssigns=set of TValidAssign;
  142. function node2opstr(nt:tnodetype):string;
  143. var
  144. i : integer;
  145. begin
  146. result:='<unknown>';
  147. for i:=1 to tok2nodes do
  148. if tok2node[i].nod=nt then
  149. begin
  150. result:=tokeninfo^[tok2node[i].tok].str;
  151. break;
  152. end;
  153. end;
  154. function isbinaryoperatoroverloadable(treetyp:tnodetype;ld:tdef;lt:tnodetype;rd:tdef;rt:tnodetype) : boolean;
  155. function internal_check(treetyp:tnodetype;ld:tdef;lt:tnodetype;rd:tdef;rt:tnodetype;var allowed:boolean):boolean;
  156. begin
  157. internal_check:=true;
  158. case ld.deftype of
  159. formaldef,
  160. recorddef,
  161. variantdef :
  162. begin
  163. allowed:=true;
  164. end;
  165. procvardef :
  166. begin
  167. if (rd.deftype in [pointerdef,procdef,procvardef]) then
  168. begin
  169. allowed:=false;
  170. exit;
  171. end;
  172. allowed:=true;
  173. end;
  174. pointerdef :
  175. begin
  176. if ((rd.deftype in [orddef,enumdef,pointerdef,classrefdef,procvardef]) or
  177. is_class_or_interface(rd)) then
  178. begin
  179. allowed:=false;
  180. exit;
  181. end;
  182. { don't allow pchar+string }
  183. if (is_pchar(ld) or is_pwidechar(ld)) and
  184. ((rd.deftype=stringdef) or
  185. is_pchar(rd) or
  186. is_pwidechar(rd) or
  187. is_chararray(rd) or
  188. is_widechararray(rd)) then
  189. begin
  190. allowed:=false;
  191. exit;
  192. end;
  193. allowed:=true;
  194. end;
  195. arraydef :
  196. begin
  197. { not mmx }
  198. if (cs_mmx in aktlocalswitches) and
  199. is_mmx_able_array(ld) then
  200. begin
  201. allowed:=false;
  202. exit;
  203. end;
  204. { not chararray+[(wide)char,(wide)string,(wide)chararray] }
  205. if (is_chararray(ld) or is_widechararray(ld) or
  206. is_open_chararray(ld) or is_open_widechararray(ld))
  207. and
  208. ((rd.deftype in [stringdef,orddef,enumdef]) or
  209. is_pchar(rd) or
  210. is_pwidechar(rd) or
  211. is_chararray(rd) or
  212. is_widechararray(rd) or
  213. is_open_chararray(rd) or
  214. is_open_widechararray(rd) or
  215. (rt=niln)) then
  216. begin
  217. allowed:=false;
  218. exit;
  219. end;
  220. { dynamic array compare with niln }
  221. if ((is_dynamic_array(ld) and
  222. (rt=niln)) or
  223. (is_dynamic_array(ld) and is_dynamic_array(rd)))
  224. and
  225. (treetyp in [equaln,unequaln]) then
  226. begin
  227. allowed:=false;
  228. exit;
  229. end;
  230. allowed:=true;
  231. end;
  232. objectdef :
  233. begin
  234. { <> and = are defined for classes }
  235. if (treetyp in [equaln,unequaln]) and
  236. is_class_or_interface(ld) then
  237. begin
  238. allowed:=false;
  239. exit;
  240. end;
  241. allowed:=true;
  242. end;
  243. stringdef :
  244. begin
  245. if (rd.deftype in [orddef,enumdef,stringdef]) or
  246. is_pchar(rd) or
  247. is_pwidechar(rd) or
  248. is_chararray(rd) or
  249. is_widechararray(rd) or
  250. is_open_chararray(rd) or
  251. is_open_widechararray(rd) then
  252. begin
  253. allowed:=false;
  254. exit;
  255. end;
  256. allowed:=true;
  257. end;
  258. else
  259. internal_check:=false;
  260. end;
  261. end;
  262. var
  263. allowed : boolean;
  264. begin
  265. { power ** is always possible }
  266. if (treetyp=starstarn) then
  267. begin
  268. isbinaryoperatoroverloadable:=true;
  269. exit;
  270. end;
  271. { order of arguments does not matter so we have to check also
  272. the reversed order }
  273. allowed:=false;
  274. if not internal_check(treetyp,ld,lt,rd,rt,allowed) then
  275. internal_check(treetyp,rd,rt,ld,lt,allowed);
  276. isbinaryoperatoroverloadable:=allowed;
  277. end;
  278. function isunaryoperatoroverloadable(treetyp : tnodetype;ld : tdef) : boolean;
  279. begin
  280. result:=false;
  281. case treetyp of
  282. subn,
  283. unaryminusn :
  284. begin
  285. if (ld.deftype in [orddef,enumdef,floatdef]) then
  286. exit;
  287. {$ifdef SUPPORT_MMX}
  288. if (cs_mmx in aktlocalswitches) and
  289. is_mmx_able_array(ld) then
  290. exit;
  291. {$endif SUPPORT_MMX}
  292. result:=true;
  293. end;
  294. notn :
  295. begin
  296. if (ld.deftype in [orddef,enumdef,floatdef]) then
  297. exit;
  298. {$ifdef SUPPORT_MMX}
  299. if (cs_mmx in aktlocalswitches) and
  300. is_mmx_able_array(ld) then
  301. exit;
  302. {$endif SUPPORT_MMX}
  303. result:=true;
  304. end;
  305. end;
  306. end;
  307. function isoperatoracceptable(pf : tprocdef; optoken : ttoken) : boolean;
  308. var
  309. ld,rd : tdef;
  310. i : longint;
  311. eq : tequaltype;
  312. conv : tconverttype;
  313. pd : tprocdef;
  314. begin
  315. result:=false;
  316. case pf.parast.symindex.count of
  317. 1 : begin
  318. ld:=tparavarsym(pf.parast.symindex.first).vartype.def;
  319. { assignment is a special case }
  320. if optoken=_ASSIGNMENT then
  321. begin
  322. eq:=compare_defs_ext(ld,pf.rettype.def,nothingn,conv,pd,[cdo_explicit]);
  323. result:=(eq=te_incompatible);
  324. end
  325. else
  326. begin
  327. for i:=1 to tok2nodes do
  328. if tok2node[i].tok=optoken then
  329. begin
  330. result:=
  331. tok2node[i].op_overloading_supported and
  332. isunaryoperatoroverloadable(tok2node[i].nod,ld);
  333. break;
  334. end;
  335. end;
  336. end;
  337. 2 : begin
  338. for i:=1 to tok2nodes do
  339. if tok2node[i].tok=optoken then
  340. begin
  341. ld:=tparavarsym(pf.parast.symindex.first).vartype.def;
  342. rd:=tparavarsym(pf.parast.symindex.first.indexnext).vartype.def;
  343. result:=
  344. tok2node[i].op_overloading_supported and
  345. isbinaryoperatoroverloadable(tok2node[i].nod,ld,nothingn,rd,nothingn);
  346. break;
  347. end;
  348. end;
  349. end;
  350. end;
  351. function isunaryoverloaded(var t : tnode) : boolean;
  352. var
  353. ld : tdef;
  354. optoken : ttoken;
  355. operpd : tprocdef;
  356. ppn : tcallparanode;
  357. candidates : tcallcandidates;
  358. cand_cnt : integer;
  359. begin
  360. result:=false;
  361. operpd:=nil;
  362. { load easier access variables }
  363. ld:=tunarynode(t).left.resulttype.def;
  364. if not isunaryoperatoroverloadable(t.nodetype,ld) then
  365. exit;
  366. { operator overload is possible }
  367. result:=true;
  368. case t.nodetype of
  369. notn:
  370. optoken:=_OP_NOT;
  371. unaryminusn:
  372. optoken:=_MINUS;
  373. else
  374. begin
  375. CGMessage(parser_e_operator_not_overloaded);
  376. t:=cnothingnode.create;
  377. exit;
  378. end;
  379. end;
  380. { generate parameter nodes }
  381. ppn:=ccallparanode.create(tunarynode(t).left.getcopy,nil);
  382. ppn.get_paratype;
  383. candidates:=tcallcandidates.create_operator(optoken,ppn);
  384. { stop when there are no operators found }
  385. if candidates.count=0 then
  386. begin
  387. CGMessage(parser_e_operator_not_overloaded);
  388. candidates.free;
  389. ppn.free;
  390. t:=cnothingnode.create;
  391. exit;
  392. end;
  393. { Retrieve information about the candidates }
  394. candidates.get_information;
  395. {$ifdef EXTDEBUG}
  396. { Display info when multiple candidates are found }
  397. candidates.dump_info(V_Debug);
  398. {$endif EXTDEBUG}
  399. cand_cnt:=candidates.choose_best(operpd);
  400. { exit when no overloads are found }
  401. if cand_cnt=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. { Multiple candidates left? }
  410. if cand_cnt>1 then
  411. begin
  412. CGMessage(type_e_cant_choose_overload_function);
  413. {$ifdef EXTDEBUG}
  414. candidates.dump_info(V_Hint);
  415. {$else EXTDEBUG}
  416. candidates.list(false);
  417. {$endif EXTDEBUG}
  418. { we'll just use the first candidate to make the
  419. call }
  420. end;
  421. candidates.free;
  422. inc(operpd.procsym.refs);
  423. { the nil as symtable signs firstcalln that this is
  424. an overloaded operator }
  425. t:=ccallnode.create(ppn,Tprocsym(operpd.procsym),nil,nil,[]);
  426. { we already know the procdef to use, so it can
  427. skip the overload choosing in callnode.det_resulttype }
  428. tcallnode(t).procdefinition:=operpd;
  429. end;
  430. function isbinaryoverloaded(var t : tnode) : boolean;
  431. var
  432. rd,ld : tdef;
  433. optoken : ttoken;
  434. operpd : tprocdef;
  435. ht : tnode;
  436. ppn : tcallparanode;
  437. candidates : tcallcandidates;
  438. cand_cnt : integer;
  439. begin
  440. isbinaryoverloaded:=false;
  441. operpd:=nil;
  442. { load easier access variables }
  443. ld:=tbinarynode(t).left.resulttype.def;
  444. rd:=tbinarynode(t).right.resulttype.def;
  445. if not isbinaryoperatoroverloadable(t.nodetype,ld,tbinarynode(t).left.nodetype,rd,tbinarynode(t).right.nodetype) then
  446. exit;
  447. { operator overload is possible }
  448. result:=true;
  449. case t.nodetype of
  450. equaln,
  451. unequaln :
  452. optoken:=_EQUAL;
  453. addn:
  454. optoken:=_PLUS;
  455. subn:
  456. optoken:=_MINUS;
  457. muln:
  458. optoken:=_STAR;
  459. starstarn:
  460. optoken:=_STARSTAR;
  461. slashn:
  462. optoken:=_SLASH;
  463. ltn:
  464. optoken:=_LT;
  465. gtn:
  466. optoken:=_GT;
  467. lten:
  468. optoken:=_LTE;
  469. gten:
  470. optoken:=_GTE;
  471. symdifn :
  472. optoken:=_SYMDIF;
  473. modn :
  474. optoken:=_OP_MOD;
  475. orn :
  476. optoken:=_OP_OR;
  477. xorn :
  478. optoken:=_OP_XOR;
  479. andn :
  480. optoken:=_OP_AND;
  481. divn :
  482. optoken:=_OP_DIV;
  483. shln :
  484. optoken:=_OP_SHL;
  485. shrn :
  486. optoken:=_OP_SHR;
  487. else
  488. begin
  489. CGMessage(parser_e_operator_not_overloaded);
  490. t:=cnothingnode.create;
  491. exit;
  492. end;
  493. end;
  494. { generate parameter nodes }
  495. ppn:=ccallparanode.create(tbinarynode(t).right.getcopy,ccallparanode.create(tbinarynode(t).left.getcopy,nil));
  496. ppn.get_paratype;
  497. candidates:=tcallcandidates.create_operator(optoken,ppn);
  498. { for commutative operators we can swap arguments and try again }
  499. if (candidates.count=0) and
  500. not(optoken in [_OP_SHL,_OP_SHR,_OP_DIV,_OP_MOD,_STARSTAR,_SLASH,_MINUS]) then
  501. begin
  502. candidates.free;
  503. reverseparameters(ppn);
  504. { reverse compare operators }
  505. case optoken of
  506. _LT:
  507. optoken:=_GTE;
  508. _GT:
  509. optoken:=_LTE;
  510. _LTE:
  511. optoken:=_GT;
  512. _GTE:
  513. optoken:=_LT;
  514. end;
  515. candidates:=tcallcandidates.create_operator(optoken,ppn);
  516. end;
  517. { stop when there are no operators found }
  518. if candidates.count=0 then
  519. begin
  520. CGMessage(parser_e_operator_not_overloaded);
  521. candidates.free;
  522. ppn.free;
  523. t:=cnothingnode.create;
  524. exit;
  525. end;
  526. { Retrieve information about the candidates }
  527. candidates.get_information;
  528. {$ifdef EXTDEBUG}
  529. { Display info when multiple candidates are found }
  530. candidates.dump_info(V_Debug);
  531. {$endif EXTDEBUG}
  532. cand_cnt:=candidates.choose_best(operpd);
  533. { exit when no overloads are found }
  534. if cand_cnt=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. { Multiple candidates left? }
  543. if cand_cnt>1 then
  544. begin
  545. CGMessage(type_e_cant_choose_overload_function);
  546. {$ifdef EXTDEBUG}
  547. candidates.dump_info(V_Hint);
  548. {$else EXTDEBUG}
  549. candidates.list(false);
  550. {$endif EXTDEBUG}
  551. { we'll just use the first candidate to make the
  552. call }
  553. end;
  554. candidates.free;
  555. inc(operpd.procsym.refs);
  556. { the nil as symtable signs firstcalln that this is
  557. an overloaded operator }
  558. ht:=ccallnode.create(ppn,Tprocsym(operpd.procsym),nil,nil,[]);
  559. { we already know the procdef to use, so it can
  560. skip the overload choosing in callnode.det_resulttype }
  561. tcallnode(ht).procdefinition:=operpd;
  562. if t.nodetype=unequaln then
  563. ht:=cnotnode.create(ht);
  564. t:=ht;
  565. end;
  566. {****************************************************************************
  567. Register Calculation
  568. ****************************************************************************}
  569. { marks an lvalue as "unregable" }
  570. procedure make_not_regable(p : tnode);
  571. begin
  572. case p.nodetype of
  573. typeconvn :
  574. make_not_regable(ttypeconvnode(p).left);
  575. loadn :
  576. if tloadnode(p).symtableentry.typ in [globalvarsym,localvarsym,paravarsym] then
  577. tabstractvarsym(tloadnode(p).symtableentry).varregable:=vr_none;
  578. end;
  579. end;
  580. { calculates the needed registers for a binary operator }
  581. procedure calcregisters(p : tbinarynode;r32,fpu,mmx : word);
  582. begin
  583. p.left_right_max;
  584. { Only when the difference between the left and right registers < the
  585. wanted registers allocate the amount of registers }
  586. if assigned(p.left) then
  587. begin
  588. if assigned(p.right) then
  589. begin
  590. { the location must be already filled in because we need it to }
  591. { calculate the necessary number of registers (JM) }
  592. if p.expectloc = LOC_INVALID then
  593. internalerror(200110101);
  594. if (abs(p.left.registersint-p.right.registersint)<r32) or
  595. ((p.expectloc = LOC_FPUREGISTER) and
  596. (p.right.registersfpu <= p.left.registersfpu) and
  597. ((p.right.registersfpu <> 0) or (p.left.registersfpu <> 0)) and
  598. (p.left.registersint < p.right.registersint)) then
  599. inc(p.registersint,r32);
  600. if (abs(p.left.registersfpu-p.right.registersfpu)<fpu) then
  601. inc(p.registersfpu,fpu);
  602. {$ifdef SUPPORT_MMX}
  603. if (abs(p.left.registersmmx-p.right.registersmmx)<mmx) then
  604. inc(p.registersmmx,mmx);
  605. {$endif SUPPORT_MMX}
  606. { the following is a little bit guessing but I think }
  607. { it's the only way to solve same internalerrors: }
  608. { if the left and right node both uses registers }
  609. { and return a mem location, but the current node }
  610. { doesn't use an integer register we get probably }
  611. { trouble when restoring a node }
  612. if (p.left.registersint=p.right.registersint) and
  613. (p.registersint=p.left.registersint) and
  614. (p.registersint>0) and
  615. (p.left.expectloc in [LOC_REFERENCE,LOC_CREFERENCE]) and
  616. (p.right.expectloc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  617. inc(p.registersint);
  618. end
  619. else
  620. begin
  621. if (p.left.registersint<r32) then
  622. inc(p.registersint,r32);
  623. if (p.left.registersfpu<fpu) then
  624. inc(p.registersfpu,fpu);
  625. {$ifdef SUPPORT_MMX}
  626. if (p.left.registersmmx<mmx) then
  627. inc(p.registersmmx,mmx);
  628. {$endif SUPPORT_MMX}
  629. end;
  630. end;
  631. end;
  632. {****************************************************************************
  633. Subroutine Handling
  634. ****************************************************************************}
  635. function is_procvar_load(p:tnode):boolean;
  636. begin
  637. result:=false;
  638. { remove voidpointer typecast for tp procvars }
  639. if (m_tp_procvar in aktmodeswitches) and
  640. (p.nodetype=typeconvn) and
  641. is_voidpointer(p.resulttype.def) then
  642. p:=tunarynode(p).left;
  643. result:=(p.nodetype=typeconvn) and
  644. (ttypeconvnode(p).convtype=tc_proc_2_procvar);
  645. end;
  646. { local routines can't be assigned to procvars }
  647. procedure test_local_to_procvar(from_def:tprocvardef;to_def:tdef);
  648. begin
  649. if (from_def.parast.symtablelevel>normal_function_level) and
  650. (to_def.deftype=procvardef) then
  651. CGMessage(type_e_cannot_local_proc_to_procvar);
  652. end;
  653. procedure set_varstate(p:tnode;newstate:tvarstate;varstateflags:tvarstateflags);
  654. var
  655. hsym : tabstractvarsym;
  656. begin
  657. while assigned(p) do
  658. begin
  659. case p.nodetype of
  660. typeconvn :
  661. begin
  662. case ttypeconvnode(p).convtype of
  663. tc_cchar_2_pchar,
  664. tc_cstring_2_pchar,
  665. tc_array_2_pointer :
  666. exclude(varstateflags,vsf_must_be_valid);
  667. tc_pchar_2_string,
  668. tc_pointer_2_array :
  669. include(varstateflags,vsf_must_be_valid);
  670. end;
  671. p:=tunarynode(p).left;
  672. end;
  673. subscriptn :
  674. p:=tunarynode(p).left;
  675. vecn:
  676. begin
  677. set_varstate(tbinarynode(p).right,vs_used,[vsf_must_be_valid]);
  678. if not(tunarynode(p).left.resulttype.def.deftype in [stringdef,arraydef]) then
  679. include(varstateflags,vsf_must_be_valid);
  680. p:=tunarynode(p).left;
  681. end;
  682. { do not parse calln }
  683. calln :
  684. break;
  685. loadn :
  686. begin
  687. if (tloadnode(p).symtableentry.typ in [localvarsym,paravarsym,globalvarsym]) then
  688. begin
  689. hsym:=tabstractvarsym(tloadnode(p).symtableentry);
  690. if (vsf_must_be_valid in varstateflags) and (hsym.varstate=vs_declared) then
  691. begin
  692. { Give warning/note for uninitialized locals }
  693. if assigned(hsym.owner) and
  694. not(vo_is_external in hsym.varoptions) and
  695. (hsym.owner.symtabletype in [localsymtable,staticsymtable]) and
  696. (hsym.owner=current_procinfo.procdef.localst) then
  697. begin
  698. if (vo_is_funcret in hsym.varoptions) then
  699. CGMessage(sym_w_function_result_not_set)
  700. else
  701. begin
  702. if tloadnode(p).symtable.symtabletype=localsymtable then
  703. begin
  704. if (vsf_use_hints in varstateflags) then
  705. CGMessage1(sym_h_uninitialized_local_variable,hsym.realname)
  706. else
  707. CGMessage1(sym_w_uninitialized_local_variable,hsym.realname);
  708. end
  709. else
  710. begin
  711. if (vsf_use_hints in varstateflags) then
  712. CGMessage1(sym_h_uninitialized_variable,hsym.realname)
  713. else
  714. CGMessage1(sym_w_uninitialized_variable,hsym.realname);
  715. end;
  716. end;
  717. end;
  718. end;
  719. { don't override vs_used with vs_assigned }
  720. if hsym.varstate<>vs_used then
  721. hsym.varstate:=newstate;
  722. end;
  723. break;
  724. end;
  725. callparan :
  726. internalerror(200310081);
  727. else
  728. break;
  729. end;{case }
  730. end;
  731. end;
  732. procedure set_unique(p : tnode);
  733. begin
  734. while assigned(p) do
  735. begin
  736. case p.nodetype of
  737. vecn:
  738. begin
  739. include(p.flags,nf_callunique);
  740. break;
  741. end;
  742. typeconvn,
  743. subscriptn,
  744. derefn:
  745. p:=tunarynode(p).left;
  746. else
  747. break;
  748. end;
  749. end;
  750. end;
  751. function valid_for_assign(p:tnode;opts:TValidAssigns):boolean;
  752. var
  753. hp : tnode;
  754. gotstring,
  755. gotwith,
  756. gotsubscript,
  757. gotpointer,
  758. gotvec,
  759. gotclass,
  760. gotdynarray,
  761. gotderef : boolean;
  762. fromdef,
  763. todef : tdef;
  764. errmsg : longint;
  765. begin
  766. if valid_const in opts then
  767. errmsg:=type_e_variable_id_expected
  768. else
  769. errmsg:=type_e_argument_cant_be_assigned;
  770. result:=false;
  771. gotsubscript:=false;
  772. gotvec:=false;
  773. gotderef:=false;
  774. gotclass:=false;
  775. gotpointer:=false;
  776. gotwith:=false;
  777. gotdynarray:=false;
  778. gotstring:=false;
  779. hp:=p;
  780. if not(valid_void in opts) and
  781. is_void(hp.resulttype.def) then
  782. begin
  783. CGMessagePos(hp.fileinfo,errmsg);
  784. exit;
  785. end;
  786. while assigned(hp) do
  787. begin
  788. { property allowed? calln has a property check itself }
  789. if (nf_isproperty in hp.flags) then
  790. begin
  791. if (hp.nodetype<>calln) or
  792. (valid_property in opts) then
  793. result:=true
  794. else
  795. begin
  796. { check return type }
  797. case hp.resulttype.def.deftype of
  798. pointerdef :
  799. gotpointer:=true;
  800. objectdef :
  801. gotclass:=is_class_or_interface(hp.resulttype.def);
  802. recorddef, { handle record like class it needs a subscription }
  803. classrefdef :
  804. gotclass:=true;
  805. stringdef :
  806. gotstring:=true;
  807. end;
  808. { 1. if it returns a pointer and we've found a deref,
  809. 2. if it returns a class or record and a subscription or with is found
  810. 3. if the address is needed of a field (subscriptn) }
  811. if (gotpointer and gotderef) or
  812. (gotstring and gotvec) or
  813. (
  814. gotclass and
  815. (gotsubscript or gotwith)
  816. ) or
  817. (
  818. (gotvec and gotdynarray)
  819. ) or
  820. (
  821. (Valid_Addr in opts) and
  822. (hp.nodetype=subscriptn)
  823. ) then
  824. result:=true
  825. else
  826. CGMessagePos(hp.fileinfo,errmsg);
  827. end;
  828. exit;
  829. end;
  830. if (Valid_Const in opts) and is_constnode(hp) then
  831. begin
  832. result:=true;
  833. exit;
  834. end;
  835. case hp.nodetype of
  836. temprefn :
  837. begin
  838. valid_for_assign := true;
  839. exit;
  840. end;
  841. derefn :
  842. begin
  843. gotderef:=true;
  844. hp:=tderefnode(hp).left;
  845. end;
  846. typeconvn :
  847. begin
  848. { typecast sizes must match, exceptions:
  849. - implicit typecast made by absolute
  850. - from formaldef
  851. - from void
  852. - from/to open array
  853. - typecast from pointer to array }
  854. fromdef:=ttypeconvnode(hp).left.resulttype.def;
  855. todef:=hp.resulttype.def;
  856. if not((nf_absolute in ttypeconvnode(hp).flags) or
  857. (fromdef.deftype=formaldef) or
  858. is_void(fromdef) or
  859. is_open_array(fromdef) or
  860. is_open_array(todef) or
  861. ((fromdef.deftype=pointerdef) and (todef.deftype=arraydef)) or
  862. ((fromdef.deftype = objectdef) and (todef.deftype = objectdef) and
  863. (tobjectdef(fromdef).is_related(tobjectdef(todef))))) and
  864. (fromdef.size<>todef.size) then
  865. begin
  866. { in TP it is allowed to typecast to smaller types. But the variable can't
  867. be in a register }
  868. if (m_tp7 in aktmodeswitches) or
  869. (todef.size<fromdef.size) then
  870. make_not_regable(hp)
  871. else
  872. CGMessagePos2(hp.fileinfo,type_e_typecast_wrong_size_for_assignment,tostr(fromdef.size),tostr(todef.size));
  873. end;
  874. { don't allow assignments to typeconvs that need special code }
  875. if not(gotsubscript or gotvec or gotderef) and
  876. not(ttypeconvnode(hp).assign_allowed) then
  877. begin
  878. CGMessagePos(hp.fileinfo,errmsg);
  879. exit;
  880. end;
  881. case hp.resulttype.def.deftype of
  882. pointerdef :
  883. gotpointer:=true;
  884. objectdef :
  885. gotclass:=is_class_or_interface(hp.resulttype.def);
  886. classrefdef :
  887. gotclass:=true;
  888. arraydef :
  889. begin
  890. { pointer -> array conversion is done then we need to see it
  891. as a deref, because a ^ is then not required anymore }
  892. if (ttypeconvnode(hp).left.resulttype.def.deftype=pointerdef) then
  893. gotderef:=true;
  894. end;
  895. end;
  896. hp:=ttypeconvnode(hp).left;
  897. end;
  898. vecn :
  899. begin
  900. gotvec:=true;
  901. { accesses to dyn. arrays override read only access in delphi }
  902. if (m_delphi in aktmodeswitches) and is_dynamic_array(tunarynode(hp).left.resulttype.def) then
  903. gotdynarray:=true;
  904. hp:=tunarynode(hp).left;
  905. end;
  906. asn :
  907. begin
  908. { asn can't be assigned directly, it returns the value in a register instead
  909. of reference. }
  910. if not(gotsubscript or gotderef or gotvec) then
  911. begin
  912. CGMessagePos(hp.fileinfo,errmsg);
  913. exit;
  914. end;
  915. hp:=tunarynode(hp).left;
  916. end;
  917. subscriptn :
  918. begin
  919. gotsubscript:=true;
  920. { loop counter? }
  921. if not(Valid_Const in opts) and
  922. (vo_is_loop_counter in tsubscriptnode(hp).vs.varoptions) then
  923. CGMessage1(parser_e_illegal_assignment_to_count_var,tsubscriptnode(hp).vs.realname);
  924. { a class/interface access is an implicit }
  925. { dereferencing }
  926. hp:=tsubscriptnode(hp).left;
  927. if is_class_or_interface(hp.resulttype.def) then
  928. gotderef:=true;
  929. end;
  930. muln,
  931. divn,
  932. andn,
  933. xorn,
  934. orn,
  935. notn,
  936. subn,
  937. addn :
  938. begin
  939. { Allow operators on a pointer, or an integer
  940. and a pointer typecast and deref has been found }
  941. if ((hp.resulttype.def.deftype=pointerdef) or
  942. (is_integer(hp.resulttype.def) and gotpointer)) and
  943. gotderef then
  944. result:=true
  945. else
  946. { Temp strings are stored in memory, for compatibility with
  947. delphi only }
  948. if (m_delphi in aktmodeswitches) and
  949. ((valid_addr in opts) or
  950. (valid_const in opts)) and
  951. (hp.resulttype.def.deftype=stringdef) then
  952. result:=true
  953. else
  954. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  955. exit;
  956. end;
  957. niln,
  958. pointerconstn :
  959. begin
  960. { to support e.g. @tmypointer(0)^.data; see tests/tbs/tb0481 }
  961. if gotderef then
  962. result:=true
  963. else
  964. CGMessagePos(hp.fileinfo,type_e_no_assign_to_addr);
  965. exit;
  966. end;
  967. addrn :
  968. begin
  969. if gotderef then
  970. result:=true
  971. else
  972. CGMessagePos(hp.fileinfo,type_e_no_assign_to_addr);
  973. exit;
  974. end;
  975. calln :
  976. begin
  977. { check return type }
  978. case hp.resulttype.def.deftype of
  979. arraydef :
  980. begin
  981. { dynamic arrays are allowed when there is also a
  982. vec node }
  983. if is_dynamic_array(hp.resulttype.def) and
  984. gotvec then
  985. begin
  986. gotderef:=true;
  987. gotpointer:=true;
  988. end;
  989. end;
  990. pointerdef :
  991. gotpointer:=true;
  992. objectdef :
  993. gotclass:=is_class_or_interface(hp.resulttype.def);
  994. recorddef, { handle record like class it needs a subscription }
  995. classrefdef :
  996. gotclass:=true;
  997. stringdef :
  998. gotstring:=true;
  999. end;
  1000. { 1. if it returns a pointer and we've found a deref,
  1001. 2. if it returns a class or record and a subscription or with is found
  1002. 3. string is returned }
  1003. if (gotstring and gotvec) or
  1004. (gotpointer and gotderef) or
  1005. (gotclass and (gotsubscript or gotwith)) then
  1006. result:=true
  1007. else
  1008. { Temp strings are stored in memory, for compatibility with
  1009. delphi only }
  1010. if (m_delphi in aktmodeswitches) and
  1011. (valid_addr in opts) and
  1012. (hp.resulttype.def.deftype=stringdef) then
  1013. result:=true
  1014. else
  1015. CGMessagePos(hp.fileinfo,errmsg);
  1016. exit;
  1017. end;
  1018. inlinen :
  1019. begin
  1020. if (valid_const in opts) and
  1021. (tinlinenode(hp).inlinenumber in [in_typeof_x]) then
  1022. result:=true
  1023. else
  1024. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  1025. exit;
  1026. end;
  1027. loadn :
  1028. begin
  1029. case tloadnode(hp).symtableentry.typ of
  1030. absolutevarsym,
  1031. globalvarsym,
  1032. localvarsym,
  1033. paravarsym :
  1034. begin
  1035. { loop counter? }
  1036. if not(Valid_Const in opts) and
  1037. (vo_is_loop_counter in tabstractvarsym(tloadnode(hp).symtableentry).varoptions) then
  1038. CGMessage1(parser_e_illegal_assignment_to_count_var,tloadnode(hp).symtableentry.realname);
  1039. { derefed pointer }
  1040. if (tabstractvarsym(tloadnode(hp).symtableentry).varspez=vs_const) then
  1041. begin
  1042. { allow p^:= constructions with p is const parameter }
  1043. if gotderef or gotdynarray or (Valid_Const in opts) then
  1044. result:=true
  1045. else
  1046. CGMessagePos(tloadnode(hp).fileinfo,type_e_no_assign_to_const);
  1047. exit;
  1048. end;
  1049. { Are we at a with symtable, then we need to process the
  1050. withrefnode also to check for maybe a const load }
  1051. if (tloadnode(hp).symtable.symtabletype=withsymtable) then
  1052. begin
  1053. { continue with processing the withref node }
  1054. hp:=tnode(twithsymtable(tloadnode(hp).symtable).withrefnode);
  1055. gotwith:=true;
  1056. end
  1057. else
  1058. begin
  1059. result:=true;
  1060. exit;
  1061. end;
  1062. end;
  1063. typedconstsym :
  1064. begin
  1065. if ttypedconstsym(tloadnode(hp).symtableentry).is_writable or
  1066. (valid_addr in opts) or
  1067. (valid_const in opts) then
  1068. result:=true
  1069. else
  1070. CGMessagePos(hp.fileinfo,type_e_no_assign_to_const);
  1071. exit;
  1072. end;
  1073. procsym :
  1074. begin
  1075. if (Valid_Const in opts) then
  1076. result:=true
  1077. else
  1078. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  1079. exit;
  1080. end;
  1081. labelsym :
  1082. begin
  1083. if (Valid_Addr in opts) then
  1084. result:=true
  1085. else
  1086. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  1087. exit;
  1088. end;
  1089. constsym:
  1090. begin
  1091. if (tconstsym(tloadnode(hp).symtableentry).consttyp=constresourcestring) and
  1092. (valid_addr in opts) then
  1093. result:=true
  1094. else
  1095. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  1096. exit;
  1097. end;
  1098. else
  1099. begin
  1100. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  1101. exit;
  1102. end;
  1103. end;
  1104. end;
  1105. else
  1106. begin
  1107. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  1108. exit;
  1109. end;
  1110. end;
  1111. end;
  1112. end;
  1113. function valid_for_var(p:tnode):boolean;
  1114. begin
  1115. valid_for_var:=valid_for_assign(p,[]);
  1116. end;
  1117. function valid_for_formal_var(p : tnode) : boolean;
  1118. begin
  1119. valid_for_formal_var:=valid_for_assign(p,[valid_void]);
  1120. end;
  1121. function valid_for_formal_const(p : tnode) : boolean;
  1122. begin
  1123. valid_for_formal_const:=(p.resulttype.def.deftype=formaldef) or
  1124. valid_for_assign(p,[valid_void,valid_const]);
  1125. end;
  1126. function valid_for_assignment(p:tnode):boolean;
  1127. begin
  1128. valid_for_assignment:=valid_for_assign(p,[valid_property]);
  1129. end;
  1130. function valid_for_addr(p : tnode) : boolean;
  1131. begin
  1132. result:=valid_for_assign(p,[valid_const,valid_addr,valid_void]);
  1133. end;
  1134. procedure var_para_allowed(var eq:tequaltype;def_from,def_to:Tdef);
  1135. begin
  1136. { Note: eq must be already valid, it will only be updated! }
  1137. case def_to.deftype of
  1138. formaldef :
  1139. begin
  1140. { all types can be passed to a formaldef }
  1141. eq:=te_equal;
  1142. end;
  1143. orddef :
  1144. begin
  1145. { allows conversion from word to integer and
  1146. byte to shortint, but only for TP7 compatibility }
  1147. if (m_tp7 in aktmodeswitches) and
  1148. (def_from.deftype=orddef) and
  1149. (def_from.size=def_to.size) then
  1150. eq:=te_convert_l1;
  1151. end;
  1152. arraydef :
  1153. begin
  1154. if is_open_array(def_to) and
  1155. is_dynamic_array(def_from) and
  1156. equal_defs(tarraydef(def_from).elementtype.def,tarraydef(def_to).elementtype.def) then
  1157. eq:=te_convert_l2;
  1158. end;
  1159. pointerdef :
  1160. begin
  1161. { an implicit pointer conversion is allowed }
  1162. if (def_from.deftype=pointerdef) then
  1163. eq:=te_convert_l1;
  1164. end;
  1165. stringdef :
  1166. begin
  1167. { all shortstrings are allowed, size is not important }
  1168. if is_shortstring(def_from) and
  1169. is_shortstring(def_to) then
  1170. eq:=te_equal;
  1171. end;
  1172. objectdef :
  1173. begin
  1174. { child objects can be also passed }
  1175. { in non-delphi mode, otherwise }
  1176. { they must match exactly, except }
  1177. { if they are objects }
  1178. if (def_from.deftype=objectdef) and
  1179. (
  1180. not(m_delphi in aktmodeswitches) or
  1181. (
  1182. (tobjectdef(def_from).objecttype=odt_object) and
  1183. (tobjectdef(def_to).objecttype=odt_object)
  1184. )
  1185. ) and
  1186. (tobjectdef(def_from).is_related(tobjectdef(def_to))) then
  1187. eq:=te_convert_l1;
  1188. end;
  1189. filedef :
  1190. begin
  1191. { an implicit file conversion is also allowed }
  1192. { from a typed file to an untyped one }
  1193. if (def_from.deftype=filedef) and
  1194. (tfiledef(def_from).filetyp = ft_typed) and
  1195. (tfiledef(def_to).filetyp = ft_untyped) then
  1196. eq:=te_convert_l1;
  1197. end;
  1198. end;
  1199. end;
  1200. procedure para_allowed(var eq:tequaltype;p:tcallparanode;def_to:tdef);
  1201. begin
  1202. { Note: eq must be already valid, it will only be updated! }
  1203. case def_to.deftype of
  1204. formaldef :
  1205. begin
  1206. { all types can be passed to a formaldef }
  1207. eq:=te_equal;
  1208. end;
  1209. stringdef :
  1210. begin
  1211. { to support ansi/long/wide strings in a proper way }
  1212. { string and string[10] are assumed as equal }
  1213. { when searching the correct overloaded procedure }
  1214. if (p.resulttype.def.deftype=stringdef) and
  1215. (tstringdef(def_to).string_typ=tstringdef(p.resulttype.def).string_typ) then
  1216. eq:=te_equal
  1217. else
  1218. { Passing a constant char to ansistring or shortstring or
  1219. a widechar to widestring then handle it as equal. }
  1220. if (p.left.nodetype=ordconstn) and
  1221. (
  1222. is_char(p.resulttype.def) and
  1223. (is_shortstring(def_to) or is_ansistring(def_to))
  1224. ) or
  1225. (
  1226. is_widechar(p.resulttype.def) and
  1227. is_widestring(def_to)
  1228. ) then
  1229. eq:=te_equal
  1230. end;
  1231. setdef :
  1232. begin
  1233. { set can also be a not yet converted array constructor }
  1234. if (p.resulttype.def.deftype=arraydef) and
  1235. (tarraydef(p.resulttype.def).IsConstructor) and
  1236. not(tarraydef(p.resulttype.def).IsVariant) then
  1237. eq:=te_equal;
  1238. end;
  1239. procvardef :
  1240. begin
  1241. { in tp7 mode proc -> procvar is allowed }
  1242. if (m_tp_procvar in aktmodeswitches) and
  1243. (p.left.nodetype=calln) and
  1244. (proc_to_procvar_equal(tprocdef(tcallnode(p.left).procdefinition),tprocvardef(def_to),true)>=te_equal) then
  1245. eq:=te_equal;
  1246. end;
  1247. end;
  1248. end;
  1249. {****************************************************************************
  1250. TCallCandidates
  1251. ****************************************************************************}
  1252. constructor tcallcandidates.create(sym:tprocsym;st:tsymtable;ppn:tnode;isprop,ignorevis : boolean);
  1253. var
  1254. j : integer;
  1255. pd : tprocdef;
  1256. hp : pcandidate;
  1257. found,
  1258. has_overload_directive : boolean;
  1259. topclassh : tobjectdef;
  1260. srsymtable : tsymtable;
  1261. srprocsym : tprocsym;
  1262. pt : tcallparanode;
  1263. begin
  1264. if not assigned(sym) then
  1265. internalerror(200411015);
  1266. FProcSym:=sym;
  1267. FProcs:=nil;
  1268. FProccnt:=0;
  1269. FProcvisiblecnt:=0;
  1270. FParanode:=ppn;
  1271. FAllowVariant:=true;
  1272. { determine length of parameter list }
  1273. pt:=tcallparanode(ppn);
  1274. FParalength:=0;
  1275. while assigned(pt) do
  1276. begin
  1277. inc(FParalength);
  1278. pt:=tcallparanode(pt.right);
  1279. end;
  1280. { when the definition has overload directive set, we search for
  1281. overloaded definitions in the class, this only needs to be done once
  1282. for class entries as the tree keeps always the same }
  1283. if (not sym.overloadchecked) and
  1284. (sym.owner.symtabletype=objectsymtable) and
  1285. (po_overload in sym.first_procdef.procoptions) then
  1286. search_class_overloads(sym);
  1287. { when the class passed is defined in this unit we
  1288. need to use the scope of that class. This is a trick
  1289. that can be used to access protected members in other
  1290. units. At least kylix supports it this way (PFV) }
  1291. if assigned(st) and
  1292. (
  1293. (st.symtabletype=objectsymtable) or
  1294. ((st.symtabletype=withsymtable) and
  1295. (st.defowner.deftype=objectdef))
  1296. ) and
  1297. (st.defowner.owner.symtabletype in [globalsymtable,staticsymtable]) and
  1298. st.defowner.owner.iscurrentunit then
  1299. topclassh:=tobjectdef(st.defowner)
  1300. else
  1301. begin
  1302. if assigned(current_procinfo) then
  1303. topclassh:=current_procinfo.procdef._class
  1304. else
  1305. topclassh:=nil;
  1306. end;
  1307. { link all procedures which have the same # of parameters }
  1308. for j:=1 to sym.procdef_count do
  1309. begin
  1310. pd:=sym.procdef[j];
  1311. { Is the procdef visible? This needs to be checked on
  1312. procdef level since a symbol can contain both private and
  1313. public declarations. But the check should not be done
  1314. when the callnode is generated by a property
  1315. inherited overrides invisible anonymous inherited (FK) }
  1316. if isprop or ignorevis or
  1317. (pd.owner.symtabletype<>objectsymtable) or
  1318. pd.is_visible_for_object(topclassh) then
  1319. begin
  1320. { we have at least one procedure that is visible }
  1321. inc(FProcvisiblecnt);
  1322. { only when the # of parameter are supported by the
  1323. procedure }
  1324. if (FParalength>=pd.minparacount) and
  1325. ((po_varargs in pd.procoptions) or { varargs }
  1326. (FParalength<=pd.maxparacount)) then
  1327. proc_add(pd);
  1328. end;
  1329. end;
  1330. { remember if the procedure is declared with the overload directive,
  1331. it's information is still needed also after all procs are removed }
  1332. has_overload_directive:=(po_overload in sym.first_procdef.procoptions);
  1333. { when the definition has overload directive set, we search for
  1334. overloaded definitions in the symtablestack. The found
  1335. entries are only added to the procs list and not the procsym, because
  1336. the list can change in every situation }
  1337. if has_overload_directive and
  1338. (sym.owner.symtabletype<>objectsymtable) then
  1339. begin
  1340. srsymtable:=sym.owner.next;
  1341. while assigned(srsymtable) do
  1342. begin
  1343. if srsymtable.symtabletype in [localsymtable,staticsymtable,globalsymtable] then
  1344. begin
  1345. srprocsym:=tprocsym(srsymtable.speedsearch(sym.name,sym.speedvalue));
  1346. if assigned(srprocsym) and
  1347. (srprocsym.typ=procsym) then
  1348. begin
  1349. { if this visible procedure doesn't have overload we can stop
  1350. searching }
  1351. if not(po_overload in srprocsym.first_procdef.procoptions) and
  1352. srprocsym.first_procdef.is_visible_for_object(topclassh) then
  1353. break;
  1354. { process all overloaded definitions }
  1355. for j:=1 to srprocsym.procdef_count do
  1356. begin
  1357. pd:=srprocsym.procdef[j];
  1358. { only visible procedures need to be added }
  1359. if pd.is_visible_for_object(topclassh) then
  1360. begin
  1361. { only when the # of parameter are supported by the
  1362. procedure }
  1363. if (FParalength>=pd.minparacount) and
  1364. ((po_varargs in pd.procoptions) or { varargs }
  1365. (FParalength<=pd.maxparacount)) then
  1366. begin
  1367. found:=false;
  1368. hp:=FProcs;
  1369. while assigned(hp) do
  1370. begin
  1371. { Only compare visible parameters for the user }
  1372. if compare_paras(hp^.data.paras,pd.paras,cp_value_equal_const,[cpo_ignorehidden])>=te_equal then
  1373. begin
  1374. found:=true;
  1375. break;
  1376. end;
  1377. hp:=hp^.next;
  1378. end;
  1379. if not found then
  1380. proc_add(pd);
  1381. end;
  1382. end;
  1383. end;
  1384. end;
  1385. end;
  1386. srsymtable:=srsymtable.next;
  1387. end;
  1388. end;
  1389. end;
  1390. constructor tcallcandidates.create_operator(op:ttoken;ppn:tnode);
  1391. var
  1392. j : integer;
  1393. pd : tprocdef;
  1394. hp : pcandidate;
  1395. found : boolean;
  1396. srsymtable : tsymtable;
  1397. srprocsym : tprocsym;
  1398. pt : tcallparanode;
  1399. sv : cardinal;
  1400. begin
  1401. FProcSym:=nil;
  1402. FProcs:=nil;
  1403. FProccnt:=0;
  1404. FProcvisiblecnt:=0;
  1405. FParanode:=ppn;
  1406. FAllowVariant:=false;
  1407. { determine length of parameter list }
  1408. pt:=tcallparanode(ppn);
  1409. FParalength:=0;
  1410. while assigned(pt) do
  1411. begin
  1412. if pt.resulttype.def.deftype=variantdef then
  1413. FAllowVariant:=true;
  1414. inc(FParalength);
  1415. pt:=tcallparanode(pt.right);
  1416. end;
  1417. { we search all overloaded operator definitions in the symtablestack. The found
  1418. entries are only added to the procs list and not the procsym, because
  1419. the list can change in every situation }
  1420. sv:=getspeedvalue(overloaded_names[op]);
  1421. srsymtable:=symtablestack;
  1422. while assigned(srsymtable) do
  1423. begin
  1424. if srsymtable.symtabletype in [localsymtable,staticsymtable,globalsymtable] then
  1425. begin
  1426. srprocsym:=tprocsym(srsymtable.speedsearch(overloaded_names[op],sv));
  1427. if assigned(srprocsym) and
  1428. (srprocsym.typ=procsym) then
  1429. begin
  1430. { Store first procsym found }
  1431. if not assigned(FProcsym) then
  1432. FProcsym:=srprocsym;
  1433. { process all overloaded definitions }
  1434. for j:=1 to srprocsym.procdef_count do
  1435. begin
  1436. pd:=srprocsym.procdef[j];
  1437. { only when the # of parameter are supported by the
  1438. procedure }
  1439. if (FParalength>=pd.minparacount) and
  1440. (FParalength<=pd.maxparacount) then
  1441. begin
  1442. found:=false;
  1443. hp:=FProcs;
  1444. while assigned(hp) do
  1445. begin
  1446. { Only compare visible parameters for the user }
  1447. if compare_paras(hp^.data.paras,pd.paras,cp_value_equal_const,[cpo_ignorehidden])>=te_equal then
  1448. begin
  1449. found:=true;
  1450. break;
  1451. end;
  1452. hp:=hp^.next;
  1453. end;
  1454. if not found then
  1455. proc_add(pd);
  1456. end;
  1457. end;
  1458. end;
  1459. end;
  1460. srsymtable:=srsymtable.next;
  1461. end;
  1462. end;
  1463. destructor tcallcandidates.destroy;
  1464. var
  1465. hpnext,
  1466. hp : pcandidate;
  1467. begin
  1468. hp:=FProcs;
  1469. while assigned(hp) do
  1470. begin
  1471. hpnext:=hp^.next;
  1472. dispose(hp);
  1473. hp:=hpnext;
  1474. end;
  1475. end;
  1476. function tcallcandidates.proc_add(pd:tprocdef):pcandidate;
  1477. var
  1478. defaultparacnt : integer;
  1479. begin
  1480. { generate new candidate entry }
  1481. new(result);
  1482. fillchar(result^,sizeof(tcandidate),0);
  1483. result^.data:=pd;
  1484. result^.next:=FProcs;
  1485. FProcs:=result;
  1486. inc(FProccnt);
  1487. { Find last parameter, skip all default parameters
  1488. that are not passed. Ignore this skipping for varargs }
  1489. result^.firstparaidx:=pd.paras.count-1;
  1490. if not(po_varargs in pd.procoptions) then
  1491. begin
  1492. { ignore hidden parameters }
  1493. while (result^.firstparaidx>=0) and (vo_is_hidden_para in tparavarsym(pd.paras[result^.firstparaidx]).varoptions) do
  1494. dec(result^.firstparaidx);
  1495. defaultparacnt:=pd.maxparacount-FParalength;
  1496. if defaultparacnt>0 then
  1497. begin
  1498. if defaultparacnt>result^.firstparaidx+1 then
  1499. internalerror(200401141);
  1500. dec(result^.firstparaidx,defaultparacnt);
  1501. end;
  1502. end;
  1503. end;
  1504. procedure tcallcandidates.list(all:boolean);
  1505. var
  1506. hp : pcandidate;
  1507. begin
  1508. hp:=FProcs;
  1509. while assigned(hp) do
  1510. begin
  1511. if all or
  1512. (not hp^.invalid) then
  1513. MessagePos1(hp^.data.fileinfo,sym_h_param_list,hp^.data.fullprocname(false));
  1514. hp:=hp^.next;
  1515. end;
  1516. end;
  1517. {$ifdef EXTDEBUG}
  1518. procedure tcallcandidates.dump_info(lvl:longint);
  1519. function ParaTreeStr(p:tcallparanode):string;
  1520. begin
  1521. result:='';
  1522. while assigned(p) do
  1523. begin
  1524. if result<>'' then
  1525. result:=result+',';
  1526. result:=result+p.resulttype.def.typename;
  1527. p:=tcallparanode(p.right);
  1528. end;
  1529. end;
  1530. var
  1531. hp : pcandidate;
  1532. i : integer;
  1533. currpara : tparavarsym;
  1534. begin
  1535. if not CheckVerbosity(lvl) then
  1536. exit;
  1537. Comment(lvl+V_LineInfo,'Overloaded callnode: '+FProcSym.name+'('+ParaTreeStr(tcallparanode(FParaNode))+')');
  1538. hp:=FProcs;
  1539. while assigned(hp) do
  1540. begin
  1541. Comment(lvl,' '+hp^.data.fullprocname(false));
  1542. if (hp^.invalid) then
  1543. Comment(lvl,' invalid')
  1544. else
  1545. begin
  1546. Comment(lvl,' ex: '+tostr(hp^.exact_count)+
  1547. ' eq: '+tostr(hp^.equal_count)+
  1548. ' l1: '+tostr(hp^.cl1_count)+
  1549. ' l2: '+tostr(hp^.cl2_count)+
  1550. ' l3: '+tostr(hp^.cl3_count)+
  1551. ' oper: '+tostr(hp^.coper_count)+
  1552. ' ord: '+realtostr(hp^.ordinal_distance));
  1553. { Print parameters in left-right order }
  1554. for i:=0 to hp^.data.paras.count-1 do
  1555. begin
  1556. currpara:=tparavarsym(hp^.data.paras[i]);
  1557. if (vo_is_hidden_para in currpara.varoptions) then
  1558. Comment(lvl,' - '+currpara.vartype.def.typename+' : '+EqualTypeName[currpara.eqval]);
  1559. end;
  1560. end;
  1561. hp:=hp^.next;
  1562. end;
  1563. end;
  1564. {$endif EXTDEBUG}
  1565. procedure tcallcandidates.get_information;
  1566. var
  1567. hp : pcandidate;
  1568. currpara : tparavarsym;
  1569. paraidx : integer;
  1570. currparanr : byte;
  1571. rfh,rth : bestreal;
  1572. def_from,
  1573. def_to : tdef;
  1574. currpt,
  1575. pt : tcallparanode;
  1576. eq : tequaltype;
  1577. convtype : tconverttype;
  1578. pdoper : tprocdef;
  1579. releasecurrpt : boolean;
  1580. cdoptions : tcompare_defs_options;
  1581. begin
  1582. cdoptions:=[cdo_check_operator];
  1583. if FAllowVariant then
  1584. include(cdoptions,cdo_allow_variant);
  1585. { process all procs }
  1586. hp:=FProcs;
  1587. while assigned(hp) do
  1588. begin
  1589. { We compare parameters in reverse order (right to left),
  1590. the firstpara is already pointing to the last parameter
  1591. were we need to start comparing }
  1592. currparanr:=FParalength;
  1593. paraidx:=hp^.firstparaidx;
  1594. while (paraidx>=0) and (vo_is_hidden_para in tparavarsym(hp^.data.paras[paraidx]).varoptions) do
  1595. dec(paraidx);
  1596. pt:=tcallparanode(FParaNode);
  1597. while assigned(pt) and (paraidx>=0) do
  1598. begin
  1599. currpara:=tparavarsym(hp^.data.paras[paraidx]);
  1600. { currpt can be changed from loadn to calln when a procvar
  1601. is passed. This is to prevent that the change is permanent }
  1602. currpt:=pt;
  1603. releasecurrpt:=false;
  1604. { retrieve current parameter definitions to compares }
  1605. eq:=te_incompatible;
  1606. def_from:=currpt.resulttype.def;
  1607. def_to:=currpara.vartype.def;
  1608. if not(assigned(def_from)) then
  1609. internalerror(200212091);
  1610. if not(
  1611. assigned(def_to) or
  1612. ((po_varargs in hp^.data.procoptions) and
  1613. (currparanr>hp^.data.minparacount))
  1614. ) then
  1615. internalerror(200212092);
  1616. { Convert tp procvars when not expecting a procvar }
  1617. if (def_to.deftype<>procvardef) and
  1618. (currpt.left.resulttype.def.deftype=procvardef) then
  1619. begin
  1620. releasecurrpt:=true;
  1621. currpt:=tcallparanode(pt.getcopy);
  1622. if maybe_call_procvar(currpt.left,true) then
  1623. begin
  1624. currpt.resulttype:=currpt.left.resulttype;
  1625. def_from:=currpt.left.resulttype.def;
  1626. end;
  1627. end;
  1628. { varargs are always equal, but not exact }
  1629. if (po_varargs in hp^.data.procoptions) and
  1630. (currparanr>hp^.data.minparacount) then
  1631. begin
  1632. eq:=te_equal;
  1633. end
  1634. else
  1635. { same definition -> exact }
  1636. if (def_from=def_to) then
  1637. begin
  1638. eq:=te_exact;
  1639. end
  1640. else
  1641. { for value and const parameters check if a integer is constant or
  1642. included in other integer -> equal and calc ordinal_distance }
  1643. if not(currpara.varspez in [vs_var,vs_out]) and
  1644. is_integer(def_from) and
  1645. is_integer(def_to) and
  1646. is_in_limit(def_from,def_to) then
  1647. begin
  1648. eq:=te_equal;
  1649. hp^.ordinal_distance:=hp^.ordinal_distance+
  1650. abs(bestreal(torddef(def_from).low)-bestreal(torddef(def_to).low));
  1651. if (torddef(def_to).typ=u64bit) then
  1652. rth:=bestreal(qword(torddef(def_to).high))
  1653. else
  1654. rth:=bestreal(torddef(def_to).high);
  1655. if (torddef(def_from).typ=u64bit) then
  1656. rfh:=bestreal(qword(torddef(def_from).high))
  1657. else
  1658. rfh:=bestreal(torddef(def_from).high);
  1659. hp^.ordinal_distance:=hp^.ordinal_distance+abs(rth-rfh);
  1660. { Give wrong sign a small penalty, this is need to get a diffrence
  1661. from word->[longword,longint] }
  1662. if is_signed(def_from)<>is_signed(def_to) then
  1663. hp^.ordinal_distance:=hp^.ordinal_distance+1.0;
  1664. end
  1665. else
  1666. { generic type comparision }
  1667. begin
  1668. eq:=compare_defs_ext(def_from,def_to,currpt.left.nodetype,convtype,pdoper,cdoptions);
  1669. { when the types are not equal we need to check
  1670. some special case for parameter passing }
  1671. if (eq<te_equal) then
  1672. begin
  1673. if currpara.varspez in [vs_var,vs_out] then
  1674. begin
  1675. { para requires an equal type so the previous found
  1676. match was not good enough, reset to incompatible }
  1677. eq:=te_incompatible;
  1678. { var_para_allowed will return te_equal and te_convert_l1 to
  1679. make a difference for best matching }
  1680. var_para_allowed(eq,currpt.resulttype.def,currpara.vartype.def)
  1681. end
  1682. else
  1683. para_allowed(eq,currpt,def_to);
  1684. end;
  1685. end;
  1686. { when a procvar was changed to a call an exact much is
  1687. downgraded to equal. This way an overload call with the
  1688. procvar is choosen. See tb0471 (PFV) }
  1689. if (pt<>currpt) and (eq=te_exact) then
  1690. eq:=te_equal;
  1691. { increase correct counter }
  1692. case eq of
  1693. te_exact :
  1694. inc(hp^.exact_count);
  1695. te_equal :
  1696. inc(hp^.equal_count);
  1697. te_convert_l1 :
  1698. inc(hp^.cl1_count);
  1699. te_convert_l2 :
  1700. inc(hp^.cl2_count);
  1701. te_convert_l3 :
  1702. inc(hp^.cl3_count);
  1703. te_convert_operator :
  1704. inc(hp^.coper_count);
  1705. te_incompatible :
  1706. hp^.invalid:=true;
  1707. else
  1708. internalerror(200212072);
  1709. end;
  1710. { stop checking when an incompatible parameter is found }
  1711. if hp^.invalid then
  1712. begin
  1713. { store the current parameter info for
  1714. a nice error message when no procedure is found }
  1715. hp^.wrongparaidx:=paraidx;
  1716. hp^.wrongparanr:=currparanr;
  1717. break;
  1718. end;
  1719. {$ifdef EXTDEBUG}
  1720. { store equal in node tree for dump }
  1721. currpara.eqval:=eq;
  1722. {$endif EXTDEBUG}
  1723. { maybe release temp currpt }
  1724. if releasecurrpt then
  1725. currpt.free;
  1726. { next parameter in the call tree }
  1727. pt:=tcallparanode(pt.right);
  1728. { next parameter for definition, only goto next para
  1729. if we're out of the varargs }
  1730. if not(po_varargs in hp^.data.procoptions) or
  1731. (currparanr<=hp^.data.maxparacount) then
  1732. begin
  1733. { Ignore vs_hidden parameters }
  1734. repeat
  1735. dec(paraidx);
  1736. until (paraidx<0) or not(vo_is_hidden_para in tparavarsym(hp^.data.paras[paraidx]).varoptions);
  1737. end;
  1738. dec(currparanr);
  1739. end;
  1740. if not(hp^.invalid) and
  1741. (assigned(pt) or (paraidx>=0) or (currparanr<>0)) then
  1742. internalerror(200212141);
  1743. { next candidate }
  1744. hp:=hp^.next;
  1745. end;
  1746. end;
  1747. function is_better_candidate(currpd,bestpd:pcandidate):integer;
  1748. var
  1749. res : integer;
  1750. begin
  1751. {
  1752. Return values:
  1753. > 0 when currpd is better than bestpd
  1754. < 0 when bestpd is better than currpd
  1755. = 0 when both are equal
  1756. To choose the best candidate we use the following order:
  1757. - Incompatible flag
  1758. - (Smaller) Number of convert operator parameters.
  1759. - (Smaller) Number of convertlevel 2 parameters.
  1760. - (Smaller) Number of convertlevel 1 parameters.
  1761. - (Bigger) Number of exact parameters.
  1762. - (Smaller) Number of equal parameters.
  1763. - (Smaller) Total of ordinal distance. For example, the distance of a word
  1764. to a byte is 65535-255=65280.
  1765. }
  1766. if bestpd^.invalid then
  1767. begin
  1768. if currpd^.invalid then
  1769. res:=0
  1770. else
  1771. res:=1;
  1772. end
  1773. else
  1774. if currpd^.invalid then
  1775. res:=-1
  1776. else
  1777. begin
  1778. { less operator parameters? }
  1779. res:=(bestpd^.coper_count-currpd^.coper_count);
  1780. if (res=0) then
  1781. begin
  1782. { less cl3 parameters? }
  1783. res:=(bestpd^.cl3_count-currpd^.cl3_count);
  1784. if (res=0) then
  1785. begin
  1786. { less cl2 parameters? }
  1787. res:=(bestpd^.cl2_count-currpd^.cl2_count);
  1788. if (res=0) then
  1789. begin
  1790. { less cl1 parameters? }
  1791. res:=(bestpd^.cl1_count-currpd^.cl1_count);
  1792. if (res=0) then
  1793. begin
  1794. { more exact parameters? }
  1795. res:=(currpd^.exact_count-bestpd^.exact_count);
  1796. if (res=0) then
  1797. begin
  1798. { less equal parameters? }
  1799. res:=(bestpd^.equal_count-currpd^.equal_count);
  1800. if (res=0) then
  1801. begin
  1802. { smaller ordinal distance? }
  1803. if (currpd^.ordinal_distance<bestpd^.ordinal_distance) then
  1804. res:=1
  1805. else
  1806. if (currpd^.ordinal_distance>bestpd^.ordinal_distance) then
  1807. res:=-1
  1808. else
  1809. res:=0;
  1810. end;
  1811. end;
  1812. end;
  1813. end;
  1814. end;
  1815. end;
  1816. end;
  1817. is_better_candidate:=res;
  1818. end;
  1819. function tcallcandidates.choose_best(var bestpd:tabstractprocdef):integer;
  1820. var
  1821. besthpstart,
  1822. hp : pcandidate;
  1823. cntpd,
  1824. res : integer;
  1825. begin
  1826. {
  1827. Returns the number of candidates left and the
  1828. first candidate is returned in pdbest
  1829. }
  1830. { Setup the first procdef as best, only count it as a result
  1831. when it is valid }
  1832. bestpd:=FProcs^.data;
  1833. if FProcs^.invalid then
  1834. cntpd:=0
  1835. else
  1836. cntpd:=1;
  1837. if assigned(FProcs^.next) then
  1838. begin
  1839. besthpstart:=FProcs;
  1840. hp:=FProcs^.next;
  1841. while assigned(hp) do
  1842. begin
  1843. res:=is_better_candidate(hp,besthpstart);
  1844. if (res>0) then
  1845. begin
  1846. { hp is better, flag all procs to be incompatible }
  1847. while (besthpstart<>hp) do
  1848. begin
  1849. besthpstart^.invalid:=true;
  1850. besthpstart:=besthpstart^.next;
  1851. end;
  1852. { besthpstart is already set to hp }
  1853. bestpd:=besthpstart^.data;
  1854. cntpd:=1;
  1855. end
  1856. else
  1857. if (res<0) then
  1858. begin
  1859. { besthpstart is better, flag current hp to be incompatible }
  1860. hp^.invalid:=true;
  1861. end
  1862. else
  1863. begin
  1864. { res=0, both are valid }
  1865. if not hp^.invalid then
  1866. inc(cntpd);
  1867. end;
  1868. hp:=hp^.next;
  1869. end;
  1870. end;
  1871. result:=cntpd;
  1872. end;
  1873. procedure tcallcandidates.find_wrong_para;
  1874. var
  1875. currparanr : smallint;
  1876. hp : pcandidate;
  1877. pt : tcallparanode;
  1878. wrongpara : tparavarsym;
  1879. begin
  1880. { Only process the first overloaded procdef }
  1881. hp:=FProcs;
  1882. { Find callparanode corresponding to the argument }
  1883. pt:=tcallparanode(FParanode);
  1884. currparanr:=FParalength;
  1885. while assigned(pt) and
  1886. (currparanr>hp^.wrongparanr) do
  1887. begin
  1888. pt:=tcallparanode(pt.right);
  1889. dec(currparanr);
  1890. end;
  1891. if (currparanr<>hp^.wrongparanr) or
  1892. not assigned(pt) then
  1893. internalerror(200212094);
  1894. { Show error message, when it was a var or out parameter
  1895. guess that it is a missing typeconv }
  1896. wrongpara:=tparavarsym(hp^.data.paras[hp^.wrongparaidx]);
  1897. if wrongpara.varspez in [vs_var,vs_out] then
  1898. begin
  1899. { Maybe passing the correct type but passing a const to var parameter }
  1900. if (compare_defs(pt.resulttype.def,wrongpara.vartype.def,pt.nodetype)<>te_incompatible) and
  1901. not valid_for_var(pt.left) then
  1902. CGMessagePos(pt.left.fileinfo,type_e_variable_id_expected)
  1903. else
  1904. CGMessagePos2(pt.left.fileinfo,parser_e_call_by_ref_without_typeconv,
  1905. FullTypeName(pt.left.resulttype.def,wrongpara.vartype.def),
  1906. FullTypeName(wrongpara.vartype.def,pt.left.resulttype.def))
  1907. end
  1908. else
  1909. CGMessagePos3(pt.left.fileinfo,type_e_wrong_parameter_type,tostr(hp^.wrongparanr),
  1910. FullTypeName(pt.left.resulttype.def,wrongpara.vartype.def),
  1911. FullTypeName(wrongpara.vartype.def,pt.left.resulttype.def));
  1912. end;
  1913. end.