htypechk.pas 74 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Florian Klaempfl
  4. This unit exports some help routines for the type checking
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit htypechk;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. tokens,cpuinfo,
  23. node,
  24. symconst,symtype,symdef,symsym,symbase;
  25. type
  26. Ttok2nodeRec=record
  27. tok : ttoken;
  28. nod : tnodetype;
  29. op_overloading_supported : boolean;
  30. end;
  31. pcandidate = ^tcandidate;
  32. tcandidate = record
  33. next : pcandidate;
  34. data : tprocdef;
  35. wrongparaidx,
  36. firstparaidx : integer;
  37. exact_count,
  38. equal_count,
  39. cl1_count,
  40. cl2_count,
  41. cl3_count,
  42. coper_count : integer; { should be signed }
  43. ordinal_distance : bestreal;
  44. invalid : boolean;
  45. wrongparanr : byte;
  46. end;
  47. tcallcandidates = class
  48. private
  49. FProcSym : tprocsym;
  50. FProcs : pcandidate;
  51. FProcVisibleCnt,
  52. FProcCnt : integer;
  53. FParaNode : tnode;
  54. FParaLength : smallint;
  55. FAllowVariant : boolean;
  56. function proc_add(pd:tprocdef):pcandidate;
  57. public
  58. constructor create(sym:tprocsym;st:tsymtable;ppn:tnode;isprop,ignorevis : boolean);
  59. constructor create_operator(op:ttoken;ppn:tnode);
  60. destructor destroy;override;
  61. procedure list(all:boolean);
  62. {$ifdef EXTDEBUG}
  63. procedure dump_info(lvl:longint);
  64. {$endif EXTDEBUG}
  65. procedure get_information;
  66. function choose_best(var bestpd:tabstractprocdef):integer;
  67. procedure find_wrong_para;
  68. property Count:integer read FProcCnt;
  69. property VisibleCount:integer read FProcVisibleCnt;
  70. end;
  71. const
  72. tok2nodes=25;
  73. tok2node:array[1..tok2nodes] of ttok2noderec=(
  74. (tok:_PLUS ;nod:addn;op_overloading_supported:true), { binary overloading supported }
  75. (tok:_MINUS ;nod:subn;op_overloading_supported:true), { binary and unary overloading supported }
  76. (tok:_STAR ;nod:muln;op_overloading_supported:true), { binary overloading supported }
  77. (tok:_SLASH ;nod:slashn;op_overloading_supported:true), { binary overloading supported }
  78. (tok:_EQUAL ;nod:equaln;op_overloading_supported:true), { binary overloading supported }
  79. (tok:_GT ;nod:gtn;op_overloading_supported:true), { binary overloading supported }
  80. (tok:_LT ;nod:ltn;op_overloading_supported:true), { binary overloading supported }
  81. (tok:_GTE ;nod:gten;op_overloading_supported:true), { binary overloading supported }
  82. (tok:_LTE ;nod:lten;op_overloading_supported:true), { binary overloading supported }
  83. (tok:_SYMDIF ;nod:symdifn;op_overloading_supported:true), { binary overloading supported }
  84. (tok:_STARSTAR;nod:starstarn;op_overloading_supported:true), { binary overloading supported }
  85. (tok:_OP_AS ;nod:asn;op_overloading_supported:false), { binary overloading NOT supported }
  86. (tok:_OP_IN ;nod:inn;op_overloading_supported:false), { binary overloading NOT supported }
  87. (tok:_OP_IS ;nod:isn;op_overloading_supported:false), { binary overloading NOT supported }
  88. (tok:_OP_OR ;nod:orn;op_overloading_supported:true), { binary overloading supported }
  89. (tok:_OP_AND ;nod:andn;op_overloading_supported:true), { binary overloading supported }
  90. (tok:_OP_DIV ;nod:divn;op_overloading_supported:true), { binary overloading supported }
  91. (tok:_OP_NOT ;nod:notn;op_overloading_supported:true), { unary overloading supported }
  92. (tok:_OP_MOD ;nod:modn;op_overloading_supported:true), { binary overloading supported }
  93. (tok:_OP_SHL ;nod:shln;op_overloading_supported:true), { binary overloading supported }
  94. (tok:_OP_SHR ;nod:shrn;op_overloading_supported:true), { binary overloading supported }
  95. (tok:_OP_XOR ;nod:xorn;op_overloading_supported:true), { binary overloading supported }
  96. (tok:_ASSIGNMENT;nod:assignn;op_overloading_supported:true), { unary overloading supported }
  97. (tok:_CARET ;nod:caretn;op_overloading_supported:false), { binary overloading NOT supported }
  98. (tok:_UNEQUAL ;nod:unequaln;op_overloading_supported:false) { binary overloading NOT supported overload = instead }
  99. );
  100. const
  101. { firstcallparan without varspez we don't count the ref }
  102. {$ifdef extdebug}
  103. count_ref : boolean = true;
  104. {$endif def extdebug}
  105. allow_array_constructor : boolean = false;
  106. function node2opstr(nt:tnodetype):string;
  107. { check operator args and result type }
  108. function isbinaryoperatoroverloadable(treetyp:tnodetype;ld:tdef;lt:tnodetype;rd:tdef;rt:tnodetype) : boolean;
  109. function isoperatoracceptable(pf : tprocdef; optoken : ttoken) : boolean;
  110. function isunaryoverloaded(var t : tnode) : boolean;
  111. function isbinaryoverloaded(var t : tnode) : boolean;
  112. { Register Allocation }
  113. procedure make_not_regable(p : tnode);
  114. procedure calcregisters(p : tbinarynode;r32,fpu,mmx : word);
  115. { procvar handling }
  116. function is_procvar_load(p:tnode):boolean;
  117. procedure test_local_to_procvar(from_def:tprocvardef;to_def:tdef);
  118. { sets varsym varstate field correctly }
  119. procedure set_varstate(p:tnode;newstate:tvarstate;must_be_valid:boolean);
  120. { sets the callunique flag, if the node is a vecn, }
  121. { takes care of type casts etc. }
  122. procedure set_unique(p : tnode);
  123. function valid_for_formal_var(p : tnode) : boolean;
  124. function valid_for_formal_const(p : tnode) : boolean;
  125. function valid_for_var(p:tnode):boolean;
  126. function valid_for_assignment(p:tnode):boolean;
  127. function valid_for_addr(p : tnode) : boolean;
  128. implementation
  129. uses
  130. globtype,systems,
  131. cutils,verbose,globals,
  132. symtable,
  133. defutil,defcmp,
  134. nbas,ncnv,nld,nmem,ncal,nmat,ninl,nutils,
  135. cgbase,procinfo
  136. ;
  137. type
  138. TValidAssign=(Valid_Property,Valid_Void,Valid_Const,Valid_Addr);
  139. TValidAssigns=set of TValidAssign;
  140. function node2opstr(nt:tnodetype):string;
  141. var
  142. i : integer;
  143. begin
  144. result:='<unknown>';
  145. for i:=1 to tok2nodes do
  146. if tok2node[i].nod=nt then
  147. begin
  148. result:=tokeninfo^[tok2node[i].tok].str;
  149. break;
  150. end;
  151. end;
  152. function isbinaryoperatoroverloadable(treetyp:tnodetype;ld:tdef;lt:tnodetype;rd:tdef;rt:tnodetype) : boolean;
  153. function internal_check(treetyp:tnodetype;ld:tdef;lt:tnodetype;rd:tdef;rt:tnodetype;var allowed:boolean):boolean;
  154. begin
  155. internal_check:=true;
  156. case ld.deftype of
  157. formaldef,
  158. recorddef,
  159. variantdef :
  160. begin
  161. allowed:=true;
  162. end;
  163. procvardef :
  164. begin
  165. if (rd.deftype in [pointerdef,procdef,procvardef]) then
  166. begin
  167. allowed:=false;
  168. exit;
  169. end;
  170. allowed:=true;
  171. end;
  172. pointerdef :
  173. begin
  174. if ((rd.deftype in [orddef,enumdef,pointerdef,classrefdef,procvardef]) or
  175. is_class_or_interface(rd)) then
  176. begin
  177. allowed:=false;
  178. exit;
  179. end;
  180. { don't allow pchar+string }
  181. if (is_pchar(ld) or is_pwidechar(ld)) and
  182. ((rd.deftype=stringdef) or
  183. is_pchar(rd) or
  184. is_pwidechar(rd) or
  185. is_chararray(rd) or
  186. is_widechararray(rd)) then
  187. begin
  188. allowed:=false;
  189. exit;
  190. end;
  191. allowed:=true;
  192. end;
  193. arraydef :
  194. begin
  195. { not mmx }
  196. if (cs_mmx in aktlocalswitches) and
  197. is_mmx_able_array(ld) then
  198. begin
  199. allowed:=false;
  200. exit;
  201. end;
  202. { not chararray+[(wide)char,(wide)string,(wide)chararray] }
  203. if (is_chararray(ld) or is_widechararray(ld) or
  204. is_open_chararray(ld) or is_open_widechararray(ld))
  205. and
  206. ((rd.deftype in [stringdef,orddef,enumdef]) or
  207. is_pchar(rd) or
  208. is_pwidechar(rd) or
  209. is_chararray(rd) or
  210. is_widechararray(rd) or
  211. is_open_chararray(rd) or
  212. is_open_widechararray(rd) or
  213. (rt=niln)) then
  214. begin
  215. allowed:=false;
  216. exit;
  217. end;
  218. { dynamic array compare with niln }
  219. if ((is_dynamic_array(ld) and
  220. (rt=niln)) or
  221. (is_dynamic_array(ld) and is_dynamic_array(rd)))
  222. and
  223. (treetyp in [equaln,unequaln]) then
  224. begin
  225. allowed:=false;
  226. exit;
  227. end;
  228. allowed:=true;
  229. end;
  230. objectdef :
  231. begin
  232. { <> and = are defined for classes }
  233. if (treetyp in [equaln,unequaln]) and
  234. is_class_or_interface(ld) then
  235. begin
  236. allowed:=false;
  237. exit;
  238. end;
  239. allowed:=true;
  240. end;
  241. stringdef :
  242. begin
  243. if (rd.deftype in [orddef,enumdef,stringdef]) or
  244. is_pchar(rd) or
  245. is_pwidechar(rd) or
  246. is_chararray(rd) or
  247. is_widechararray(rd) or
  248. is_open_chararray(rd) or
  249. is_open_widechararray(rd) then
  250. begin
  251. allowed:=false;
  252. exit;
  253. end;
  254. allowed:=true;
  255. end;
  256. else
  257. internal_check:=false;
  258. end;
  259. end;
  260. var
  261. allowed : boolean;
  262. begin
  263. { power ** is always possible }
  264. if (treetyp=starstarn) then
  265. begin
  266. isbinaryoperatoroverloadable:=true;
  267. exit;
  268. end;
  269. { order of arguments does not matter so we have to check also
  270. the reversed order }
  271. allowed:=false;
  272. if not internal_check(treetyp,ld,lt,rd,rt,allowed) then
  273. internal_check(treetyp,rd,rt,ld,lt,allowed);
  274. isbinaryoperatoroverloadable:=allowed;
  275. end;
  276. function isunaryoperatoroverloadable(treetyp : tnodetype;ld : tdef) : boolean;
  277. begin
  278. result:=false;
  279. case treetyp of
  280. subn,
  281. unaryminusn :
  282. begin
  283. if is_integer(ld) or
  284. (ld.deftype=floatdef) then
  285. exit;
  286. {$ifdef SUPPORT_MMX}
  287. if (cs_mmx in aktlocalswitches) and
  288. is_mmx_able_array(ld) then
  289. exit;
  290. {$endif SUPPORT_MMX}
  291. result:=true;
  292. end;
  293. notn :
  294. begin
  295. if is_integer(ld) or
  296. is_boolean(ld) 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;must_be_valid:boolean);
  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. must_be_valid:=false;
  667. tc_pchar_2_string,
  668. tc_pointer_2_array :
  669. must_be_valid:=true;
  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,true);
  678. if not(tunarynode(p).left.resulttype.def.deftype in [stringdef,arraydef]) then
  679. must_be_valid:=true;
  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 must_be_valid 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. if tloadnode(p).symtable.symtabletype=localsymtable then
  702. CGMessage1(sym_n_uninitialized_local_variable,hsym.realname)
  703. else
  704. CGMessage1(sym_n_uninitialized_variable,hsym.realname);
  705. end;
  706. end;
  707. { don't override vs_used with vs_assigned }
  708. if hsym.varstate<>vs_used then
  709. hsym.varstate:=newstate;
  710. end;
  711. break;
  712. end;
  713. callparan :
  714. internalerror(200310081);
  715. else
  716. break;
  717. end;{case }
  718. end;
  719. end;
  720. procedure set_unique(p : tnode);
  721. begin
  722. while assigned(p) do
  723. begin
  724. case p.nodetype of
  725. vecn:
  726. begin
  727. include(p.flags,nf_callunique);
  728. break;
  729. end;
  730. typeconvn,
  731. subscriptn,
  732. derefn:
  733. p:=tunarynode(p).left;
  734. else
  735. break;
  736. end;
  737. end;
  738. end;
  739. function valid_for_assign(p:tnode;opts:TValidAssigns):boolean;
  740. var
  741. hp : tnode;
  742. gotwith,
  743. gotsubscript,
  744. gotpointer,
  745. gotvec,
  746. gotclass,
  747. gotdynarray,
  748. gotderef : boolean;
  749. fromdef,
  750. todef : tdef;
  751. errmsg : longint;
  752. begin
  753. if valid_const in opts then
  754. errmsg:=type_e_variable_id_expected
  755. else
  756. errmsg:=type_e_argument_cant_be_assigned;
  757. result:=false;
  758. gotsubscript:=false;
  759. gotvec:=false;
  760. gotderef:=false;
  761. gotclass:=false;
  762. gotpointer:=false;
  763. gotwith:=false;
  764. gotdynarray:=false;
  765. hp:=p;
  766. if not(valid_void in opts) and
  767. is_void(hp.resulttype.def) then
  768. begin
  769. CGMessagePos(hp.fileinfo,errmsg);
  770. exit;
  771. end;
  772. while assigned(hp) do
  773. begin
  774. { property allowed? calln has a property check itself }
  775. if (nf_isproperty in hp.flags) then
  776. begin
  777. if (valid_property in opts) then
  778. result:=true
  779. else
  780. begin
  781. { check return type }
  782. case hp.resulttype.def.deftype of
  783. pointerdef :
  784. gotpointer:=true;
  785. objectdef :
  786. gotclass:=is_class_or_interface(hp.resulttype.def);
  787. recorddef, { handle record like class it needs a subscription }
  788. classrefdef :
  789. gotclass:=true;
  790. end;
  791. { 1. if it returns a pointer and we've found a deref,
  792. 2. if it returns a class or record and a subscription or with is found
  793. 3. if the address is needed of a field (subscriptn) }
  794. if (gotpointer and gotderef) or
  795. (
  796. gotclass and
  797. (gotsubscript or gotwith)
  798. ) or
  799. (
  800. (gotvec and gotdynarray)
  801. ) or
  802. (
  803. (Valid_Addr in opts) and
  804. (hp.nodetype=subscriptn)
  805. ) then
  806. result:=true
  807. else
  808. CGMessagePos(hp.fileinfo,errmsg);
  809. end;
  810. exit;
  811. end;
  812. if (Valid_Const in opts) and is_constnode(hp) then
  813. begin
  814. result:=true;
  815. exit;
  816. end;
  817. case hp.nodetype of
  818. temprefn :
  819. begin
  820. valid_for_assign := true;
  821. exit;
  822. end;
  823. derefn :
  824. begin
  825. gotderef:=true;
  826. hp:=tderefnode(hp).left;
  827. end;
  828. typeconvn :
  829. begin
  830. { typecast sizes must match, exceptions:
  831. - implicit typecast made by absolute
  832. - from formaldef
  833. - from void
  834. - from/to open array
  835. - typecast from pointer to array }
  836. fromdef:=ttypeconvnode(hp).left.resulttype.def;
  837. todef:=hp.resulttype.def;
  838. if not((nf_absolute in ttypeconvnode(hp).flags) or
  839. (fromdef.deftype=formaldef) or
  840. is_void(fromdef) or
  841. is_open_array(fromdef) or
  842. is_open_array(todef) or
  843. ((fromdef.deftype=pointerdef) and (todef.deftype=arraydef)) or
  844. ((fromdef.deftype = objectdef) and (todef.deftype = objectdef) and
  845. (tobjectdef(fromdef).is_related(tobjectdef(todef))))) and
  846. (fromdef.size<>todef.size) then
  847. begin
  848. { in TP it is allowed to typecast to smaller types. But the variable can't
  849. be in a register }
  850. if (m_tp7 in aktmodeswitches) or
  851. (todef.size<fromdef.size) then
  852. make_not_regable(hp)
  853. else
  854. CGMessagePos2(hp.fileinfo,type_e_typecast_wrong_size_for_assignment,tostr(fromdef.size),tostr(todef.size));
  855. end;
  856. { don't allow assignments to typeconvs that need special code }
  857. if not(gotsubscript or gotvec or gotderef) and
  858. not(ttypeconvnode(hp).assign_allowed) then
  859. begin
  860. CGMessagePos(hp.fileinfo,errmsg);
  861. exit;
  862. end;
  863. case hp.resulttype.def.deftype of
  864. pointerdef :
  865. gotpointer:=true;
  866. objectdef :
  867. gotclass:=is_class_or_interface(hp.resulttype.def);
  868. classrefdef :
  869. gotclass:=true;
  870. arraydef :
  871. begin
  872. { pointer -> array conversion is done then we need to see it
  873. as a deref, because a ^ is then not required anymore }
  874. if (ttypeconvnode(hp).left.resulttype.def.deftype=pointerdef) then
  875. gotderef:=true;
  876. end;
  877. end;
  878. hp:=ttypeconvnode(hp).left;
  879. end;
  880. vecn :
  881. begin
  882. gotvec:=true;
  883. { accesses to dyn. arrays override read only access in delphi }
  884. if (m_delphi in aktmodeswitches) and is_dynamic_array(tunarynode(hp).left.resulttype.def) then
  885. gotdynarray:=true;
  886. hp:=tunarynode(hp).left;
  887. end;
  888. asn :
  889. begin
  890. { asn can't be assigned directly, it returns the value in a register instead
  891. of reference. }
  892. if not(gotsubscript or gotderef or gotvec) then
  893. begin
  894. CGMessagePos(hp.fileinfo,errmsg);
  895. exit;
  896. end;
  897. hp:=tunarynode(hp).left;
  898. end;
  899. subscriptn :
  900. begin
  901. gotsubscript:=true;
  902. { loop counter? }
  903. if not(Valid_Const in opts) and
  904. (vo_is_loop_counter in tsubscriptnode(hp).vs.varoptions) then
  905. CGMessage1(parser_e_illegal_assignment_to_count_var,tsubscriptnode(hp).vs.realname);
  906. { a class/interface access is an implicit }
  907. { dereferencing }
  908. hp:=tsubscriptnode(hp).left;
  909. if is_class_or_interface(hp.resulttype.def) then
  910. gotderef:=true;
  911. end;
  912. muln,
  913. divn,
  914. andn,
  915. xorn,
  916. orn,
  917. notn,
  918. subn,
  919. addn :
  920. begin
  921. { Allow operators on a pointer, or an integer
  922. and a pointer typecast and deref has been found }
  923. if ((hp.resulttype.def.deftype=pointerdef) or
  924. (is_integer(hp.resulttype.def) and gotpointer)) and
  925. gotderef then
  926. result:=true
  927. else
  928. { Temp strings are stored in memory, for compatibility with
  929. delphi only }
  930. if (m_delphi in aktmodeswitches) and
  931. (valid_addr in opts) and
  932. (hp.resulttype.def.deftype=stringdef) then
  933. result:=true
  934. else
  935. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  936. exit;
  937. end;
  938. niln,
  939. pointerconstn :
  940. begin
  941. { to support e.g. @tmypointer(0)^.data; see tests/tbs/tb0481 }
  942. if gotderef then
  943. result:=true
  944. else
  945. CGMessagePos(hp.fileinfo,type_e_no_assign_to_addr);
  946. exit;
  947. end;
  948. addrn :
  949. begin
  950. if gotderef then
  951. result:=true
  952. else
  953. CGMessagePos(hp.fileinfo,type_e_no_assign_to_addr);
  954. exit;
  955. end;
  956. calln :
  957. begin
  958. { check return type }
  959. case hp.resulttype.def.deftype of
  960. arraydef :
  961. begin
  962. { dynamic arrays are allowed when there is also a
  963. vec node }
  964. if is_dynamic_array(hp.resulttype.def) and
  965. gotvec then
  966. begin
  967. gotderef:=true;
  968. gotpointer:=true;
  969. end;
  970. end;
  971. pointerdef :
  972. gotpointer:=true;
  973. objectdef :
  974. gotclass:=is_class_or_interface(hp.resulttype.def);
  975. recorddef, { handle record like class it needs a subscription }
  976. classrefdef :
  977. gotclass:=true;
  978. end;
  979. { 1. if it returns a pointer and we've found a deref,
  980. 2. if it returns a class or record and a subscription or with is found }
  981. if (gotpointer and gotderef) or
  982. (gotclass and (gotsubscript or gotwith)) then
  983. result:=true
  984. else
  985. { Temp strings are stored in memory, for compatibility with
  986. delphi only }
  987. if (m_delphi in aktmodeswitches) and
  988. (valid_addr in opts) and
  989. (hp.resulttype.def.deftype=stringdef) then
  990. result:=true
  991. else
  992. CGMessagePos(hp.fileinfo,errmsg);
  993. exit;
  994. end;
  995. inlinen :
  996. begin
  997. if (valid_const in opts) and
  998. (tinlinenode(hp).inlinenumber in [in_typeof_x]) then
  999. result:=true
  1000. else
  1001. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  1002. exit;
  1003. end;
  1004. loadn :
  1005. begin
  1006. case tloadnode(hp).symtableentry.typ of
  1007. absolutevarsym,
  1008. globalvarsym,
  1009. localvarsym,
  1010. paravarsym :
  1011. begin
  1012. { loop counter? }
  1013. if not(Valid_Const in opts) and
  1014. (vo_is_loop_counter in tabstractvarsym(tloadnode(hp).symtableentry).varoptions) then
  1015. CGMessage1(parser_e_illegal_assignment_to_count_var,tloadnode(hp).symtableentry.realname);
  1016. { derefed pointer }
  1017. if (tabstractvarsym(tloadnode(hp).symtableentry).varspez=vs_const) then
  1018. begin
  1019. { allow p^:= constructions with p is const parameter }
  1020. if gotderef or gotdynarray or (Valid_Const in opts) then
  1021. result:=true
  1022. else
  1023. CGMessagePos(tloadnode(hp).fileinfo,type_e_no_assign_to_const);
  1024. exit;
  1025. end;
  1026. { Are we at a with symtable, then we need to process the
  1027. withrefnode also to check for maybe a const load }
  1028. if (tloadnode(hp).symtable.symtabletype=withsymtable) then
  1029. begin
  1030. { continue with processing the withref node }
  1031. hp:=tnode(twithsymtable(tloadnode(hp).symtable).withrefnode);
  1032. gotwith:=true;
  1033. end
  1034. else
  1035. begin
  1036. result:=true;
  1037. exit;
  1038. end;
  1039. end;
  1040. typedconstsym :
  1041. begin
  1042. if ttypedconstsym(tloadnode(hp).symtableentry).is_writable then
  1043. result:=true
  1044. else
  1045. CGMessagePos(hp.fileinfo,type_e_no_assign_to_const);
  1046. exit;
  1047. end;
  1048. procsym :
  1049. begin
  1050. if (Valid_Const in opts) then
  1051. result:=true
  1052. else
  1053. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  1054. exit;
  1055. end;
  1056. labelsym :
  1057. begin
  1058. if (Valid_Addr in opts) then
  1059. result:=true
  1060. else
  1061. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  1062. exit;
  1063. end;
  1064. constsym:
  1065. begin
  1066. if (tconstsym(tloadnode(hp).symtableentry).consttyp=constresourcestring) and
  1067. (valid_addr in opts) then
  1068. result:=true
  1069. else
  1070. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  1071. exit;
  1072. end;
  1073. else
  1074. begin
  1075. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  1076. exit;
  1077. end;
  1078. end;
  1079. end;
  1080. else
  1081. begin
  1082. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  1083. exit;
  1084. end;
  1085. end;
  1086. end;
  1087. end;
  1088. function valid_for_var(p:tnode):boolean;
  1089. begin
  1090. valid_for_var:=valid_for_assign(p,[]);
  1091. end;
  1092. function valid_for_formal_var(p : tnode) : boolean;
  1093. begin
  1094. valid_for_formal_var:=valid_for_assign(p,[valid_void]);
  1095. end;
  1096. function valid_for_formal_const(p : tnode) : boolean;
  1097. begin
  1098. valid_for_formal_const:=(p.resulttype.def.deftype=formaldef) or
  1099. valid_for_assign(p,[valid_void,valid_const,valid_property]);
  1100. end;
  1101. function valid_for_assignment(p:tnode):boolean;
  1102. begin
  1103. valid_for_assignment:=valid_for_assign(p,[valid_property]);
  1104. end;
  1105. function valid_for_addr(p : tnode) : boolean;
  1106. begin
  1107. result:=valid_for_assign(p,[valid_const,valid_addr,valid_void]);
  1108. end;
  1109. procedure var_para_allowed(var eq:tequaltype;def_from,def_to:Tdef);
  1110. begin
  1111. { Note: eq must be already valid, it will only be updated! }
  1112. case def_to.deftype of
  1113. formaldef :
  1114. begin
  1115. { all types can be passed to a formaldef }
  1116. eq:=te_equal;
  1117. end;
  1118. orddef :
  1119. begin
  1120. { allows conversion from word to integer and
  1121. byte to shortint, but only for TP7 compatibility }
  1122. if (m_tp7 in aktmodeswitches) and
  1123. (def_from.deftype=orddef) and
  1124. (def_from.size=def_to.size) then
  1125. eq:=te_convert_l1;
  1126. end;
  1127. arraydef :
  1128. begin
  1129. if is_open_array(def_to) and
  1130. is_dynamic_array(def_from) and
  1131. equal_defs(tarraydef(def_from).elementtype.def,tarraydef(def_to).elementtype.def) then
  1132. eq:=te_convert_l2;
  1133. end;
  1134. pointerdef :
  1135. begin
  1136. { an implicit pointer conversion is allowed }
  1137. if (def_from.deftype=pointerdef) then
  1138. eq:=te_convert_l1;
  1139. end;
  1140. stringdef :
  1141. begin
  1142. { all shortstrings are allowed, size is not important }
  1143. if is_shortstring(def_from) and
  1144. is_shortstring(def_to) then
  1145. eq:=te_equal;
  1146. end;
  1147. objectdef :
  1148. begin
  1149. { child objects can be also passed }
  1150. { in non-delphi mode, otherwise }
  1151. { they must match exactly, except }
  1152. { if they are objects }
  1153. if (def_from.deftype=objectdef) and
  1154. (
  1155. not(m_delphi in aktmodeswitches) or
  1156. (
  1157. (tobjectdef(def_from).objecttype=odt_object) and
  1158. (tobjectdef(def_to).objecttype=odt_object)
  1159. )
  1160. ) and
  1161. (tobjectdef(def_from).is_related(tobjectdef(def_to))) then
  1162. eq:=te_convert_l1;
  1163. end;
  1164. filedef :
  1165. begin
  1166. { an implicit file conversion is also allowed }
  1167. { from a typed file to an untyped one }
  1168. if (def_from.deftype=filedef) and
  1169. (tfiledef(def_from).filetyp = ft_typed) and
  1170. (tfiledef(def_to).filetyp = ft_untyped) then
  1171. eq:=te_convert_l1;
  1172. end;
  1173. end;
  1174. end;
  1175. procedure para_allowed(var eq:tequaltype;p:tcallparanode;def_to:tdef);
  1176. begin
  1177. { Note: eq must be already valid, it will only be updated! }
  1178. case def_to.deftype of
  1179. formaldef :
  1180. begin
  1181. { all types can be passed to a formaldef }
  1182. eq:=te_equal;
  1183. end;
  1184. stringdef :
  1185. begin
  1186. { to support ansi/long/wide strings in a proper way }
  1187. { string and string[10] are assumed as equal }
  1188. { when searching the correct overloaded procedure }
  1189. if (p.resulttype.def.deftype=stringdef) and
  1190. (tstringdef(def_to).string_typ=tstringdef(p.resulttype.def).string_typ) then
  1191. eq:=te_equal
  1192. else
  1193. { Passing a constant char to ansistring or shortstring or
  1194. a widechar to widestring then handle it as equal. }
  1195. if (p.left.nodetype=ordconstn) and
  1196. (
  1197. is_char(p.resulttype.def) and
  1198. (is_shortstring(def_to) or is_ansistring(def_to))
  1199. ) or
  1200. (
  1201. is_widechar(p.resulttype.def) and
  1202. is_widestring(def_to)
  1203. ) then
  1204. eq:=te_equal
  1205. end;
  1206. setdef :
  1207. begin
  1208. { set can also be a not yet converted array constructor }
  1209. if (p.resulttype.def.deftype=arraydef) and
  1210. (tarraydef(p.resulttype.def).IsConstructor) and
  1211. not(tarraydef(p.resulttype.def).IsVariant) then
  1212. eq:=te_equal;
  1213. end;
  1214. procvardef :
  1215. begin
  1216. { in tp7 mode proc -> procvar is allowed }
  1217. if (m_tp_procvar in aktmodeswitches) and
  1218. (p.left.nodetype=calln) and
  1219. (proc_to_procvar_equal(tprocdef(tcallnode(p.left).procdefinition),tprocvardef(def_to),true)>=te_equal) then
  1220. eq:=te_equal;
  1221. end;
  1222. end;
  1223. end;
  1224. {****************************************************************************
  1225. TCallCandidates
  1226. ****************************************************************************}
  1227. constructor tcallcandidates.create(sym:tprocsym;st:tsymtable;ppn:tnode;isprop,ignorevis : boolean);
  1228. var
  1229. j : integer;
  1230. pd : tprocdef;
  1231. hp : pcandidate;
  1232. found,
  1233. has_overload_directive : boolean;
  1234. topclassh : tobjectdef;
  1235. srsymtable : tsymtable;
  1236. srprocsym : tprocsym;
  1237. pt : tcallparanode;
  1238. begin
  1239. if not assigned(sym) then
  1240. internalerror(200411015);
  1241. FProcSym:=sym;
  1242. FProcs:=nil;
  1243. FProccnt:=0;
  1244. FProcvisiblecnt:=0;
  1245. FParanode:=ppn;
  1246. FAllowVariant:=true;
  1247. { determine length of parameter list }
  1248. pt:=tcallparanode(ppn);
  1249. FParalength:=0;
  1250. while assigned(pt) do
  1251. begin
  1252. inc(FParalength);
  1253. pt:=tcallparanode(pt.right);
  1254. end;
  1255. { when the definition has overload directive set, we search for
  1256. overloaded definitions in the class, this only needs to be done once
  1257. for class entries as the tree keeps always the same }
  1258. if (not sym.overloadchecked) and
  1259. (sym.owner.symtabletype=objectsymtable) and
  1260. (po_overload in sym.first_procdef.procoptions) then
  1261. search_class_overloads(sym);
  1262. { when the class passed is defined in this unit we
  1263. need to use the scope of that class. This is a trick
  1264. that can be used to access protected members in other
  1265. units. At least kylix supports it this way (PFV) }
  1266. if assigned(st) and
  1267. (st.symtabletype=objectsymtable) and
  1268. (st.defowner.owner.symtabletype in [globalsymtable,staticsymtable]) and
  1269. st.defowner.owner.iscurrentunit then
  1270. topclassh:=tobjectdef(st.defowner)
  1271. else
  1272. begin
  1273. if assigned(current_procinfo) then
  1274. topclassh:=current_procinfo.procdef._class
  1275. else
  1276. topclassh:=nil;
  1277. end;
  1278. { link all procedures which have the same # of parameters }
  1279. for j:=1 to sym.procdef_count do
  1280. begin
  1281. pd:=sym.procdef[j];
  1282. { Is the procdef visible? This needs to be checked on
  1283. procdef level since a symbol can contain both private and
  1284. public declarations. But the check should not be done
  1285. when the callnode is generated by a property
  1286. inherited overrides invisible anonymous inherited (FK) }
  1287. if isprop or ignorevis or
  1288. (pd.owner.symtabletype<>objectsymtable) or
  1289. pd.is_visible_for_object(topclassh) then
  1290. begin
  1291. { we have at least one procedure that is visible }
  1292. inc(FProcvisiblecnt);
  1293. { only when the # of parameter are supported by the
  1294. procedure }
  1295. if (FParalength>=pd.minparacount) and
  1296. ((po_varargs in pd.procoptions) or { varargs }
  1297. (FParalength<=pd.maxparacount)) then
  1298. proc_add(pd);
  1299. end;
  1300. end;
  1301. { remember if the procedure is declared with the overload directive,
  1302. it's information is still needed also after all procs are removed }
  1303. has_overload_directive:=(po_overload in sym.first_procdef.procoptions);
  1304. { when the definition has overload directive set, we search for
  1305. overloaded definitions in the symtablestack. The found
  1306. entries are only added to the procs list and not the procsym, because
  1307. the list can change in every situation }
  1308. if has_overload_directive and
  1309. (sym.owner.symtabletype<>objectsymtable) then
  1310. begin
  1311. srsymtable:=sym.owner.next;
  1312. while assigned(srsymtable) do
  1313. begin
  1314. if srsymtable.symtabletype in [localsymtable,staticsymtable,globalsymtable] then
  1315. begin
  1316. srprocsym:=tprocsym(srsymtable.speedsearch(sym.name,sym.speedvalue));
  1317. if assigned(srprocsym) and
  1318. (srprocsym.typ=procsym) then
  1319. begin
  1320. { if this visible procedure doesn't have overload we can stop
  1321. searching }
  1322. if not(po_overload in srprocsym.first_procdef.procoptions) and
  1323. srprocsym.first_procdef.is_visible_for_object(topclassh) then
  1324. break;
  1325. { process all overloaded definitions }
  1326. for j:=1 to srprocsym.procdef_count do
  1327. begin
  1328. pd:=srprocsym.procdef[j];
  1329. { only visible procedures need to be added }
  1330. if pd.is_visible_for_object(topclassh) then
  1331. begin
  1332. { only when the # of parameter are supported by the
  1333. procedure }
  1334. if (FParalength>=pd.minparacount) and
  1335. ((po_varargs in pd.procoptions) or { varargs }
  1336. (FParalength<=pd.maxparacount)) then
  1337. begin
  1338. found:=false;
  1339. hp:=FProcs;
  1340. while assigned(hp) do
  1341. begin
  1342. { Only compare visible parameters for the user }
  1343. if compare_paras(hp^.data.paras,pd.paras,cp_value_equal_const,[cpo_ignorehidden])>=te_equal then
  1344. begin
  1345. found:=true;
  1346. break;
  1347. end;
  1348. hp:=hp^.next;
  1349. end;
  1350. if not found then
  1351. proc_add(pd);
  1352. end;
  1353. end;
  1354. end;
  1355. end;
  1356. end;
  1357. srsymtable:=srsymtable.next;
  1358. end;
  1359. end;
  1360. end;
  1361. constructor tcallcandidates.create_operator(op:ttoken;ppn:tnode);
  1362. var
  1363. j : integer;
  1364. pd : tprocdef;
  1365. hp : pcandidate;
  1366. found : boolean;
  1367. srsymtable : tsymtable;
  1368. srprocsym : tprocsym;
  1369. pt : tcallparanode;
  1370. sv : cardinal;
  1371. begin
  1372. FProcSym:=nil;
  1373. FProcs:=nil;
  1374. FProccnt:=0;
  1375. FProcvisiblecnt:=0;
  1376. FParanode:=ppn;
  1377. FAllowVariant:=false;
  1378. { determine length of parameter list }
  1379. pt:=tcallparanode(ppn);
  1380. FParalength:=0;
  1381. while assigned(pt) do
  1382. begin
  1383. if pt.resulttype.def.deftype=variantdef then
  1384. FAllowVariant:=true;
  1385. inc(FParalength);
  1386. pt:=tcallparanode(pt.right);
  1387. end;
  1388. { we search all overloaded operator definitions in the symtablestack. The found
  1389. entries are only added to the procs list and not the procsym, because
  1390. the list can change in every situation }
  1391. sv:=getspeedvalue(overloaded_names[op]);
  1392. srsymtable:=symtablestack;
  1393. while assigned(srsymtable) do
  1394. begin
  1395. if srsymtable.symtabletype in [localsymtable,staticsymtable,globalsymtable] then
  1396. begin
  1397. srprocsym:=tprocsym(srsymtable.speedsearch(overloaded_names[op],sv));
  1398. if assigned(srprocsym) and
  1399. (srprocsym.typ=procsym) then
  1400. begin
  1401. { Store first procsym found }
  1402. if not assigned(FProcsym) then
  1403. FProcsym:=srprocsym;
  1404. { process all overloaded definitions }
  1405. for j:=1 to srprocsym.procdef_count do
  1406. begin
  1407. pd:=srprocsym.procdef[j];
  1408. { only when the # of parameter are supported by the
  1409. procedure }
  1410. if (FParalength>=pd.minparacount) and
  1411. (FParalength<=pd.maxparacount) then
  1412. begin
  1413. found:=false;
  1414. hp:=FProcs;
  1415. while assigned(hp) do
  1416. begin
  1417. { Only compare visible parameters for the user }
  1418. if compare_paras(hp^.data.paras,pd.paras,cp_value_equal_const,[cpo_ignorehidden])>=te_equal then
  1419. begin
  1420. found:=true;
  1421. break;
  1422. end;
  1423. hp:=hp^.next;
  1424. end;
  1425. if not found then
  1426. proc_add(pd);
  1427. end;
  1428. end;
  1429. end;
  1430. end;
  1431. srsymtable:=srsymtable.next;
  1432. end;
  1433. end;
  1434. destructor tcallcandidates.destroy;
  1435. var
  1436. hpnext,
  1437. hp : pcandidate;
  1438. begin
  1439. hp:=FProcs;
  1440. while assigned(hp) do
  1441. begin
  1442. hpnext:=hp^.next;
  1443. dispose(hp);
  1444. hp:=hpnext;
  1445. end;
  1446. end;
  1447. function tcallcandidates.proc_add(pd:tprocdef):pcandidate;
  1448. var
  1449. defaultparacnt : integer;
  1450. begin
  1451. { generate new candidate entry }
  1452. new(result);
  1453. fillchar(result^,sizeof(tcandidate),0);
  1454. result^.data:=pd;
  1455. result^.next:=FProcs;
  1456. FProcs:=result;
  1457. inc(FProccnt);
  1458. { Find last parameter, skip all default parameters
  1459. that are not passed. Ignore this skipping for varargs }
  1460. result^.firstparaidx:=pd.paras.count-1;
  1461. if not(po_varargs in pd.procoptions) then
  1462. begin
  1463. { ignore hidden parameters }
  1464. while (result^.firstparaidx>=0) and (vo_is_hidden_para in tparavarsym(pd.paras[result^.firstparaidx]).varoptions) do
  1465. dec(result^.firstparaidx);
  1466. defaultparacnt:=pd.maxparacount-FParalength;
  1467. if defaultparacnt>0 then
  1468. begin
  1469. if defaultparacnt>result^.firstparaidx+1 then
  1470. internalerror(200401141);
  1471. dec(result^.firstparaidx,defaultparacnt);
  1472. end;
  1473. end;
  1474. end;
  1475. procedure tcallcandidates.list(all:boolean);
  1476. var
  1477. hp : pcandidate;
  1478. begin
  1479. hp:=FProcs;
  1480. while assigned(hp) do
  1481. begin
  1482. if all or
  1483. (not hp^.invalid) then
  1484. MessagePos1(hp^.data.fileinfo,sym_h_param_list,hp^.data.fullprocname(false));
  1485. hp:=hp^.next;
  1486. end;
  1487. end;
  1488. {$ifdef EXTDEBUG}
  1489. procedure tcallcandidates.dump_info(lvl:longint);
  1490. function ParaTreeStr(p:tcallparanode):string;
  1491. begin
  1492. result:='';
  1493. while assigned(p) do
  1494. begin
  1495. if result<>'' then
  1496. result:=result+',';
  1497. result:=result+p.resulttype.def.typename;
  1498. p:=tcallparanode(p.right);
  1499. end;
  1500. end;
  1501. var
  1502. hp : pcandidate;
  1503. i : integer;
  1504. currpara : tparavarsym;
  1505. begin
  1506. if not CheckVerbosity(lvl) then
  1507. exit;
  1508. Comment(lvl+V_LineInfo,'Overloaded callnode: '+FProcSym.name+'('+ParaTreeStr(tcallparanode(FParaNode))+')');
  1509. hp:=FProcs;
  1510. while assigned(hp) do
  1511. begin
  1512. Comment(lvl,' '+hp^.data.fullprocname(false));
  1513. if (hp^.invalid) then
  1514. Comment(lvl,' invalid')
  1515. else
  1516. begin
  1517. Comment(lvl,' ex: '+tostr(hp^.exact_count)+
  1518. ' eq: '+tostr(hp^.equal_count)+
  1519. ' l1: '+tostr(hp^.cl1_count)+
  1520. ' l2: '+tostr(hp^.cl2_count)+
  1521. ' l3: '+tostr(hp^.cl3_count)+
  1522. ' oper: '+tostr(hp^.coper_count)+
  1523. ' ord: '+realtostr(hp^.ordinal_distance));
  1524. { Print parameters in left-right order }
  1525. for i:=0 to hp^.data.paras.count-1 do
  1526. begin
  1527. currpara:=tparavarsym(hp^.data.paras[i]);
  1528. if (vo_is_hidden_para in currpara.varoptions) then
  1529. Comment(lvl,' - '+currpara.vartype.def.typename+' : '+EqualTypeName[currpara.eqval]);
  1530. end;
  1531. end;
  1532. hp:=hp^.next;
  1533. end;
  1534. end;
  1535. {$endif EXTDEBUG}
  1536. procedure tcallcandidates.get_information;
  1537. var
  1538. hp : pcandidate;
  1539. currpara : tparavarsym;
  1540. paraidx : integer;
  1541. currparanr : byte;
  1542. rfh,rth : bestreal;
  1543. def_from,
  1544. def_to : tdef;
  1545. currpt,
  1546. pt : tcallparanode;
  1547. eq : tequaltype;
  1548. convtype : tconverttype;
  1549. pdoper : tprocdef;
  1550. releasecurrpt : boolean;
  1551. cdoptions : tcompare_defs_options;
  1552. begin
  1553. cdoptions:=[cdo_check_operator];
  1554. if FAllowVariant then
  1555. include(cdoptions,cdo_allow_variant);
  1556. { process all procs }
  1557. hp:=FProcs;
  1558. while assigned(hp) do
  1559. begin
  1560. { We compare parameters in reverse order (right to left),
  1561. the firstpara is already pointing to the last parameter
  1562. were we need to start comparing }
  1563. currparanr:=FParalength;
  1564. paraidx:=hp^.firstparaidx;
  1565. while (paraidx>=0) and (vo_is_hidden_para in tparavarsym(hp^.data.paras[paraidx]).varoptions) do
  1566. dec(paraidx);
  1567. pt:=tcallparanode(FParaNode);
  1568. while assigned(pt) and (paraidx>=0) do
  1569. begin
  1570. currpara:=tparavarsym(hp^.data.paras[paraidx]);
  1571. { currpt can be changed from loadn to calln when a procvar
  1572. is passed. This is to prevent that the change is permanent }
  1573. currpt:=pt;
  1574. releasecurrpt:=false;
  1575. { retrieve current parameter definitions to compares }
  1576. eq:=te_incompatible;
  1577. def_from:=currpt.resulttype.def;
  1578. def_to:=currpara.vartype.def;
  1579. if not(assigned(def_from)) then
  1580. internalerror(200212091);
  1581. if not(
  1582. assigned(def_to) or
  1583. ((po_varargs in hp^.data.procoptions) and
  1584. (currparanr>hp^.data.minparacount))
  1585. ) then
  1586. internalerror(200212092);
  1587. { Convert tp procvars when not expecting a procvar }
  1588. if (def_to.deftype<>procvardef) and
  1589. (currpt.left.resulttype.def.deftype=procvardef) then
  1590. begin
  1591. releasecurrpt:=true;
  1592. currpt:=tcallparanode(pt.getcopy);
  1593. if maybe_call_procvar(currpt.left,true) then
  1594. begin
  1595. currpt.resulttype:=currpt.left.resulttype;
  1596. def_from:=currpt.left.resulttype.def;
  1597. end;
  1598. end;
  1599. { varargs are always equal, but not exact }
  1600. if (po_varargs in hp^.data.procoptions) and
  1601. (currparanr>hp^.data.minparacount) then
  1602. begin
  1603. eq:=te_equal;
  1604. end
  1605. else
  1606. { same definition -> exact }
  1607. if (def_from=def_to) then
  1608. begin
  1609. eq:=te_exact;
  1610. end
  1611. else
  1612. { for value and const parameters check if a integer is constant or
  1613. included in other integer -> equal and calc ordinal_distance }
  1614. if not(currpara.varspez in [vs_var,vs_out]) and
  1615. is_integer(def_from) and
  1616. is_integer(def_to) and
  1617. is_in_limit(def_from,def_to) then
  1618. begin
  1619. eq:=te_equal;
  1620. hp^.ordinal_distance:=hp^.ordinal_distance+
  1621. abs(bestreal(torddef(def_from).low)-bestreal(torddef(def_to).low));
  1622. if (torddef(def_to).typ=u64bit) then
  1623. rth:=bestreal(qword(torddef(def_to).high))
  1624. else
  1625. rth:=bestreal(torddef(def_to).high);
  1626. if (torddef(def_from).typ=u64bit) then
  1627. rfh:=bestreal(qword(torddef(def_from).high))
  1628. else
  1629. rfh:=bestreal(torddef(def_from).high);
  1630. hp^.ordinal_distance:=hp^.ordinal_distance+abs(rth-rfh);
  1631. { Give wrong sign a small penalty, this is need to get a diffrence
  1632. from word->[longword,longint] }
  1633. if is_signed(def_from)<>is_signed(def_to) then
  1634. hp^.ordinal_distance:=hp^.ordinal_distance+1.0;
  1635. end
  1636. else
  1637. { generic type comparision }
  1638. begin
  1639. eq:=compare_defs_ext(def_from,def_to,currpt.left.nodetype,convtype,pdoper,cdoptions);
  1640. { when the types are not equal we need to check
  1641. some special case for parameter passing }
  1642. if (eq<te_equal) then
  1643. begin
  1644. if currpara.varspez in [vs_var,vs_out] then
  1645. begin
  1646. { para requires an equal type so the previous found
  1647. match was not good enough, reset to incompatible }
  1648. eq:=te_incompatible;
  1649. { var_para_allowed will return te_equal and te_convert_l1 to
  1650. make a difference for best matching }
  1651. var_para_allowed(eq,currpt.resulttype.def,currpara.vartype.def)
  1652. end
  1653. else
  1654. para_allowed(eq,currpt,def_to);
  1655. end;
  1656. end;
  1657. { when a procvar was changed to a call an exact much is
  1658. downgraded to equal. This way an overload call with the
  1659. procvar is choosen. See tb0471 (PFV) }
  1660. if (pt<>currpt) and (eq=te_exact) then
  1661. eq:=te_equal;
  1662. { increase correct counter }
  1663. case eq of
  1664. te_exact :
  1665. inc(hp^.exact_count);
  1666. te_equal :
  1667. inc(hp^.equal_count);
  1668. te_convert_l1 :
  1669. inc(hp^.cl1_count);
  1670. te_convert_l2 :
  1671. inc(hp^.cl2_count);
  1672. te_convert_l3 :
  1673. inc(hp^.cl3_count);
  1674. te_convert_operator :
  1675. inc(hp^.coper_count);
  1676. te_incompatible :
  1677. hp^.invalid:=true;
  1678. else
  1679. internalerror(200212072);
  1680. end;
  1681. { stop checking when an incompatible parameter is found }
  1682. if hp^.invalid then
  1683. begin
  1684. { store the current parameter info for
  1685. a nice error message when no procedure is found }
  1686. hp^.wrongparaidx:=paraidx;
  1687. hp^.wrongparanr:=currparanr;
  1688. break;
  1689. end;
  1690. {$ifdef EXTDEBUG}
  1691. { store equal in node tree for dump }
  1692. currpara.eqval:=eq;
  1693. {$endif EXTDEBUG}
  1694. { maybe release temp currpt }
  1695. if releasecurrpt then
  1696. currpt.free;
  1697. { next parameter in the call tree }
  1698. pt:=tcallparanode(pt.right);
  1699. { next parameter for definition, only goto next para
  1700. if we're out of the varargs }
  1701. if not(po_varargs in hp^.data.procoptions) or
  1702. (currparanr<=hp^.data.maxparacount) then
  1703. begin
  1704. { Ignore vs_hidden parameters }
  1705. repeat
  1706. dec(paraidx);
  1707. until (paraidx<0) or not(vo_is_hidden_para in tparavarsym(hp^.data.paras[paraidx]).varoptions);
  1708. end;
  1709. dec(currparanr);
  1710. end;
  1711. if not(hp^.invalid) and
  1712. (assigned(pt) or (paraidx>=0) or (currparanr<>0)) then
  1713. internalerror(200212141);
  1714. { next candidate }
  1715. hp:=hp^.next;
  1716. end;
  1717. end;
  1718. function is_better_candidate(currpd,bestpd:pcandidate):integer;
  1719. var
  1720. res : integer;
  1721. begin
  1722. {
  1723. Return values:
  1724. > 0 when currpd is better than bestpd
  1725. < 0 when bestpd is better than currpd
  1726. = 0 when both are equal
  1727. To choose the best candidate we use the following order:
  1728. - Incompatible flag
  1729. - (Smaller) Number of convert operator parameters.
  1730. - (Smaller) Number of convertlevel 2 parameters.
  1731. - (Smaller) Number of convertlevel 1 parameters.
  1732. - (Bigger) Number of exact parameters.
  1733. - (Smaller) Number of equal parameters.
  1734. - (Smaller) Total of ordinal distance. For example, the distance of a word
  1735. to a byte is 65535-255=65280.
  1736. }
  1737. if bestpd^.invalid then
  1738. begin
  1739. if currpd^.invalid then
  1740. res:=0
  1741. else
  1742. res:=1;
  1743. end
  1744. else
  1745. if currpd^.invalid then
  1746. res:=-1
  1747. else
  1748. begin
  1749. { less operator parameters? }
  1750. res:=(bestpd^.coper_count-currpd^.coper_count);
  1751. if (res=0) then
  1752. begin
  1753. { less cl3 parameters? }
  1754. res:=(bestpd^.cl3_count-currpd^.cl3_count);
  1755. if (res=0) then
  1756. begin
  1757. { less cl2 parameters? }
  1758. res:=(bestpd^.cl2_count-currpd^.cl2_count);
  1759. if (res=0) then
  1760. begin
  1761. { less cl1 parameters? }
  1762. res:=(bestpd^.cl1_count-currpd^.cl1_count);
  1763. if (res=0) then
  1764. begin
  1765. { more exact parameters? }
  1766. res:=(currpd^.exact_count-bestpd^.exact_count);
  1767. if (res=0) then
  1768. begin
  1769. { less equal parameters? }
  1770. res:=(bestpd^.equal_count-currpd^.equal_count);
  1771. if (res=0) then
  1772. begin
  1773. { smaller ordinal distance? }
  1774. if (currpd^.ordinal_distance<bestpd^.ordinal_distance) then
  1775. res:=1
  1776. else
  1777. if (currpd^.ordinal_distance>bestpd^.ordinal_distance) then
  1778. res:=-1
  1779. else
  1780. res:=0;
  1781. end;
  1782. end;
  1783. end;
  1784. end;
  1785. end;
  1786. end;
  1787. end;
  1788. is_better_candidate:=res;
  1789. end;
  1790. function tcallcandidates.choose_best(var bestpd:tabstractprocdef):integer;
  1791. var
  1792. besthpstart,
  1793. hp : pcandidate;
  1794. cntpd,
  1795. res : integer;
  1796. begin
  1797. {
  1798. Returns the number of candidates left and the
  1799. first candidate is returned in pdbest
  1800. }
  1801. { Setup the first procdef as best, only count it as a result
  1802. when it is valid }
  1803. bestpd:=FProcs^.data;
  1804. if FProcs^.invalid then
  1805. cntpd:=0
  1806. else
  1807. cntpd:=1;
  1808. if assigned(FProcs^.next) then
  1809. begin
  1810. besthpstart:=FProcs;
  1811. hp:=FProcs^.next;
  1812. while assigned(hp) do
  1813. begin
  1814. res:=is_better_candidate(hp,besthpstart);
  1815. if (res>0) then
  1816. begin
  1817. { hp is better, flag all procs to be incompatible }
  1818. while (besthpstart<>hp) do
  1819. begin
  1820. besthpstart^.invalid:=true;
  1821. besthpstart:=besthpstart^.next;
  1822. end;
  1823. { besthpstart is already set to hp }
  1824. bestpd:=besthpstart^.data;
  1825. cntpd:=1;
  1826. end
  1827. else
  1828. if (res<0) then
  1829. begin
  1830. { besthpstart is better, flag current hp to be incompatible }
  1831. hp^.invalid:=true;
  1832. end
  1833. else
  1834. begin
  1835. { res=0, both are valid }
  1836. if not hp^.invalid then
  1837. inc(cntpd);
  1838. end;
  1839. hp:=hp^.next;
  1840. end;
  1841. end;
  1842. result:=cntpd;
  1843. end;
  1844. procedure tcallcandidates.find_wrong_para;
  1845. var
  1846. currparanr : smallint;
  1847. hp : pcandidate;
  1848. pt : tcallparanode;
  1849. wrongpara : tparavarsym;
  1850. begin
  1851. { Only process the first overloaded procdef }
  1852. hp:=FProcs;
  1853. { Find callparanode corresponding to the argument }
  1854. pt:=tcallparanode(FParanode);
  1855. currparanr:=FParalength;
  1856. while assigned(pt) and
  1857. (currparanr>hp^.wrongparanr) do
  1858. begin
  1859. pt:=tcallparanode(pt.right);
  1860. dec(currparanr);
  1861. end;
  1862. if (currparanr<>hp^.wrongparanr) or
  1863. not assigned(pt) then
  1864. internalerror(200212094);
  1865. { Show error message, when it was a var or out parameter
  1866. guess that it is a missing typeconv }
  1867. wrongpara:=tparavarsym(hp^.data.paras[hp^.wrongparaidx]);
  1868. if wrongpara.varspez in [vs_var,vs_out] then
  1869. begin
  1870. { Maybe passing the correct type but passing a const to var parameter }
  1871. if (compare_defs(pt.resulttype.def,wrongpara.vartype.def,pt.nodetype)<>te_incompatible) and
  1872. not valid_for_var(pt.left) then
  1873. CGMessagePos(pt.left.fileinfo,type_e_variable_id_expected)
  1874. else
  1875. CGMessagePos2(pt.left.fileinfo,parser_e_call_by_ref_without_typeconv,
  1876. FullTypeName(pt.left.resulttype.def,wrongpara.vartype.def),
  1877. FullTypeName(wrongpara.vartype.def,pt.left.resulttype.def))
  1878. end
  1879. else
  1880. CGMessagePos3(pt.left.fileinfo,type_e_wrong_parameter_type,tostr(hp^.wrongparanr),
  1881. FullTypeName(pt.left.resulttype.def,wrongpara.vartype.def),
  1882. FullTypeName(wrongpara.vartype.def,pt.left.resulttype.def));
  1883. end;
  1884. end.
  1885. {
  1886. $Log$
  1887. Revision 1.118 2005-02-20 13:12:22 peter
  1888. * allow assignment to elements of constant dyn array in delphi mode
  1889. Revision 1.117 2005/02/14 17:13:06 peter
  1890. * truncate log
  1891. Revision 1.116 2005/02/14 16:45:00 peter
  1892. * allow more operations on integers with a typecast to pointer
  1893. Revision 1.115 2005/02/13 20:33:57 peter
  1894. * allow nil^ passed to var parameter
  1895. Revision 1.114 2005/02/02 22:16:39 florian
  1896. * delphi assumes dyn. array access make expressions l-values because it's internally a pointer
  1897. Revision 1.113 2005/02/01 22:50:50 florian
  1898. * inherited; works now in delphi mode for private methods; looks like a delphi bug
  1899. Revision 1.112 2005/01/25 18:49:45 peter
  1900. * fix overload choosing with an qword overload
  1901. * allow to get the address of string temps in delphi mode
  1902. Revision 1.111 2005/01/19 23:23:12 florian
  1903. * taking the address of a resourcestring is allowed now
  1904. Revision 1.110 2005/01/19 22:19:41 peter
  1905. * unit mapping rewrite
  1906. * new derefmap added
  1907. Revision 1.109 2005/01/19 20:53:27 florian
  1908. * tmypointer(12435)^ is an l-value
  1909. Revision 1.108 2005/01/10 22:10:26 peter
  1910. * widestring patches from Alexey Barkovoy
  1911. Revision 1.107 2005/01/07 16:22:47 peter
  1912. * handle string-open array of (wide)char without variants
  1913. }