htypechk.pas 80 KB

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