htypechk.pas 81 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. This unit exports some help routines for the type checking
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit htypechk;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. tokens,cpuinfo,
  22. node,
  23. symconst,symtype,symdef,symsym,symbase;
  24. type
  25. Ttok2nodeRec=record
  26. tok : ttoken;
  27. nod : tnodetype;
  28. op_overloading_supported : boolean;
  29. end;
  30. pcandidate = ^tcandidate;
  31. tcandidate = record
  32. next : pcandidate;
  33. data : tprocdef;
  34. wrongparaidx,
  35. firstparaidx : integer;
  36. exact_count,
  37. equal_count,
  38. cl1_count,
  39. cl2_count,
  40. cl3_count,
  41. coper_count : integer; { should be signed }
  42. ordinal_distance : bestreal;
  43. invalid : boolean;
  44. wrongparanr : byte;
  45. end;
  46. tcallcandidates = class
  47. private
  48. FProcSym : tprocsym;
  49. FProcs : pcandidate;
  50. FProcVisibleCnt,
  51. FProcCnt : integer;
  52. FParaNode : tnode;
  53. FParaLength : smallint;
  54. FAllowVariant : boolean;
  55. function proc_add(pd:tprocdef):pcandidate;
  56. public
  57. constructor create(sym:tprocsym;st:tsymtable;ppn:tnode;isprop,ignorevis : boolean);
  58. constructor create_operator(op:ttoken;ppn:tnode);
  59. destructor destroy;override;
  60. procedure list(all:boolean);
  61. {$ifdef EXTDEBUG}
  62. procedure dump_info(lvl:longint);
  63. {$endif EXTDEBUG}
  64. procedure get_information;
  65. function choose_best(var bestpd:tabstractprocdef):integer;
  66. procedure find_wrong_para;
  67. property Count:integer read FProcCnt;
  68. property VisibleCount:integer read FProcVisibleCnt;
  69. end;
  70. const
  71. tok2nodes=25;
  72. tok2node:array[1..tok2nodes] of ttok2noderec=(
  73. (tok:_PLUS ;nod:addn;op_overloading_supported:true), { binary overloading supported }
  74. (tok:_MINUS ;nod:subn;op_overloading_supported:true), { binary and unary overloading supported }
  75. (tok:_STAR ;nod:muln;op_overloading_supported:true), { binary overloading supported }
  76. (tok:_SLASH ;nod:slashn;op_overloading_supported:true), { binary overloading supported }
  77. (tok:_EQUAL ;nod:equaln;op_overloading_supported:true), { binary overloading supported }
  78. (tok:_GT ;nod:gtn;op_overloading_supported:true), { binary overloading supported }
  79. (tok:_LT ;nod:ltn;op_overloading_supported:true), { binary overloading supported }
  80. (tok:_GTE ;nod:gten;op_overloading_supported:true), { binary overloading supported }
  81. (tok:_LTE ;nod:lten;op_overloading_supported:true), { binary overloading supported }
  82. (tok:_SYMDIF ;nod:symdifn;op_overloading_supported:true), { binary overloading supported }
  83. (tok:_STARSTAR;nod:starstarn;op_overloading_supported:true), { binary overloading supported }
  84. (tok:_OP_AS ;nod:asn;op_overloading_supported:false), { binary overloading NOT supported }
  85. (tok:_OP_IN ;nod:inn;op_overloading_supported:false), { binary overloading NOT supported }
  86. (tok:_OP_IS ;nod:isn;op_overloading_supported:false), { binary overloading NOT supported }
  87. (tok:_OP_OR ;nod:orn;op_overloading_supported:true), { binary overloading supported }
  88. (tok:_OP_AND ;nod:andn;op_overloading_supported:true), { binary overloading supported }
  89. (tok:_OP_DIV ;nod:divn;op_overloading_supported:true), { binary overloading supported }
  90. (tok:_OP_NOT ;nod:notn;op_overloading_supported:true), { unary overloading supported }
  91. (tok:_OP_MOD ;nod:modn;op_overloading_supported:true), { binary overloading supported }
  92. (tok:_OP_SHL ;nod:shln;op_overloading_supported:true), { binary overloading supported }
  93. (tok:_OP_SHR ;nod:shrn;op_overloading_supported:true), { binary overloading supported }
  94. (tok:_OP_XOR ;nod:xorn;op_overloading_supported:true), { binary overloading supported }
  95. (tok:_ASSIGNMENT;nod:assignn;op_overloading_supported:true), { unary overloading supported }
  96. (tok:_CARET ;nod:caretn;op_overloading_supported:false), { binary overloading NOT supported }
  97. (tok:_UNEQUAL ;nod:unequaln;op_overloading_supported:false) { binary overloading NOT supported overload = instead }
  98. );
  99. const
  100. { firstcallparan without varspez we don't count the ref }
  101. {$ifdef extdebug}
  102. count_ref : boolean = true;
  103. {$endif def extdebug}
  104. allow_array_constructor : boolean = false;
  105. function node2opstr(nt:tnodetype):string;
  106. { check operator args and result type }
  107. function isbinaryoperatoroverloadable(treetyp:tnodetype;ld:tdef;lt:tnodetype;rd:tdef;rt:tnodetype) : boolean;
  108. function isoperatoracceptable(pf : tprocdef; optoken : ttoken) : boolean;
  109. function isunaryoverloaded(var t : tnode) : boolean;
  110. function isbinaryoverloaded(var t : tnode) : boolean;
  111. { Register Allocation }
  112. procedure make_not_regable(p : tnode);
  113. procedure calcregisters(p : tbinarynode;r32,fpu,mmx : word);
  114. { procvar handling }
  115. function is_procvar_load(p:tnode):boolean;
  116. procedure test_local_to_procvar(from_def:tprocvardef;to_def:tdef);
  117. { sets varsym varstate field correctly }
  118. type
  119. tvarstateflag = (vsf_must_be_valid,vsf_use_hints);
  120. tvarstateflags = set of tvarstateflag;
  121. procedure set_varstate(p:tnode;newstate:tvarstate;varstateflags:tvarstateflags);
  122. { sets the callunique flag, if the node is a vecn, }
  123. { takes care of type casts etc. }
  124. procedure set_unique(p : tnode);
  125. function valid_for_formal_var(p : tnode; report_errors: boolean) : boolean;
  126. function valid_for_formal_const(p : tnode; report_errors: boolean) : boolean;
  127. function valid_for_var(p:tnode; report_errors: boolean):boolean;
  128. function valid_for_assignment(p:tnode; report_errors: boolean):boolean;
  129. function valid_for_addr(p : tnode; report_errors: boolean) : boolean;
  130. function allowenumop(nt:tnodetype):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]) or
  578. ((tloadnode(p).symtableentry.typ = paravarsym) and
  579. { not a nested variable }
  580. (assigned(tloadnode(p).left) or
  581. not(tparavarsym(tloadnode(p).symtableentry).varspez in [vs_var,vs_out]))) then
  582. tabstractvarsym(tloadnode(p).symtableentry).varregable:=vr_none;
  583. end;
  584. end;
  585. { calculates the needed registers for a binary operator }
  586. procedure calcregisters(p : tbinarynode;r32,fpu,mmx : word);
  587. begin
  588. p.left_right_max;
  589. { Only when the difference between the left and right registers < the
  590. wanted registers allocate the amount of registers }
  591. if assigned(p.left) then
  592. begin
  593. if assigned(p.right) then
  594. begin
  595. { the location must be already filled in because we need it to }
  596. { calculate the necessary number of registers (JM) }
  597. if p.expectloc = LOC_INVALID then
  598. internalerror(200110101);
  599. if (abs(p.left.registersint-p.right.registersint)<r32) or
  600. ((p.expectloc = LOC_FPUREGISTER) and
  601. (p.right.registersfpu <= p.left.registersfpu) and
  602. ((p.right.registersfpu <> 0) or (p.left.registersfpu <> 0)) and
  603. (p.left.registersint < p.right.registersint)) then
  604. inc(p.registersint,r32);
  605. if (abs(p.left.registersfpu-p.right.registersfpu)<fpu) then
  606. inc(p.registersfpu,fpu);
  607. {$ifdef SUPPORT_MMX}
  608. if (abs(p.left.registersmmx-p.right.registersmmx)<mmx) then
  609. inc(p.registersmmx,mmx);
  610. {$endif SUPPORT_MMX}
  611. { the following is a little bit guessing but I think }
  612. { it's the only way to solve same internalerrors: }
  613. { if the left and right node both uses registers }
  614. { and return a mem location, but the current node }
  615. { doesn't use an integer register we get probably }
  616. { trouble when restoring a node }
  617. if (p.left.registersint=p.right.registersint) and
  618. (p.registersint=p.left.registersint) and
  619. (p.registersint>0) and
  620. (p.left.expectloc in [LOC_REFERENCE,LOC_CREFERENCE]) and
  621. (p.right.expectloc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  622. inc(p.registersint);
  623. end
  624. else
  625. begin
  626. if (p.left.registersint<r32) then
  627. inc(p.registersint,r32);
  628. if (p.left.registersfpu<fpu) then
  629. inc(p.registersfpu,fpu);
  630. {$ifdef SUPPORT_MMX}
  631. if (p.left.registersmmx<mmx) then
  632. inc(p.registersmmx,mmx);
  633. {$endif SUPPORT_MMX}
  634. end;
  635. end;
  636. end;
  637. {****************************************************************************
  638. Subroutine Handling
  639. ****************************************************************************}
  640. function is_procvar_load(p:tnode):boolean;
  641. begin
  642. result:=false;
  643. { remove voidpointer typecast for tp procvars }
  644. if ((m_tp_procvar in aktmodeswitches) or
  645. (m_mac_procvar in aktmodeswitches)) and
  646. (p.nodetype=typeconvn) and
  647. is_voidpointer(p.resulttype.def) then
  648. p:=tunarynode(p).left;
  649. result:=(p.nodetype=typeconvn) and
  650. (ttypeconvnode(p).convtype=tc_proc_2_procvar);
  651. end;
  652. { local routines can't be assigned to procvars }
  653. procedure test_local_to_procvar(from_def:tprocvardef;to_def:tdef);
  654. begin
  655. if (from_def.parast.symtablelevel>normal_function_level) and
  656. (to_def.deftype=procvardef) then
  657. CGMessage(type_e_cannot_local_proc_to_procvar);
  658. end;
  659. procedure set_varstate(p:tnode;newstate:tvarstate;varstateflags:tvarstateflags);
  660. const
  661. vstrans: array[tvarstate,tvarstate] of tvarstate = (
  662. { vs_none -> ... }
  663. (vs_none,vs_declared,vs_initialised,vs_read,vs_written,vs_readwritten),
  664. { vs_declared -> ... }
  665. (vs_none,vs_declared,vs_initialised,vs_read,vs_written,vs_readwritten),
  666. { vs_initialised -> ... }
  667. (vs_none,vs_initialised,vs_initialised,vs_read,vs_written,vs_readwritten),
  668. { vs_read -> ... }
  669. (vs_none,vs_read,vs_read,vs_read,vs_readwritten,vs_readwritten),
  670. { vs_written -> ... }
  671. (vs_none,vs_written,vs_written,vs_readwritten,vs_written,vs_readwritten),
  672. { vs_readwritten -> ... }
  673. (vs_none,vs_readwritten,vs_readwritten,vs_readwritten,vs_readwritten,vs_readwritten));
  674. var
  675. hsym : tabstractvarsym;
  676. begin
  677. while assigned(p) do
  678. begin
  679. case p.nodetype of
  680. typeconvn :
  681. begin
  682. case ttypeconvnode(p).convtype of
  683. tc_cchar_2_pchar,
  684. tc_cstring_2_pchar,
  685. tc_array_2_pointer :
  686. exclude(varstateflags,vsf_must_be_valid);
  687. tc_pchar_2_string,
  688. tc_pointer_2_array :
  689. include(varstateflags,vsf_must_be_valid);
  690. end;
  691. p:=tunarynode(p).left;
  692. end;
  693. subscriptn :
  694. p:=tunarynode(p).left;
  695. vecn:
  696. begin
  697. set_varstate(tbinarynode(p).right,vs_read,[vsf_must_be_valid]);
  698. if (newstate in [vs_read,vs_readwritten]) or
  699. not(tunarynode(p).left.resulttype.def.deftype in [stringdef,arraydef]) then
  700. include(varstateflags,vsf_must_be_valid)
  701. else if (newstate = vs_written) then
  702. exclude(varstateflags,vsf_must_be_valid);
  703. p:=tunarynode(p).left;
  704. end;
  705. { do not parse calln }
  706. calln :
  707. break;
  708. loadn :
  709. begin
  710. if (tloadnode(p).symtableentry.typ in [localvarsym,paravarsym,globalvarsym]) then
  711. begin
  712. hsym:=tabstractvarsym(tloadnode(p).symtableentry);
  713. if (vsf_must_be_valid in varstateflags) and (hsym.varstate=vs_declared) then
  714. begin
  715. { Give warning/note for uninitialized locals }
  716. if assigned(hsym.owner) and
  717. not(vo_is_external in hsym.varoptions) and
  718. (hsym.owner.symtabletype in [parasymtable,localsymtable,staticsymtable]) and
  719. ((hsym.owner=current_procinfo.procdef.localst) or
  720. (vo_is_funcret in hsym.varoptions)) then
  721. begin
  722. if (vo_is_funcret in hsym.varoptions) then
  723. begin
  724. if (vsf_use_hints in varstateflags) then
  725. CGMessage(sym_h_function_result_uninitialized)
  726. else
  727. CGMessage(sym_w_function_result_uninitialized)
  728. end
  729. else
  730. begin
  731. if tloadnode(p).symtable.symtabletype=localsymtable then
  732. begin
  733. if (vsf_use_hints in varstateflags) then
  734. CGMessage1(sym_h_uninitialized_local_variable,hsym.realname)
  735. else
  736. CGMessage1(sym_w_uninitialized_local_variable,hsym.realname);
  737. end
  738. else
  739. begin
  740. if (vsf_use_hints in varstateflags) then
  741. CGMessage1(sym_h_uninitialized_variable,hsym.realname)
  742. else
  743. CGMessage1(sym_w_uninitialized_variable,hsym.realname);
  744. end;
  745. end;
  746. end;
  747. end;
  748. { don't override vs_readwritten with vs_initialised }
  749. hsym.varstate := vstrans[hsym.varstate,newstate];
  750. end;
  751. break;
  752. end;
  753. callparan :
  754. internalerror(200310081);
  755. else
  756. break;
  757. end;{case }
  758. end;
  759. end;
  760. procedure set_unique(p : tnode);
  761. begin
  762. while assigned(p) do
  763. begin
  764. case p.nodetype of
  765. vecn:
  766. begin
  767. include(p.flags,nf_callunique);
  768. break;
  769. end;
  770. typeconvn,
  771. subscriptn,
  772. derefn:
  773. p:=tunarynode(p).left;
  774. else
  775. break;
  776. end;
  777. end;
  778. end;
  779. function valid_for_assign(p:tnode;opts:TValidAssigns; report_errors: boolean):boolean;
  780. var
  781. hp : tnode;
  782. gotstring,
  783. gotwith,
  784. gotsubscript,
  785. gotrecord,
  786. gotpointer,
  787. gotvec,
  788. gotclass,
  789. gotdynarray,
  790. gotderef : boolean;
  791. fromdef,
  792. todef : tdef;
  793. errmsg : longint;
  794. begin
  795. if valid_const in opts then
  796. errmsg:=type_e_variable_id_expected
  797. else
  798. errmsg:=type_e_argument_cant_be_assigned;
  799. result:=false;
  800. gotsubscript:=false;
  801. gotvec:=false;
  802. gotderef:=false;
  803. gotrecord:=false;
  804. gotclass:=false;
  805. gotpointer:=false;
  806. gotwith:=false;
  807. gotdynarray:=false;
  808. gotstring:=false;
  809. hp:=p;
  810. if not(valid_void in opts) and
  811. is_void(hp.resulttype.def) then
  812. begin
  813. if report_errors then
  814. CGMessagePos(hp.fileinfo,errmsg);
  815. exit;
  816. end;
  817. while assigned(hp) do
  818. begin
  819. { property allowed? calln has a property check itself }
  820. if (nf_isproperty in hp.flags) then
  821. begin
  822. if (hp.nodetype=calln) then
  823. begin
  824. { check return type }
  825. case hp.resulttype.def.deftype of
  826. pointerdef :
  827. gotpointer:=true;
  828. objectdef :
  829. gotclass:=is_class_or_interface(hp.resulttype.def);
  830. recorddef :
  831. gotrecord:=true;
  832. classrefdef :
  833. gotclass:=true;
  834. stringdef :
  835. gotstring:=true;
  836. end;
  837. if (valid_property in opts) then
  838. begin
  839. { don't allow writing to calls that will create
  840. temps like calls that return a structure and we
  841. are assigning to a member }
  842. if (valid_const in opts) or
  843. not(
  844. (gotsubscript and gotrecord) or
  845. (gotstring and gotvec)
  846. ) then
  847. result:=true
  848. else
  849. if report_errors then
  850. CGMessagePos(hp.fileinfo,errmsg);
  851. end
  852. else
  853. begin
  854. { 1. if it returns a pointer and we've found a deref,
  855. 2. if it returns a class or record and a subscription or with is found
  856. 3. if the address is needed of a field (subscriptn) }
  857. if (gotpointer and gotderef) or
  858. (gotstring and gotvec) or
  859. (
  860. (gotclass or gotrecord) and
  861. (gotsubscript or gotwith)
  862. ) or
  863. (
  864. (gotvec and gotdynarray)
  865. ) or
  866. (
  867. (Valid_Addr in opts) and
  868. (hp.nodetype=subscriptn)
  869. ) then
  870. result:=true
  871. else
  872. if report_errors then
  873. CGMessagePos(hp.fileinfo,errmsg);
  874. end;
  875. end
  876. else
  877. result:=true;
  878. exit;
  879. end;
  880. if (Valid_Const in opts) and is_constnode(hp) then
  881. begin
  882. result:=true;
  883. exit;
  884. end;
  885. case hp.nodetype of
  886. temprefn :
  887. begin
  888. valid_for_assign := true;
  889. exit;
  890. end;
  891. derefn :
  892. begin
  893. gotderef:=true;
  894. hp:=tderefnode(hp).left;
  895. end;
  896. typeconvn :
  897. begin
  898. { typecast sizes must match, exceptions:
  899. - implicit typecast made by absolute
  900. - from formaldef
  901. - from void
  902. - from/to open array
  903. - typecast from pointer to array }
  904. fromdef:=ttypeconvnode(hp).left.resulttype.def;
  905. todef:=hp.resulttype.def;
  906. if not((nf_absolute in ttypeconvnode(hp).flags) or
  907. (fromdef.deftype=formaldef) or
  908. is_void(fromdef) or
  909. is_open_array(fromdef) or
  910. is_open_array(todef) or
  911. ((fromdef.deftype=pointerdef) and (todef.deftype=arraydef)) or
  912. ((fromdef.deftype = objectdef) and (todef.deftype = objectdef) and
  913. (tobjectdef(fromdef).is_related(tobjectdef(todef))))) and
  914. (fromdef.size<>todef.size) then
  915. begin
  916. { in TP it is allowed to typecast to smaller types. But the variable can't
  917. be in a register }
  918. if (m_tp7 in aktmodeswitches) or
  919. (todef.size<fromdef.size) then
  920. make_not_regable(hp)
  921. else
  922. if report_errors then
  923. CGMessagePos2(hp.fileinfo,type_e_typecast_wrong_size_for_assignment,tostr(fromdef.size),tostr(todef.size));
  924. end;
  925. { don't allow assignments to typeconvs that need special code }
  926. if not(gotsubscript or gotvec or gotderef) and
  927. not(ttypeconvnode(hp).assign_allowed) then
  928. begin
  929. if report_errors then
  930. CGMessagePos(hp.fileinfo,errmsg);
  931. exit;
  932. end;
  933. case hp.resulttype.def.deftype of
  934. pointerdef :
  935. gotpointer:=true;
  936. objectdef :
  937. gotclass:=is_class_or_interface(hp.resulttype.def);
  938. classrefdef :
  939. gotclass:=true;
  940. arraydef :
  941. begin
  942. { pointer -> array conversion is done then we need to see it
  943. as a deref, because a ^ is then not required anymore }
  944. if (ttypeconvnode(hp).left.resulttype.def.deftype=pointerdef) then
  945. gotderef:=true;
  946. end;
  947. end;
  948. hp:=ttypeconvnode(hp).left;
  949. end;
  950. vecn :
  951. begin
  952. gotvec:=true;
  953. { accesses to dyn. arrays override read only access in delphi }
  954. if (m_delphi in aktmodeswitches) and is_dynamic_array(tunarynode(hp).left.resulttype.def) then
  955. gotdynarray:=true;
  956. hp:=tunarynode(hp).left;
  957. end;
  958. asn :
  959. begin
  960. { asn can't be assigned directly, it returns the value in a register instead
  961. of reference. }
  962. if not(gotsubscript or gotderef or gotvec) then
  963. begin
  964. if report_errors then
  965. CGMessagePos(hp.fileinfo,errmsg);
  966. exit;
  967. end;
  968. hp:=tunarynode(hp).left;
  969. end;
  970. subscriptn :
  971. begin
  972. gotsubscript:=true;
  973. { loop counter? }
  974. if not(Valid_Const in opts) and
  975. (vo_is_loop_counter in tsubscriptnode(hp).vs.varoptions) then
  976. begin
  977. if report_errors then
  978. CGMessage1(parser_e_illegal_assignment_to_count_var,tsubscriptnode(hp).vs.realname)
  979. else
  980. exit;
  981. end;
  982. { a class/interface access is an implicit }
  983. { dereferencing }
  984. hp:=tsubscriptnode(hp).left;
  985. if is_class_or_interface(hp.resulttype.def) then
  986. gotderef:=true;
  987. end;
  988. muln,
  989. divn,
  990. andn,
  991. xorn,
  992. orn,
  993. notn,
  994. subn,
  995. addn :
  996. begin
  997. { Allow operators on a pointer, or an integer
  998. and a pointer typecast and deref has been found }
  999. if ((hp.resulttype.def.deftype=pointerdef) or
  1000. (is_integer(hp.resulttype.def) and gotpointer)) and
  1001. gotderef then
  1002. result:=true
  1003. else
  1004. { Temp strings are stored in memory, for compatibility with
  1005. delphi only }
  1006. if (m_delphi in aktmodeswitches) and
  1007. ((valid_addr in opts) or
  1008. (valid_const in opts)) and
  1009. (hp.resulttype.def.deftype=stringdef) then
  1010. result:=true
  1011. else
  1012. if report_errors then
  1013. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  1014. exit;
  1015. end;
  1016. niln,
  1017. pointerconstn :
  1018. begin
  1019. { to support e.g. @tmypointer(0)^.data; see tests/tbs/tb0481 }
  1020. if gotderef then
  1021. result:=true
  1022. else
  1023. if report_errors then
  1024. CGMessagePos(hp.fileinfo,type_e_no_assign_to_addr);
  1025. exit;
  1026. end;
  1027. addrn :
  1028. begin
  1029. if gotderef then
  1030. result:=true
  1031. else
  1032. if report_errors then
  1033. CGMessagePos(hp.fileinfo,type_e_no_assign_to_addr);
  1034. exit;
  1035. end;
  1036. calln :
  1037. begin
  1038. { check return type }
  1039. case hp.resulttype.def.deftype of
  1040. arraydef :
  1041. begin
  1042. { dynamic arrays are allowed when there is also a
  1043. vec node }
  1044. if is_dynamic_array(hp.resulttype.def) and
  1045. gotvec then
  1046. begin
  1047. gotderef:=true;
  1048. gotpointer:=true;
  1049. end;
  1050. end;
  1051. pointerdef :
  1052. gotpointer:=true;
  1053. objectdef :
  1054. gotclass:=is_class_or_interface(hp.resulttype.def);
  1055. recorddef, { handle record like class it needs a subscription }
  1056. classrefdef :
  1057. gotclass:=true;
  1058. stringdef :
  1059. gotstring:=true;
  1060. end;
  1061. { 1. if it returns a pointer and we've found a deref,
  1062. 2. if it returns a class or record and a subscription or with is found
  1063. 3. string is returned }
  1064. if (gotstring and gotvec) or
  1065. (gotpointer and gotderef) or
  1066. (gotclass and (gotsubscript or gotwith)) then
  1067. result:=true
  1068. else
  1069. { Temp strings are stored in memory, for compatibility with
  1070. delphi only }
  1071. if (m_delphi in aktmodeswitches) and
  1072. (valid_addr in opts) and
  1073. (hp.resulttype.def.deftype=stringdef) then
  1074. result:=true
  1075. else
  1076. if ([valid_const,valid_addr] * opts = [valid_const]) then
  1077. result:=true
  1078. else
  1079. if report_errors then
  1080. CGMessagePos(hp.fileinfo,errmsg);
  1081. exit;
  1082. end;
  1083. inlinen :
  1084. begin
  1085. if (valid_const in opts) and
  1086. (tinlinenode(hp).inlinenumber in [in_typeof_x]) then
  1087. result:=true
  1088. else
  1089. if report_errors then
  1090. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  1091. exit;
  1092. end;
  1093. loadn :
  1094. begin
  1095. case tloadnode(hp).symtableentry.typ of
  1096. absolutevarsym,
  1097. globalvarsym,
  1098. localvarsym,
  1099. paravarsym :
  1100. begin
  1101. { loop counter? }
  1102. if not(Valid_Const in opts) and
  1103. not gotderef and
  1104. (vo_is_loop_counter in tabstractvarsym(tloadnode(hp).symtableentry).varoptions) then
  1105. if report_errors then
  1106. CGMessage1(parser_e_illegal_assignment_to_count_var,tloadnode(hp).symtableentry.realname)
  1107. else
  1108. exit;
  1109. { derefed pointer }
  1110. if (tabstractvarsym(tloadnode(hp).symtableentry).varspez=vs_const) then
  1111. begin
  1112. { allow p^:= constructions with p is const parameter }
  1113. if gotderef or gotdynarray or (Valid_Const in opts) then
  1114. result:=true
  1115. else
  1116. if report_errors then
  1117. CGMessagePos(tloadnode(hp).fileinfo,type_e_no_assign_to_const);
  1118. exit;
  1119. end;
  1120. { Are we at a with symtable, then we need to process the
  1121. withrefnode also to check for maybe a const load }
  1122. if (tloadnode(hp).symtable.symtabletype=withsymtable) then
  1123. begin
  1124. { continue with processing the withref node }
  1125. hp:=tnode(twithsymtable(tloadnode(hp).symtable).withrefnode);
  1126. gotwith:=true;
  1127. end
  1128. else
  1129. begin
  1130. result:=true;
  1131. exit;
  1132. end;
  1133. end;
  1134. typedconstsym :
  1135. begin
  1136. if ttypedconstsym(tloadnode(hp).symtableentry).is_writable or
  1137. (valid_addr in opts) or
  1138. (valid_const in opts) then
  1139. result:=true
  1140. else
  1141. if report_errors then
  1142. CGMessagePos(hp.fileinfo,type_e_no_assign_to_const);
  1143. exit;
  1144. end;
  1145. procsym :
  1146. begin
  1147. if (Valid_Const in opts) then
  1148. result:=true
  1149. else
  1150. if report_errors then
  1151. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  1152. exit;
  1153. end;
  1154. labelsym :
  1155. begin
  1156. if (Valid_Addr in opts) then
  1157. result:=true
  1158. else
  1159. if report_errors then
  1160. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  1161. exit;
  1162. end;
  1163. constsym:
  1164. begin
  1165. if (tconstsym(tloadnode(hp).symtableentry).consttyp=constresourcestring) and
  1166. (valid_addr in opts) then
  1167. result:=true
  1168. else
  1169. if report_errors then
  1170. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  1171. exit;
  1172. end;
  1173. else
  1174. begin
  1175. if report_errors then
  1176. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  1177. exit;
  1178. end;
  1179. end;
  1180. end;
  1181. else
  1182. begin
  1183. if report_errors then
  1184. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  1185. exit;
  1186. end;
  1187. end;
  1188. end;
  1189. end;
  1190. function valid_for_var(p:tnode; report_errors: boolean):boolean;
  1191. begin
  1192. valid_for_var:=valid_for_assign(p,[],report_errors);
  1193. end;
  1194. function valid_for_formal_var(p : tnode; report_errors: boolean) : boolean;
  1195. begin
  1196. valid_for_formal_var:=valid_for_assign(p,[valid_void],report_errors);
  1197. end;
  1198. function valid_for_formal_const(p : tnode; report_errors: boolean) : boolean;
  1199. begin
  1200. valid_for_formal_const:=(p.resulttype.def.deftype=formaldef) or
  1201. valid_for_assign(p,[valid_void,valid_const],report_errors);
  1202. end;
  1203. function valid_for_assignment(p:tnode; report_errors: boolean):boolean;
  1204. begin
  1205. valid_for_assignment:=valid_for_assign(p,[valid_property],report_errors);
  1206. end;
  1207. function valid_for_addr(p : tnode; report_errors: boolean) : boolean;
  1208. begin
  1209. result:=valid_for_assign(p,[valid_const,valid_addr,valid_void],report_errors);
  1210. end;
  1211. procedure var_para_allowed(var eq:tequaltype;def_from,def_to:Tdef);
  1212. begin
  1213. { Note: eq must be already valid, it will only be updated! }
  1214. case def_to.deftype of
  1215. formaldef :
  1216. begin
  1217. { all types can be passed to a formaldef }
  1218. eq:=te_equal;
  1219. end;
  1220. orddef :
  1221. begin
  1222. { allows conversion from word to integer and
  1223. byte to shortint, but only for TP7 compatibility }
  1224. if (m_tp7 in aktmodeswitches) and
  1225. (def_from.deftype=orddef) and
  1226. (def_from.size=def_to.size) then
  1227. eq:=te_convert_l1;
  1228. end;
  1229. arraydef :
  1230. begin
  1231. if is_open_array(def_to) and
  1232. is_dynamic_array(def_from) and
  1233. equal_defs(tarraydef(def_from).elementtype.def,tarraydef(def_to).elementtype.def) then
  1234. eq:=te_convert_l2;
  1235. end;
  1236. pointerdef :
  1237. begin
  1238. { an implicit pointer conversion is allowed }
  1239. if (def_from.deftype=pointerdef) then
  1240. eq:=te_convert_l1;
  1241. end;
  1242. stringdef :
  1243. begin
  1244. { all shortstrings are allowed, size is not important }
  1245. if is_shortstring(def_from) and
  1246. is_shortstring(def_to) then
  1247. eq:=te_equal;
  1248. end;
  1249. objectdef :
  1250. begin
  1251. { child objects can be also passed }
  1252. { in non-delphi mode, otherwise }
  1253. { they must match exactly, except }
  1254. { if they are objects }
  1255. if (def_from.deftype=objectdef) and
  1256. (
  1257. not(m_delphi in aktmodeswitches) or
  1258. (
  1259. (tobjectdef(def_from).objecttype=odt_object) and
  1260. (tobjectdef(def_to).objecttype=odt_object)
  1261. )
  1262. ) and
  1263. (tobjectdef(def_from).is_related(tobjectdef(def_to))) then
  1264. eq:=te_convert_l1;
  1265. end;
  1266. filedef :
  1267. begin
  1268. { an implicit file conversion is also allowed }
  1269. { from a typed file to an untyped one }
  1270. if (def_from.deftype=filedef) and
  1271. (tfiledef(def_from).filetyp = ft_typed) and
  1272. (tfiledef(def_to).filetyp = ft_untyped) then
  1273. eq:=te_convert_l1;
  1274. end;
  1275. end;
  1276. end;
  1277. procedure para_allowed(var eq:tequaltype;p:tcallparanode;def_to:tdef);
  1278. begin
  1279. { Note: eq must be already valid, it will only be updated! }
  1280. case def_to.deftype of
  1281. formaldef :
  1282. begin
  1283. { all types can be passed to a formaldef }
  1284. eq:=te_equal;
  1285. end;
  1286. stringdef :
  1287. begin
  1288. { to support ansi/long/wide strings in a proper way }
  1289. { string and string[10] are assumed as equal }
  1290. { when searching the correct overloaded procedure }
  1291. if (p.resulttype.def.deftype=stringdef) and
  1292. (tstringdef(def_to).string_typ=tstringdef(p.resulttype.def).string_typ) then
  1293. eq:=te_equal
  1294. else
  1295. { Passing a constant char to ansistring or shortstring or
  1296. a widechar to widestring then handle it as equal. }
  1297. if (p.left.nodetype=ordconstn) and
  1298. (
  1299. is_char(p.resulttype.def) and
  1300. (is_shortstring(def_to) or is_ansistring(def_to))
  1301. ) or
  1302. (
  1303. is_widechar(p.resulttype.def) and
  1304. is_widestring(def_to)
  1305. ) then
  1306. eq:=te_equal
  1307. end;
  1308. setdef :
  1309. begin
  1310. { set can also be a not yet converted array constructor }
  1311. if (p.resulttype.def.deftype=arraydef) and
  1312. (tarraydef(p.resulttype.def).IsConstructor) and
  1313. not(tarraydef(p.resulttype.def).IsVariant) then
  1314. eq:=te_equal;
  1315. end;
  1316. procvardef :
  1317. begin
  1318. { in tp7 mode proc -> procvar is allowed }
  1319. if ((m_tp_procvar in aktmodeswitches) or
  1320. (m_mac_procvar in aktmodeswitches)) and
  1321. (p.left.nodetype=calln) and
  1322. (proc_to_procvar_equal(tprocdef(tcallnode(p.left).procdefinition),tprocvardef(def_to))>=te_equal) then
  1323. eq:=te_equal
  1324. else
  1325. if (m_mac_procvar in aktmodeswitches) and
  1326. is_procvar_load(p.left) then
  1327. eq:=te_convert_l2;
  1328. end;
  1329. end;
  1330. end;
  1331. function allowenumop(nt:tnodetype):boolean;
  1332. begin
  1333. result:=(nt in [equaln,unequaln,ltn,lten,gtn,gten]) or
  1334. ((cs_allow_enum_calc in aktlocalswitches) and
  1335. (nt in [addn,subn]));
  1336. end;
  1337. {****************************************************************************
  1338. TCallCandidates
  1339. ****************************************************************************}
  1340. constructor tcallcandidates.create(sym:tprocsym;st:tsymtable;ppn:tnode;isprop,ignorevis : boolean);
  1341. var
  1342. j : integer;
  1343. pd : tprocdef;
  1344. hp : pcandidate;
  1345. found,
  1346. has_overload_directive : boolean;
  1347. topclassh : tobjectdef;
  1348. srsymtable : tsymtable;
  1349. srprocsym : tprocsym;
  1350. pt : tcallparanode;
  1351. begin
  1352. if not assigned(sym) then
  1353. internalerror(200411015);
  1354. FProcSym:=sym;
  1355. FProcs:=nil;
  1356. FProccnt:=0;
  1357. FProcvisiblecnt:=0;
  1358. FParanode:=ppn;
  1359. FAllowVariant:=true;
  1360. { determine length of parameter list }
  1361. pt:=tcallparanode(ppn);
  1362. FParalength:=0;
  1363. while assigned(pt) do
  1364. begin
  1365. inc(FParalength);
  1366. pt:=tcallparanode(pt.right);
  1367. end;
  1368. { when the definition has overload directive set, we search for
  1369. overloaded definitions in the class, this only needs to be done once
  1370. for class entries as the tree keeps always the same }
  1371. if (not sym.overloadchecked) and
  1372. (sym.owner.symtabletype=objectsymtable) and
  1373. (po_overload in sym.first_procdef.procoptions) then
  1374. search_class_overloads(sym);
  1375. { when the class passed is defined in this unit we
  1376. need to use the scope of that class. This is a trick
  1377. that can be used to access protected members in other
  1378. units. At least kylix supports it this way (PFV) }
  1379. if assigned(st) and
  1380. (
  1381. (st.symtabletype=objectsymtable) or
  1382. ((st.symtabletype=withsymtable) and
  1383. (st.defowner.deftype=objectdef))
  1384. ) and
  1385. (st.defowner.owner.symtabletype in [globalsymtable,staticsymtable]) and
  1386. st.defowner.owner.iscurrentunit then
  1387. topclassh:=tobjectdef(st.defowner)
  1388. else
  1389. begin
  1390. if assigned(current_procinfo) then
  1391. topclassh:=current_procinfo.procdef._class
  1392. else
  1393. topclassh:=nil;
  1394. end;
  1395. { link all procedures which have the same # of parameters }
  1396. for j:=1 to sym.procdef_count do
  1397. begin
  1398. pd:=sym.procdef[j];
  1399. { Is the procdef visible? This needs to be checked on
  1400. procdef level since a symbol can contain both private and
  1401. public declarations. But the check should not be done
  1402. when the callnode is generated by a property
  1403. inherited overrides invisible anonymous inherited (FK) }
  1404. if isprop or ignorevis or
  1405. (pd.owner.symtabletype<>objectsymtable) or
  1406. pd.is_visible_for_object(topclassh) then
  1407. begin
  1408. { we have at least one procedure that is visible }
  1409. inc(FProcvisiblecnt);
  1410. { only when the # of parameter are supported by the
  1411. procedure }
  1412. if (FParalength>=pd.minparacount) and
  1413. ((po_varargs in pd.procoptions) or { varargs }
  1414. (FParalength<=pd.maxparacount)) then
  1415. proc_add(pd);
  1416. end;
  1417. end;
  1418. { remember if the procedure is declared with the overload directive,
  1419. it's information is still needed also after all procs are removed }
  1420. has_overload_directive:=(po_overload in sym.first_procdef.procoptions);
  1421. { when the definition has overload directive set, we search for
  1422. overloaded definitions in the symtablestack. The found
  1423. entries are only added to the procs list and not the procsym, because
  1424. the list can change in every situation }
  1425. if has_overload_directive and
  1426. (sym.owner.symtabletype<>objectsymtable) then
  1427. begin
  1428. srsymtable:=sym.owner.next;
  1429. while assigned(srsymtable) do
  1430. begin
  1431. if srsymtable.symtabletype in [localsymtable,staticsymtable,globalsymtable] then
  1432. begin
  1433. srprocsym:=tprocsym(srsymtable.speedsearch(sym.name,sym.speedvalue));
  1434. if assigned(srprocsym) and
  1435. (srprocsym.typ=procsym) then
  1436. begin
  1437. { if this visible procedure doesn't have overload we can stop
  1438. searching }
  1439. if not(po_overload in srprocsym.first_procdef.procoptions) and
  1440. srprocsym.first_procdef.is_visible_for_object(topclassh) then
  1441. break;
  1442. { process all overloaded definitions }
  1443. for j:=1 to srprocsym.procdef_count do
  1444. begin
  1445. pd:=srprocsym.procdef[j];
  1446. { only visible procedures need to be added }
  1447. if pd.is_visible_for_object(topclassh) then
  1448. begin
  1449. { only when the # of parameter are supported by the
  1450. procedure }
  1451. if (FParalength>=pd.minparacount) and
  1452. ((po_varargs in pd.procoptions) or { varargs }
  1453. (FParalength<=pd.maxparacount)) then
  1454. begin
  1455. found:=false;
  1456. hp:=FProcs;
  1457. while assigned(hp) do
  1458. begin
  1459. { Only compare visible parameters for the user }
  1460. if compare_paras(hp^.data.paras,pd.paras,cp_value_equal_const,[cpo_ignorehidden])>=te_equal then
  1461. begin
  1462. found:=true;
  1463. break;
  1464. end;
  1465. hp:=hp^.next;
  1466. end;
  1467. if not found then
  1468. proc_add(pd);
  1469. end;
  1470. end;
  1471. end;
  1472. end;
  1473. end;
  1474. srsymtable:=srsymtable.next;
  1475. end;
  1476. end;
  1477. end;
  1478. constructor tcallcandidates.create_operator(op:ttoken;ppn:tnode);
  1479. var
  1480. j : integer;
  1481. pd : tprocdef;
  1482. hp : pcandidate;
  1483. found : boolean;
  1484. srsymtable : tsymtable;
  1485. srprocsym : tprocsym;
  1486. pt : tcallparanode;
  1487. sv : cardinal;
  1488. begin
  1489. FProcSym:=nil;
  1490. FProcs:=nil;
  1491. FProccnt:=0;
  1492. FProcvisiblecnt:=0;
  1493. FParanode:=ppn;
  1494. FAllowVariant:=false;
  1495. { determine length of parameter list }
  1496. pt:=tcallparanode(ppn);
  1497. FParalength:=0;
  1498. while assigned(pt) do
  1499. begin
  1500. if pt.resulttype.def.deftype=variantdef then
  1501. FAllowVariant:=true;
  1502. inc(FParalength);
  1503. pt:=tcallparanode(pt.right);
  1504. end;
  1505. { we search all overloaded operator definitions in the symtablestack. The found
  1506. entries are only added to the procs list and not the procsym, because
  1507. the list can change in every situation }
  1508. sv:=getspeedvalue(overloaded_names[op]);
  1509. srsymtable:=symtablestack;
  1510. while assigned(srsymtable) do
  1511. begin
  1512. if srsymtable.symtabletype in [localsymtable,staticsymtable,globalsymtable] then
  1513. begin
  1514. srprocsym:=tprocsym(srsymtable.speedsearch(overloaded_names[op],sv));
  1515. if assigned(srprocsym) and
  1516. (srprocsym.typ=procsym) then
  1517. begin
  1518. { Store first procsym found }
  1519. if not assigned(FProcsym) then
  1520. FProcsym:=srprocsym;
  1521. { process all overloaded definitions }
  1522. for j:=1 to srprocsym.procdef_count do
  1523. begin
  1524. pd:=srprocsym.procdef[j];
  1525. { only when the # of parameter are supported by the
  1526. procedure }
  1527. if (FParalength>=pd.minparacount) and
  1528. (FParalength<=pd.maxparacount) then
  1529. begin
  1530. found:=false;
  1531. hp:=FProcs;
  1532. while assigned(hp) do
  1533. begin
  1534. { Only compare visible parameters for the user }
  1535. if compare_paras(hp^.data.paras,pd.paras,cp_value_equal_const,[cpo_ignorehidden])>=te_equal then
  1536. begin
  1537. found:=true;
  1538. break;
  1539. end;
  1540. hp:=hp^.next;
  1541. end;
  1542. if not found then
  1543. proc_add(pd);
  1544. end;
  1545. end;
  1546. end;
  1547. end;
  1548. srsymtable:=srsymtable.next;
  1549. end;
  1550. end;
  1551. destructor tcallcandidates.destroy;
  1552. var
  1553. hpnext,
  1554. hp : pcandidate;
  1555. begin
  1556. hp:=FProcs;
  1557. while assigned(hp) do
  1558. begin
  1559. hpnext:=hp^.next;
  1560. dispose(hp);
  1561. hp:=hpnext;
  1562. end;
  1563. end;
  1564. function tcallcandidates.proc_add(pd:tprocdef):pcandidate;
  1565. var
  1566. defaultparacnt : integer;
  1567. begin
  1568. { generate new candidate entry }
  1569. new(result);
  1570. fillchar(result^,sizeof(tcandidate),0);
  1571. result^.data:=pd;
  1572. result^.next:=FProcs;
  1573. FProcs:=result;
  1574. inc(FProccnt);
  1575. { Find last parameter, skip all default parameters
  1576. that are not passed. Ignore this skipping for varargs }
  1577. result^.firstparaidx:=pd.paras.count-1;
  1578. if not(po_varargs in pd.procoptions) then
  1579. begin
  1580. { ignore hidden parameters }
  1581. while (result^.firstparaidx>=0) and (vo_is_hidden_para in tparavarsym(pd.paras[result^.firstparaidx]).varoptions) do
  1582. dec(result^.firstparaidx);
  1583. defaultparacnt:=pd.maxparacount-FParalength;
  1584. if defaultparacnt>0 then
  1585. begin
  1586. if defaultparacnt>result^.firstparaidx+1 then
  1587. internalerror(200401141);
  1588. dec(result^.firstparaidx,defaultparacnt);
  1589. end;
  1590. end;
  1591. end;
  1592. procedure tcallcandidates.list(all:boolean);
  1593. var
  1594. hp : pcandidate;
  1595. begin
  1596. hp:=FProcs;
  1597. while assigned(hp) do
  1598. begin
  1599. if all or
  1600. (not hp^.invalid) then
  1601. MessagePos1(hp^.data.fileinfo,sym_h_param_list,hp^.data.fullprocname(false));
  1602. hp:=hp^.next;
  1603. end;
  1604. end;
  1605. {$ifdef EXTDEBUG}
  1606. procedure tcallcandidates.dump_info(lvl:longint);
  1607. function ParaTreeStr(p:tcallparanode):string;
  1608. begin
  1609. result:='';
  1610. while assigned(p) do
  1611. begin
  1612. if result<>'' then
  1613. result:=','+result;
  1614. result:=p.resulttype.def.typename+result;
  1615. p:=tcallparanode(p.right);
  1616. end;
  1617. end;
  1618. var
  1619. hp : pcandidate;
  1620. i : integer;
  1621. currpara : tparavarsym;
  1622. begin
  1623. if not CheckVerbosity(lvl) then
  1624. exit;
  1625. Comment(lvl+V_LineInfo,'Overloaded callnode: '+FProcSym.name+'('+ParaTreeStr(tcallparanode(FParaNode))+')');
  1626. hp:=FProcs;
  1627. while assigned(hp) do
  1628. begin
  1629. Comment(lvl,' '+hp^.data.fullprocname(false));
  1630. if (hp^.invalid) then
  1631. Comment(lvl,' invalid')
  1632. else
  1633. begin
  1634. Comment(lvl,' ex: '+tostr(hp^.exact_count)+
  1635. ' eq: '+tostr(hp^.equal_count)+
  1636. ' l1: '+tostr(hp^.cl1_count)+
  1637. ' l2: '+tostr(hp^.cl2_count)+
  1638. ' l3: '+tostr(hp^.cl3_count)+
  1639. ' oper: '+tostr(hp^.coper_count)+
  1640. ' ord: '+realtostr(hp^.ordinal_distance));
  1641. { Print parameters in left-right order }
  1642. for i:=0 to hp^.data.paras.count-1 do
  1643. begin
  1644. currpara:=tparavarsym(hp^.data.paras[i]);
  1645. if (vo_is_hidden_para in currpara.varoptions) then
  1646. Comment(lvl,' - '+currpara.vartype.def.typename+' : '+EqualTypeName[currpara.eqval]);
  1647. end;
  1648. end;
  1649. hp:=hp^.next;
  1650. end;
  1651. end;
  1652. {$endif EXTDEBUG}
  1653. procedure tcallcandidates.get_information;
  1654. var
  1655. hp : pcandidate;
  1656. currpara : tparavarsym;
  1657. paraidx : integer;
  1658. currparanr : byte;
  1659. rfh,rth : bestreal;
  1660. objdef : tobjectdef;
  1661. def_from,
  1662. def_to : tdef;
  1663. currpt,
  1664. pt : tcallparanode;
  1665. eq : tequaltype;
  1666. convtype : tconverttype;
  1667. pdoper : tprocdef;
  1668. releasecurrpt : boolean;
  1669. cdoptions : tcompare_defs_options;
  1670. begin
  1671. cdoptions:=[cdo_check_operator];
  1672. if FAllowVariant then
  1673. include(cdoptions,cdo_allow_variant);
  1674. { process all procs }
  1675. hp:=FProcs;
  1676. while assigned(hp) do
  1677. begin
  1678. { We compare parameters in reverse order (right to left),
  1679. the firstpara is already pointing to the last parameter
  1680. were we need to start comparing }
  1681. currparanr:=FParalength;
  1682. paraidx:=hp^.firstparaidx;
  1683. while (paraidx>=0) and (vo_is_hidden_para in tparavarsym(hp^.data.paras[paraidx]).varoptions) do
  1684. dec(paraidx);
  1685. pt:=tcallparanode(FParaNode);
  1686. while assigned(pt) and (paraidx>=0) do
  1687. begin
  1688. currpara:=tparavarsym(hp^.data.paras[paraidx]);
  1689. { currpt can be changed from loadn to calln when a procvar
  1690. is passed. This is to prevent that the change is permanent }
  1691. currpt:=pt;
  1692. releasecurrpt:=false;
  1693. { retrieve current parameter definitions to compares }
  1694. eq:=te_incompatible;
  1695. def_from:=currpt.resulttype.def;
  1696. def_to:=currpara.vartype.def;
  1697. if not(assigned(def_from)) then
  1698. internalerror(200212091);
  1699. if not(
  1700. assigned(def_to) or
  1701. ((po_varargs in hp^.data.procoptions) and
  1702. (currparanr>hp^.data.minparacount))
  1703. ) then
  1704. internalerror(200212092);
  1705. { Convert tp procvars when not expecting a procvar }
  1706. if (def_to.deftype<>procvardef) and
  1707. (currpt.left.resulttype.def.deftype=procvardef) then
  1708. begin
  1709. releasecurrpt:=true;
  1710. currpt:=tcallparanode(pt.getcopy);
  1711. if maybe_call_procvar(currpt.left,true) then
  1712. begin
  1713. currpt.resulttype:=currpt.left.resulttype;
  1714. def_from:=currpt.left.resulttype.def;
  1715. end;
  1716. end;
  1717. { varargs are always equal, but not exact }
  1718. if (po_varargs in hp^.data.procoptions) and
  1719. (currparanr>hp^.data.minparacount) then
  1720. begin
  1721. eq:=te_equal;
  1722. end
  1723. else
  1724. { same definition -> exact }
  1725. if (def_from=def_to) then
  1726. begin
  1727. eq:=te_exact;
  1728. end
  1729. else
  1730. { for value and const parameters check if a integer is constant or
  1731. included in other integer -> equal and calc ordinal_distance }
  1732. if not(currpara.varspez in [vs_var,vs_out]) and
  1733. is_integer(def_from) and
  1734. is_integer(def_to) and
  1735. is_in_limit(def_from,def_to) then
  1736. begin
  1737. eq:=te_equal;
  1738. hp^.ordinal_distance:=hp^.ordinal_distance+
  1739. abs(bestreal(torddef(def_from).low)-bestreal(torddef(def_to).low));
  1740. if (torddef(def_to).typ=u64bit) then
  1741. rth:=bestreal(qword(torddef(def_to).high))
  1742. else
  1743. rth:=bestreal(torddef(def_to).high);
  1744. if (torddef(def_from).typ=u64bit) then
  1745. rfh:=bestreal(qword(torddef(def_from).high))
  1746. else
  1747. rfh:=bestreal(torddef(def_from).high);
  1748. hp^.ordinal_distance:=hp^.ordinal_distance+abs(rth-rfh);
  1749. { Give wrong sign a small penalty, this is need to get a diffrence
  1750. from word->[longword,longint] }
  1751. if is_signed(def_from)<>is_signed(def_to) then
  1752. hp^.ordinal_distance:=hp^.ordinal_distance+1.0;
  1753. end
  1754. else
  1755. { for value and const parameters check precision of real, give
  1756. penalty for loosing of precision. var and out parameters must match exactly }
  1757. if not(currpara.varspez in [vs_var,vs_out]) and
  1758. is_real(def_from) and
  1759. is_real(def_to) then
  1760. begin
  1761. eq:=te_equal;
  1762. if is_extended(def_to) then
  1763. rth:=bestreal(4)
  1764. else
  1765. if is_double (def_to) then
  1766. rth:=bestreal(2)
  1767. else
  1768. rth:=bestreal(1);
  1769. if is_extended(def_from) then
  1770. rfh:=bestreal(4)
  1771. else
  1772. if is_double (def_from) then
  1773. rfh:=bestreal(2)
  1774. else
  1775. rfh:=bestreal(1);
  1776. { penalty for shrinking of precision }
  1777. if rth<rfh then
  1778. rfh:=(rfh-rth)*16
  1779. else
  1780. rfh:=rth-rfh;
  1781. hp^.ordinal_distance:=hp^.ordinal_distance+rfh;
  1782. end
  1783. else
  1784. { related object parameters also need to determine the distance between the current
  1785. object and the object we are comparing with. var and out parameters must match exactly }
  1786. if not(currpara.varspez in [vs_var,vs_out]) and
  1787. (def_from.deftype=objectdef) and
  1788. (def_to.deftype=objectdef) and
  1789. (tobjectdef(def_from).objecttype=tobjectdef(def_to).objecttype) and
  1790. tobjectdef(def_from).is_related(tobjectdef(def_to)) then
  1791. begin
  1792. eq:=te_convert_l1;
  1793. objdef:=tobjectdef(def_from);
  1794. while assigned(objdef) do
  1795. begin
  1796. if objdef=def_to then
  1797. break;
  1798. hp^.ordinal_distance:=hp^.ordinal_distance+1;
  1799. objdef:=objdef.childof;
  1800. end;
  1801. end
  1802. else
  1803. { generic type comparision }
  1804. begin
  1805. eq:=compare_defs_ext(def_from,def_to,currpt.left.nodetype,convtype,pdoper,cdoptions);
  1806. { when the types are not equal we need to check
  1807. some special case for parameter passing }
  1808. if (eq<te_equal) then
  1809. begin
  1810. if currpara.varspez in [vs_var,vs_out] then
  1811. begin
  1812. { para requires an equal type so the previous found
  1813. match was not good enough, reset to incompatible }
  1814. eq:=te_incompatible;
  1815. { var_para_allowed will return te_equal and te_convert_l1 to
  1816. make a difference for best matching }
  1817. var_para_allowed(eq,currpt.resulttype.def,currpara.vartype.def)
  1818. end
  1819. else
  1820. para_allowed(eq,currpt,def_to);
  1821. end;
  1822. end;
  1823. { when a procvar was changed to a call an exact much is
  1824. downgraded to equal. This way an overload call with the
  1825. procvar is choosen. See tb0471 (PFV) }
  1826. if (pt<>currpt) and (eq=te_exact) then
  1827. eq:=te_equal;
  1828. { increase correct counter }
  1829. case eq of
  1830. te_exact :
  1831. inc(hp^.exact_count);
  1832. te_equal :
  1833. inc(hp^.equal_count);
  1834. te_convert_l1 :
  1835. inc(hp^.cl1_count);
  1836. te_convert_l2 :
  1837. inc(hp^.cl2_count);
  1838. te_convert_l3 :
  1839. inc(hp^.cl3_count);
  1840. te_convert_operator :
  1841. inc(hp^.coper_count);
  1842. te_incompatible :
  1843. hp^.invalid:=true;
  1844. else
  1845. internalerror(200212072);
  1846. end;
  1847. { stop checking when an incompatible parameter is found }
  1848. if hp^.invalid then
  1849. begin
  1850. { store the current parameter info for
  1851. a nice error message when no procedure is found }
  1852. hp^.wrongparaidx:=paraidx;
  1853. hp^.wrongparanr:=currparanr;
  1854. break;
  1855. end;
  1856. {$ifdef EXTDEBUG}
  1857. { store equal in node tree for dump }
  1858. currpara.eqval:=eq;
  1859. {$endif EXTDEBUG}
  1860. { maybe release temp currpt }
  1861. if releasecurrpt then
  1862. currpt.free;
  1863. { next parameter in the call tree }
  1864. pt:=tcallparanode(pt.right);
  1865. { next parameter for definition, only goto next para
  1866. if we're out of the varargs }
  1867. if not(po_varargs in hp^.data.procoptions) or
  1868. (currparanr<=hp^.data.maxparacount) then
  1869. begin
  1870. { Ignore vs_hidden parameters }
  1871. repeat
  1872. dec(paraidx);
  1873. until (paraidx<0) or not(vo_is_hidden_para in tparavarsym(hp^.data.paras[paraidx]).varoptions);
  1874. end;
  1875. dec(currparanr);
  1876. end;
  1877. if not(hp^.invalid) and
  1878. (assigned(pt) or (paraidx>=0) or (currparanr<>0)) then
  1879. internalerror(200212141);
  1880. { next candidate }
  1881. hp:=hp^.next;
  1882. end;
  1883. end;
  1884. function is_better_candidate(currpd,bestpd:pcandidate):integer;
  1885. var
  1886. res : integer;
  1887. begin
  1888. {
  1889. Return values:
  1890. > 0 when currpd is better than bestpd
  1891. < 0 when bestpd is better than currpd
  1892. = 0 when both are equal
  1893. To choose the best candidate we use the following order:
  1894. - Incompatible flag
  1895. - (Smaller) Number of convert operator parameters.
  1896. - (Smaller) Number of convertlevel 2 parameters.
  1897. - (Smaller) Number of convertlevel 1 parameters.
  1898. - (Bigger) Number of exact parameters.
  1899. - (Smaller) Number of equal parameters.
  1900. - (Smaller) Total of ordinal distance. For example, the distance of a word
  1901. to a byte is 65535-255=65280.
  1902. }
  1903. if bestpd^.invalid then
  1904. begin
  1905. if currpd^.invalid then
  1906. res:=0
  1907. else
  1908. res:=1;
  1909. end
  1910. else
  1911. if currpd^.invalid then
  1912. res:=-1
  1913. else
  1914. begin
  1915. { less operator parameters? }
  1916. res:=(bestpd^.coper_count-currpd^.coper_count);
  1917. if (res=0) then
  1918. begin
  1919. { less cl3 parameters? }
  1920. res:=(bestpd^.cl3_count-currpd^.cl3_count);
  1921. if (res=0) then
  1922. begin
  1923. { less cl2 parameters? }
  1924. res:=(bestpd^.cl2_count-currpd^.cl2_count);
  1925. if (res=0) then
  1926. begin
  1927. { less cl1 parameters? }
  1928. res:=(bestpd^.cl1_count-currpd^.cl1_count);
  1929. if (res=0) then
  1930. begin
  1931. { more exact parameters? }
  1932. res:=(currpd^.exact_count-bestpd^.exact_count);
  1933. if (res=0) then
  1934. begin
  1935. { less equal parameters? }
  1936. res:=(bestpd^.equal_count-currpd^.equal_count);
  1937. if (res=0) then
  1938. begin
  1939. { smaller ordinal distance? }
  1940. if (currpd^.ordinal_distance<bestpd^.ordinal_distance) then
  1941. res:=1
  1942. else
  1943. if (currpd^.ordinal_distance>bestpd^.ordinal_distance) then
  1944. res:=-1
  1945. else
  1946. res:=0;
  1947. end;
  1948. end;
  1949. end;
  1950. end;
  1951. end;
  1952. end;
  1953. end;
  1954. is_better_candidate:=res;
  1955. end;
  1956. function tcallcandidates.choose_best(var bestpd:tabstractprocdef):integer;
  1957. var
  1958. besthpstart,
  1959. hp : pcandidate;
  1960. cntpd,
  1961. res : integer;
  1962. begin
  1963. {
  1964. Returns the number of candidates left and the
  1965. first candidate is returned in pdbest
  1966. }
  1967. { Setup the first procdef as best, only count it as a result
  1968. when it is valid }
  1969. bestpd:=FProcs^.data;
  1970. if FProcs^.invalid then
  1971. cntpd:=0
  1972. else
  1973. cntpd:=1;
  1974. if assigned(FProcs^.next) then
  1975. begin
  1976. besthpstart:=FProcs;
  1977. hp:=FProcs^.next;
  1978. while assigned(hp) do
  1979. begin
  1980. res:=is_better_candidate(hp,besthpstart);
  1981. if (res>0) then
  1982. begin
  1983. { hp is better, flag all procs to be incompatible }
  1984. while (besthpstart<>hp) do
  1985. begin
  1986. besthpstart^.invalid:=true;
  1987. besthpstart:=besthpstart^.next;
  1988. end;
  1989. { besthpstart is already set to hp }
  1990. bestpd:=besthpstart^.data;
  1991. cntpd:=1;
  1992. end
  1993. else
  1994. if (res<0) then
  1995. begin
  1996. { besthpstart is better, flag current hp to be incompatible }
  1997. hp^.invalid:=true;
  1998. end
  1999. else
  2000. begin
  2001. { res=0, both are valid }
  2002. if not hp^.invalid then
  2003. inc(cntpd);
  2004. end;
  2005. hp:=hp^.next;
  2006. end;
  2007. end;
  2008. result:=cntpd;
  2009. end;
  2010. procedure tcallcandidates.find_wrong_para;
  2011. var
  2012. currparanr : smallint;
  2013. hp : pcandidate;
  2014. pt : tcallparanode;
  2015. wrongpara : tparavarsym;
  2016. begin
  2017. { Only process the first overloaded procdef }
  2018. hp:=FProcs;
  2019. { Find callparanode corresponding to the argument }
  2020. pt:=tcallparanode(FParanode);
  2021. currparanr:=FParalength;
  2022. while assigned(pt) and
  2023. (currparanr>hp^.wrongparanr) do
  2024. begin
  2025. pt:=tcallparanode(pt.right);
  2026. dec(currparanr);
  2027. end;
  2028. if (currparanr<>hp^.wrongparanr) or
  2029. not assigned(pt) then
  2030. internalerror(200212094);
  2031. { Show error message, when it was a var or out parameter
  2032. guess that it is a missing typeconv }
  2033. wrongpara:=tparavarsym(hp^.data.paras[hp^.wrongparaidx]);
  2034. if wrongpara.varspez in [vs_var,vs_out] then
  2035. begin
  2036. { Maybe passing the correct type but passing a const to var parameter }
  2037. if (compare_defs(pt.resulttype.def,wrongpara.vartype.def,pt.nodetype)<>te_incompatible) and
  2038. not valid_for_var(pt.left,true) then
  2039. CGMessagePos(pt.left.fileinfo,type_e_variable_id_expected)
  2040. else
  2041. CGMessagePos2(pt.left.fileinfo,parser_e_call_by_ref_without_typeconv,
  2042. FullTypeName(pt.left.resulttype.def,wrongpara.vartype.def),
  2043. FullTypeName(wrongpara.vartype.def,pt.left.resulttype.def))
  2044. end
  2045. else
  2046. CGMessagePos3(pt.left.fileinfo,type_e_wrong_parameter_type,tostr(hp^.wrongparanr),
  2047. FullTypeName(pt.left.resulttype.def,wrongpara.vartype.def),
  2048. FullTypeName(wrongpara.vartype.def,pt.left.resulttype.def));
  2049. end;
  2050. end.