htypechk.pas 83 KB

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