htypechk.pas 85 KB

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