htypechk.pas 83 KB

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