htypechk.pas 76 KB

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