htypechk.pas 84 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268
  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(p : tnode; how: tvarregable);
  574. begin
  575. case p.nodetype of
  576. typeconvn :
  577. make_not_regable(ttypeconvnode(p).left,how);
  578. loadn :
  579. if (tloadnode(p).symtableentry.typ in [globalvarsym,localvarsym,paravarsym]) and
  580. (tabstractvarsym(tloadnode(p).symtableentry).varregable <> vr_none) then
  581. if (tloadnode(p).symtableentry.typ = paravarsym) then
  582. tabstractvarsym(tloadnode(p).symtableentry).varregable:=how
  583. else
  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,vr_addr)
  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]))
  1091. {$ifdef SUPPORT_UNALIGNED}
  1092. or (tinlinenode(hp).inlinenumber in [in_unaligned_x])
  1093. {$endif SUPPORT_UNALIGNED}
  1094. then
  1095. result:=true
  1096. else
  1097. if report_errors then
  1098. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  1099. exit;
  1100. end;
  1101. loadn :
  1102. begin
  1103. case tloadnode(hp).symtableentry.typ of
  1104. absolutevarsym,
  1105. globalvarsym,
  1106. localvarsym,
  1107. paravarsym :
  1108. begin
  1109. { loop counter? }
  1110. if not(Valid_Const in opts) and
  1111. not gotderef and
  1112. (vo_is_loop_counter in tabstractvarsym(tloadnode(hp).symtableentry).varoptions) then
  1113. if report_errors then
  1114. CGMessage1(parser_e_illegal_assignment_to_count_var,tloadnode(hp).symtableentry.realname)
  1115. else
  1116. exit;
  1117. { derefed pointer }
  1118. if (tabstractvarsym(tloadnode(hp).symtableentry).varspez=vs_const) then
  1119. begin
  1120. { allow p^:= constructions with p is const parameter }
  1121. if gotderef or gotdynarray or (Valid_Const in opts) then
  1122. result:=true
  1123. else
  1124. if report_errors then
  1125. CGMessagePos(tloadnode(hp).fileinfo,type_e_no_assign_to_const);
  1126. exit;
  1127. end;
  1128. result:=true;
  1129. exit;
  1130. end;
  1131. typedconstsym :
  1132. begin
  1133. if ttypedconstsym(tloadnode(hp).symtableentry).is_writable or
  1134. (valid_addr in opts) or
  1135. (valid_const in opts) then
  1136. result:=true
  1137. else
  1138. if report_errors then
  1139. CGMessagePos(hp.fileinfo,type_e_no_assign_to_const);
  1140. exit;
  1141. end;
  1142. procsym :
  1143. begin
  1144. if (Valid_Const in opts) then
  1145. result:=true
  1146. else
  1147. if report_errors then
  1148. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  1149. exit;
  1150. end;
  1151. labelsym :
  1152. begin
  1153. if (Valid_Addr in opts) then
  1154. result:=true
  1155. else
  1156. if report_errors then
  1157. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  1158. exit;
  1159. end;
  1160. constsym:
  1161. begin
  1162. if (tconstsym(tloadnode(hp).symtableentry).consttyp=constresourcestring) and
  1163. (valid_addr in opts) then
  1164. result:=true
  1165. else
  1166. if report_errors then
  1167. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  1168. exit;
  1169. end;
  1170. else
  1171. begin
  1172. if report_errors then
  1173. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  1174. exit;
  1175. end;
  1176. end;
  1177. end;
  1178. else
  1179. begin
  1180. if report_errors then
  1181. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  1182. exit;
  1183. end;
  1184. end;
  1185. end;
  1186. end;
  1187. function valid_for_var(p:tnode; report_errors: boolean):boolean;
  1188. begin
  1189. valid_for_var:=valid_for_assign(p,[],report_errors);
  1190. end;
  1191. function valid_for_formal_var(p : tnode; report_errors: boolean) : boolean;
  1192. begin
  1193. valid_for_formal_var:=valid_for_assign(p,[valid_void],report_errors);
  1194. end;
  1195. function valid_for_formal_const(p : tnode; report_errors: boolean) : boolean;
  1196. begin
  1197. valid_for_formal_const:=(p.resulttype.def.deftype=formaldef) or
  1198. valid_for_assign(p,[valid_void,valid_const],report_errors);
  1199. end;
  1200. function valid_for_assignment(p:tnode; report_errors: boolean):boolean;
  1201. begin
  1202. valid_for_assignment:=valid_for_assign(p,[valid_property],report_errors);
  1203. end;
  1204. function valid_for_addr(p : tnode; report_errors: boolean) : boolean;
  1205. begin
  1206. result:=valid_for_assign(p,[valid_const,valid_addr,valid_void],report_errors);
  1207. end;
  1208. procedure var_para_allowed(var eq:tequaltype;def_from,def_to:Tdef);
  1209. begin
  1210. { Note: eq must be already valid, it will only be updated! }
  1211. case def_to.deftype of
  1212. formaldef :
  1213. begin
  1214. { all types can be passed to a formaldef }
  1215. eq:=te_equal;
  1216. end;
  1217. orddef :
  1218. begin
  1219. { allows conversion from word to integer and
  1220. byte to shortint, but only for TP7 compatibility }
  1221. if (m_tp7 in aktmodeswitches) and
  1222. (def_from.deftype=orddef) and
  1223. (def_from.size=def_to.size) then
  1224. eq:=te_convert_l1;
  1225. end;
  1226. arraydef :
  1227. begin
  1228. if is_open_array(def_to) and
  1229. is_dynamic_array(def_from) and
  1230. equal_defs(tarraydef(def_from).elementtype.def,tarraydef(def_to).elementtype.def) then
  1231. eq:=te_convert_l2;
  1232. end;
  1233. pointerdef :
  1234. begin
  1235. { an implicit pointer conversion is allowed }
  1236. if (def_from.deftype=pointerdef) then
  1237. eq:=te_convert_l1;
  1238. end;
  1239. stringdef :
  1240. begin
  1241. { all shortstrings are allowed, size is not important }
  1242. if is_shortstring(def_from) and
  1243. is_shortstring(def_to) then
  1244. eq:=te_equal;
  1245. end;
  1246. objectdef :
  1247. begin
  1248. { child objects can be also passed }
  1249. { in non-delphi mode, otherwise }
  1250. { they must match exactly, except }
  1251. { if they are objects }
  1252. if (def_from.deftype=objectdef) and
  1253. (
  1254. not(m_delphi in aktmodeswitches) or
  1255. (
  1256. (tobjectdef(def_from).objecttype=odt_object) and
  1257. (tobjectdef(def_to).objecttype=odt_object)
  1258. )
  1259. ) and
  1260. (tobjectdef(def_from).is_related(tobjectdef(def_to))) then
  1261. eq:=te_convert_l1;
  1262. end;
  1263. filedef :
  1264. begin
  1265. { an implicit file conversion is also allowed }
  1266. { from a typed file to an untyped one }
  1267. if (def_from.deftype=filedef) and
  1268. (tfiledef(def_from).filetyp = ft_typed) and
  1269. (tfiledef(def_to).filetyp = ft_untyped) then
  1270. eq:=te_convert_l1;
  1271. end;
  1272. end;
  1273. end;
  1274. procedure para_allowed(var eq:tequaltype;p:tcallparanode;def_to:tdef);
  1275. begin
  1276. { Note: eq must be already valid, it will only be updated! }
  1277. case def_to.deftype of
  1278. formaldef :
  1279. begin
  1280. { all types can be passed to a formaldef }
  1281. eq:=te_equal;
  1282. end;
  1283. stringdef :
  1284. begin
  1285. { to support ansi/long/wide strings in a proper way }
  1286. { string and string[10] are assumed as equal }
  1287. { when searching the correct overloaded procedure }
  1288. if (p.resulttype.def.deftype=stringdef) and
  1289. (tstringdef(def_to).string_typ=tstringdef(p.resulttype.def).string_typ) then
  1290. eq:=te_equal
  1291. else
  1292. { Passing a constant char to ansistring or shortstring or
  1293. a widechar to widestring then handle it as equal. }
  1294. if (p.left.nodetype=ordconstn) and
  1295. (
  1296. is_char(p.resulttype.def) and
  1297. (is_shortstring(def_to) or is_ansistring(def_to))
  1298. ) or
  1299. (
  1300. is_widechar(p.resulttype.def) and
  1301. is_widestring(def_to)
  1302. ) then
  1303. eq:=te_equal
  1304. end;
  1305. setdef :
  1306. begin
  1307. { set can also be a not yet converted array constructor }
  1308. if (p.resulttype.def.deftype=arraydef) and
  1309. is_array_constructor(p.resulttype.def) and
  1310. not is_variant_array(p.resulttype.def) then
  1311. eq:=te_equal;
  1312. end;
  1313. procvardef :
  1314. begin
  1315. { in tp7 mode proc -> procvar is allowed }
  1316. if ((m_tp_procvar in aktmodeswitches) or
  1317. (m_mac_procvar in aktmodeswitches)) and
  1318. (p.left.nodetype=calln) and
  1319. (proc_to_procvar_equal(tprocdef(tcallnode(p.left).procdefinition),tprocvardef(def_to))>=te_equal) then
  1320. eq:=te_equal
  1321. else
  1322. if (m_mac_procvar in aktmodeswitches) and
  1323. is_procvar_load(p.left) then
  1324. eq:=te_convert_l2;
  1325. end;
  1326. end;
  1327. end;
  1328. function allowenumop(nt:tnodetype):boolean;
  1329. begin
  1330. result:=(nt in [equaln,unequaln,ltn,lten,gtn,gten]) or
  1331. ((cs_allow_enum_calc in aktlocalswitches) and
  1332. (nt in [addn,subn]));
  1333. end;
  1334. {****************************************************************************
  1335. TCallCandidates
  1336. ****************************************************************************}
  1337. constructor tcallcandidates.create(sym:tprocsym;st:tsymtable;ppn:tnode;isprop,ignorevis : boolean);
  1338. var
  1339. j : integer;
  1340. pd : tprocdef;
  1341. hp : pcandidate;
  1342. found,
  1343. has_overload_directive : boolean;
  1344. topclassh : tobjectdef;
  1345. srsymtable : tsymtable;
  1346. srprocsym : tprocsym;
  1347. pt : tcallparanode;
  1348. checkstack : psymtablestackitem;
  1349. begin
  1350. if not assigned(sym) then
  1351. internalerror(200411015);
  1352. FProcSym:=sym;
  1353. FProcs:=nil;
  1354. FProccnt:=0;
  1355. FProcvisiblecnt:=0;
  1356. FParanode:=ppn;
  1357. FAllowVariant:=true;
  1358. { determine length of parameter list }
  1359. pt:=tcallparanode(ppn);
  1360. FParalength:=0;
  1361. while assigned(pt) do
  1362. begin
  1363. inc(FParalength);
  1364. pt:=tcallparanode(pt.right);
  1365. end;
  1366. { when the definition has overload directive set, we search for
  1367. overloaded definitions in the class, this only needs to be done once
  1368. for class entries as the tree keeps always the same }
  1369. if (not sym.overloadchecked) and
  1370. (sym.owner.symtabletype=objectsymtable) and
  1371. (po_overload in sym.first_procdef.procoptions) then
  1372. search_class_overloads(sym);
  1373. { when the class passed is defined in this unit we
  1374. need to use the scope of that class. This is a trick
  1375. that can be used to access protected members in other
  1376. units. At least kylix supports it this way (PFV) }
  1377. if assigned(st) and
  1378. (
  1379. (st.symtabletype=objectsymtable) or
  1380. ((st.symtabletype=withsymtable) and
  1381. (st.defowner.deftype=objectdef))
  1382. ) and
  1383. (st.defowner.owner.symtabletype in [globalsymtable,staticsymtable]) and
  1384. st.defowner.owner.iscurrentunit then
  1385. topclassh:=tobjectdef(st.defowner)
  1386. else
  1387. begin
  1388. if assigned(current_procinfo) then
  1389. topclassh:=current_procinfo.procdef._class
  1390. else
  1391. topclassh:=nil;
  1392. end;
  1393. { link all procedures which have the same # of parameters }
  1394. for j:=1 to sym.procdef_count do
  1395. begin
  1396. pd:=sym.procdef[j];
  1397. { Is the procdef visible? This needs to be checked on
  1398. procdef level since a symbol can contain both private and
  1399. public declarations. But the check should not be done
  1400. when the callnode is generated by a property
  1401. inherited overrides invisible anonymous inherited (FK) }
  1402. if isprop or ignorevis or
  1403. (pd.owner.symtabletype<>objectsymtable) or
  1404. pd.is_visible_for_object(topclassh) then
  1405. begin
  1406. { we have at least one procedure that is visible }
  1407. inc(FProcvisiblecnt);
  1408. { only when the # of parameter are supported by the
  1409. procedure }
  1410. if (FParalength>=pd.minparacount) and
  1411. ((po_varargs in pd.procoptions) or { varargs }
  1412. (FParalength<=pd.maxparacount)) then
  1413. proc_add(pd);
  1414. end;
  1415. end;
  1416. { remember if the procedure is declared with the overload directive,
  1417. it's information is still needed also after all procs are removed }
  1418. has_overload_directive:=(po_overload in sym.first_procdef.procoptions);
  1419. { when the definition has overload directive set, we search for
  1420. overloaded definitions in the symtablestack. The found
  1421. entries are only added to the procs list and not the procsym, because
  1422. the list can change in every situation }
  1423. if has_overload_directive and
  1424. (sym.owner.symtabletype<>objectsymtable) then
  1425. begin
  1426. srsymtable:=sym.owner;
  1427. checkstack:=symtablestack.stack;
  1428. while assigned(checkstack) and
  1429. (checkstack^.symtable<>srsymtable) do
  1430. checkstack:=checkstack^.next;
  1431. { we've already processed the current symtable, start with
  1432. the next symtable in the stack }
  1433. if assigned(checkstack) then
  1434. checkstack:=checkstack^.next;
  1435. while assigned(checkstack) do
  1436. begin
  1437. srsymtable:=checkstack^.symtable;
  1438. if srsymtable.symtabletype in [localsymtable,staticsymtable,globalsymtable] then
  1439. begin
  1440. srprocsym:=tprocsym(srsymtable.speedsearch(sym.name,sym.speedvalue));
  1441. if assigned(srprocsym) and
  1442. (srprocsym.typ=procsym) then
  1443. begin
  1444. { if this visible procedure doesn't have overload we can stop
  1445. searching }
  1446. if not(po_overload in srprocsym.first_procdef.procoptions) and
  1447. srprocsym.first_procdef.is_visible_for_object(topclassh) then
  1448. break;
  1449. { process all overloaded definitions }
  1450. for j:=1 to srprocsym.procdef_count do
  1451. begin
  1452. pd:=srprocsym.procdef[j];
  1453. { only visible procedures need to be added }
  1454. if pd.is_visible_for_object(topclassh) then
  1455. begin
  1456. { only when the # of parameter are supported by the
  1457. procedure }
  1458. if (FParalength>=pd.minparacount) and
  1459. ((po_varargs in pd.procoptions) or { varargs }
  1460. (FParalength<=pd.maxparacount)) then
  1461. begin
  1462. found:=false;
  1463. hp:=FProcs;
  1464. while assigned(hp) do
  1465. begin
  1466. { Only compare visible parameters for the user }
  1467. if compare_paras(hp^.data.paras,pd.paras,cp_value_equal_const,[cpo_ignorehidden])>=te_equal then
  1468. begin
  1469. found:=true;
  1470. break;
  1471. end;
  1472. hp:=hp^.next;
  1473. end;
  1474. if not found then
  1475. proc_add(pd);
  1476. end;
  1477. end;
  1478. end;
  1479. end;
  1480. end;
  1481. checkstack:=checkstack^.next;
  1482. end;
  1483. end;
  1484. end;
  1485. constructor tcallcandidates.create_operator(op:ttoken;ppn:tnode);
  1486. var
  1487. j : integer;
  1488. pd : tprocdef;
  1489. hp : pcandidate;
  1490. found : boolean;
  1491. srsymtable : tsymtable;
  1492. srprocsym : tprocsym;
  1493. pt : tcallparanode;
  1494. sv : cardinal;
  1495. checkstack : psymtablestackitem;
  1496. begin
  1497. FProcSym:=nil;
  1498. FProcs:=nil;
  1499. FProccnt:=0;
  1500. FProcvisiblecnt:=0;
  1501. FParanode:=ppn;
  1502. FAllowVariant:=false;
  1503. { determine length of parameter list }
  1504. pt:=tcallparanode(ppn);
  1505. FParalength:=0;
  1506. while assigned(pt) do
  1507. begin
  1508. if pt.resulttype.def.deftype=variantdef then
  1509. FAllowVariant:=true;
  1510. inc(FParalength);
  1511. pt:=tcallparanode(pt.right);
  1512. end;
  1513. { we search all overloaded operator definitions in the symtablestack. The found
  1514. entries are only added to the procs list and not the procsym, because
  1515. the list can change in every situation }
  1516. sv:=getspeedvalue(overloaded_names[op]);
  1517. checkstack:=symtablestack.stack;
  1518. while assigned(checkstack) do
  1519. begin
  1520. srsymtable:=checkstack^.symtable;
  1521. if srsymtable.symtabletype in [localsymtable,staticsymtable,globalsymtable] then
  1522. begin
  1523. srprocsym:=tprocsym(srsymtable.speedsearch(overloaded_names[op],sv));
  1524. if assigned(srprocsym) and
  1525. (srprocsym.typ=procsym) then
  1526. begin
  1527. { Store first procsym found }
  1528. if not assigned(FProcsym) then
  1529. FProcsym:=srprocsym;
  1530. { process all overloaded definitions }
  1531. for j:=1 to srprocsym.procdef_count do
  1532. begin
  1533. pd:=srprocsym.procdef[j];
  1534. { only when the # of parameter are supported by the
  1535. procedure }
  1536. if (FParalength>=pd.minparacount) and
  1537. (FParalength<=pd.maxparacount) then
  1538. begin
  1539. found:=false;
  1540. hp:=FProcs;
  1541. while assigned(hp) do
  1542. begin
  1543. { Only compare visible parameters for the user }
  1544. if compare_paras(hp^.data.paras,pd.paras,cp_value_equal_const,[cpo_ignorehidden])>=te_equal then
  1545. begin
  1546. found:=true;
  1547. break;
  1548. end;
  1549. hp:=hp^.next;
  1550. end;
  1551. if not found then
  1552. proc_add(pd);
  1553. end;
  1554. end;
  1555. end;
  1556. end;
  1557. checkstack:=checkstack^.next;
  1558. end;
  1559. end;
  1560. destructor tcallcandidates.destroy;
  1561. var
  1562. hpnext,
  1563. hp : pcandidate;
  1564. begin
  1565. hp:=FProcs;
  1566. while assigned(hp) do
  1567. begin
  1568. hpnext:=hp^.next;
  1569. dispose(hp);
  1570. hp:=hpnext;
  1571. end;
  1572. end;
  1573. function tcallcandidates.proc_add(pd:tprocdef):pcandidate;
  1574. var
  1575. defaultparacnt : integer;
  1576. begin
  1577. { generate new candidate entry }
  1578. new(result);
  1579. fillchar(result^,sizeof(tcandidate),0);
  1580. result^.data:=pd;
  1581. result^.next:=FProcs;
  1582. FProcs:=result;
  1583. inc(FProccnt);
  1584. { Find last parameter, skip all default parameters
  1585. that are not passed. Ignore this skipping for varargs }
  1586. result^.firstparaidx:=pd.paras.count-1;
  1587. if not(po_varargs in pd.procoptions) then
  1588. begin
  1589. { ignore hidden parameters }
  1590. while (result^.firstparaidx>=0) and (vo_is_hidden_para in tparavarsym(pd.paras[result^.firstparaidx]).varoptions) do
  1591. dec(result^.firstparaidx);
  1592. defaultparacnt:=pd.maxparacount-FParalength;
  1593. if defaultparacnt>0 then
  1594. begin
  1595. if defaultparacnt>result^.firstparaidx+1 then
  1596. internalerror(200401141);
  1597. dec(result^.firstparaidx,defaultparacnt);
  1598. end;
  1599. end;
  1600. end;
  1601. procedure tcallcandidates.list(all:boolean);
  1602. var
  1603. hp : pcandidate;
  1604. begin
  1605. hp:=FProcs;
  1606. while assigned(hp) do
  1607. begin
  1608. if all or
  1609. (not hp^.invalid) then
  1610. MessagePos1(hp^.data.fileinfo,sym_h_param_list,hp^.data.fullprocname(false));
  1611. hp:=hp^.next;
  1612. end;
  1613. end;
  1614. {$ifdef EXTDEBUG}
  1615. procedure tcallcandidates.dump_info(lvl:longint);
  1616. function ParaTreeStr(p:tcallparanode):string;
  1617. begin
  1618. result:='';
  1619. while assigned(p) do
  1620. begin
  1621. if result<>'' then
  1622. result:=','+result;
  1623. result:=p.resulttype.def.typename+result;
  1624. p:=tcallparanode(p.right);
  1625. end;
  1626. end;
  1627. var
  1628. hp : pcandidate;
  1629. i : integer;
  1630. currpara : tparavarsym;
  1631. begin
  1632. if not CheckVerbosity(lvl) then
  1633. exit;
  1634. Comment(lvl+V_LineInfo,'Overloaded callnode: '+FProcSym.name+'('+ParaTreeStr(tcallparanode(FParaNode))+')');
  1635. hp:=FProcs;
  1636. while assigned(hp) do
  1637. begin
  1638. Comment(lvl,' '+hp^.data.fullprocname(false));
  1639. if (hp^.invalid) then
  1640. Comment(lvl,' invalid')
  1641. else
  1642. begin
  1643. Comment(lvl,' ex: '+tostr(hp^.exact_count)+
  1644. ' eq: '+tostr(hp^.equal_count)+
  1645. ' l1: '+tostr(hp^.cl1_count)+
  1646. ' l2: '+tostr(hp^.cl2_count)+
  1647. ' l3: '+tostr(hp^.cl3_count)+
  1648. ' oper: '+tostr(hp^.coper_count)+
  1649. ' ord: '+realtostr(hp^.ordinal_distance));
  1650. { Print parameters in left-right order }
  1651. for i:=0 to hp^.data.paras.count-1 do
  1652. begin
  1653. currpara:=tparavarsym(hp^.data.paras[i]);
  1654. if (vo_is_hidden_para in currpara.varoptions) then
  1655. Comment(lvl,' - '+currpara.vartype.def.typename+' : '+EqualTypeName[currpara.eqval]);
  1656. end;
  1657. end;
  1658. hp:=hp^.next;
  1659. end;
  1660. end;
  1661. {$endif EXTDEBUG}
  1662. procedure tcallcandidates.get_information;
  1663. var
  1664. hp : pcandidate;
  1665. currpara : tparavarsym;
  1666. paraidx : integer;
  1667. currparanr : byte;
  1668. rfh,rth : bestreal;
  1669. objdef : tobjectdef;
  1670. def_from,
  1671. def_to : tdef;
  1672. currpt,
  1673. pt : tcallparanode;
  1674. eq : tequaltype;
  1675. convtype : tconverttype;
  1676. pdtemp,
  1677. pdoper : tprocdef;
  1678. releasecurrpt : boolean;
  1679. cdoptions : tcompare_defs_options;
  1680. begin
  1681. cdoptions:=[cdo_check_operator];
  1682. if FAllowVariant then
  1683. include(cdoptions,cdo_allow_variant);
  1684. { process all procs }
  1685. hp:=FProcs;
  1686. while assigned(hp) do
  1687. begin
  1688. { We compare parameters in reverse order (right to left),
  1689. the firstpara is already pointing to the last parameter
  1690. were we need to start comparing }
  1691. currparanr:=FParalength;
  1692. paraidx:=hp^.firstparaidx;
  1693. while (paraidx>=0) and (vo_is_hidden_para in tparavarsym(hp^.data.paras[paraidx]).varoptions) do
  1694. dec(paraidx);
  1695. pt:=tcallparanode(FParaNode);
  1696. while assigned(pt) and (paraidx>=0) do
  1697. begin
  1698. currpara:=tparavarsym(hp^.data.paras[paraidx]);
  1699. { currpt can be changed from loadn to calln when a procvar
  1700. is passed. This is to prevent that the change is permanent }
  1701. currpt:=pt;
  1702. releasecurrpt:=false;
  1703. { retrieve current parameter definitions to compares }
  1704. eq:=te_incompatible;
  1705. def_from:=currpt.resulttype.def;
  1706. def_to:=currpara.vartype.def;
  1707. if not(assigned(def_from)) then
  1708. internalerror(200212091);
  1709. if not(
  1710. assigned(def_to) or
  1711. ((po_varargs in hp^.data.procoptions) and
  1712. (currparanr>hp^.data.minparacount))
  1713. ) then
  1714. internalerror(200212092);
  1715. { Convert tp procvars when not expecting a procvar }
  1716. if (def_to.deftype<>procvardef) and
  1717. (currpt.left.resulttype.def.deftype=procvardef) then
  1718. begin
  1719. releasecurrpt:=true;
  1720. currpt:=tcallparanode(pt.getcopy);
  1721. if maybe_call_procvar(currpt.left,true) then
  1722. begin
  1723. currpt.resulttype:=currpt.left.resulttype;
  1724. def_from:=currpt.left.resulttype.def;
  1725. end;
  1726. end;
  1727. { If we expect a procvar and the left is loadnode that
  1728. returns a procdef we need to find the correct overloaded
  1729. procdef that matches the expected procvar. The loadnode
  1730. temporary returned the first procdef (PFV) }
  1731. if (def_to.deftype=procvardef) and
  1732. (currpt.left.nodetype=loadn) and
  1733. (currpt.left.resulttype.def.deftype=procdef) then
  1734. begin
  1735. pdtemp:=tprocsym(Tloadnode(currpt.left).symtableentry).search_procdef_byprocvardef(Tprocvardef(def_to));
  1736. if assigned(pdtemp) then
  1737. begin
  1738. tloadnode(currpt.left).procdef:=pdtemp;
  1739. currpt.left.resulttype.setdef(tloadnode(currpt.left).procdef);
  1740. currpt.resulttype:=currpt.left.resulttype;
  1741. def_from:=currpt.left.resulttype.def;
  1742. end;
  1743. end;
  1744. { varargs are always equal, but not exact }
  1745. if (po_varargs in hp^.data.procoptions) and
  1746. (currparanr>hp^.data.minparacount) then
  1747. begin
  1748. eq:=te_equal;
  1749. end
  1750. else
  1751. { same definition -> exact }
  1752. if (def_from=def_to) then
  1753. begin
  1754. eq:=te_exact;
  1755. end
  1756. else
  1757. { for value and const parameters check if a integer is constant or
  1758. included in other integer -> equal and calc ordinal_distance }
  1759. if not(currpara.varspez in [vs_var,vs_out]) and
  1760. is_integer(def_from) and
  1761. is_integer(def_to) and
  1762. is_in_limit(def_from,def_to) then
  1763. begin
  1764. eq:=te_equal;
  1765. hp^.ordinal_distance:=hp^.ordinal_distance+
  1766. abs(bestreal(torddef(def_from).low)-bestreal(torddef(def_to).low));
  1767. if (torddef(def_to).typ=u64bit) then
  1768. rth:=bestreal(qword(torddef(def_to).high))
  1769. else
  1770. rth:=bestreal(torddef(def_to).high);
  1771. if (torddef(def_from).typ=u64bit) then
  1772. rfh:=bestreal(qword(torddef(def_from).high))
  1773. else
  1774. rfh:=bestreal(torddef(def_from).high);
  1775. hp^.ordinal_distance:=hp^.ordinal_distance+abs(rth-rfh);
  1776. { Give wrong sign a small penalty, this is need to get a diffrence
  1777. from word->[longword,longint] }
  1778. if is_signed(def_from)<>is_signed(def_to) then
  1779. hp^.ordinal_distance:=hp^.ordinal_distance+1.0;
  1780. end
  1781. else
  1782. { for value and const parameters check precision of real, give
  1783. penalty for loosing of precision. var and out parameters must match exactly }
  1784. if not(currpara.varspez in [vs_var,vs_out]) and
  1785. is_real(def_from) and
  1786. is_real(def_to) then
  1787. begin
  1788. eq:=te_equal;
  1789. if is_extended(def_to) then
  1790. rth:=bestreal(4)
  1791. else
  1792. if is_double (def_to) then
  1793. rth:=bestreal(2)
  1794. else
  1795. rth:=bestreal(1);
  1796. if is_extended(def_from) then
  1797. rfh:=bestreal(4)
  1798. else
  1799. if is_double (def_from) then
  1800. rfh:=bestreal(2)
  1801. else
  1802. rfh:=bestreal(1);
  1803. { penalty for shrinking of precision }
  1804. if rth<rfh then
  1805. rfh:=(rfh-rth)*16
  1806. else
  1807. rfh:=rth-rfh;
  1808. hp^.ordinal_distance:=hp^.ordinal_distance+rfh;
  1809. end
  1810. else
  1811. { related object parameters also need to determine the distance between the current
  1812. object and the object we are comparing with. var and out parameters must match exactly }
  1813. if not(currpara.varspez in [vs_var,vs_out]) and
  1814. (def_from.deftype=objectdef) and
  1815. (def_to.deftype=objectdef) and
  1816. (tobjectdef(def_from).objecttype=tobjectdef(def_to).objecttype) and
  1817. tobjectdef(def_from).is_related(tobjectdef(def_to)) then
  1818. begin
  1819. eq:=te_convert_l1;
  1820. objdef:=tobjectdef(def_from);
  1821. while assigned(objdef) do
  1822. begin
  1823. if objdef=def_to then
  1824. break;
  1825. hp^.ordinal_distance:=hp^.ordinal_distance+1;
  1826. objdef:=objdef.childof;
  1827. end;
  1828. end
  1829. else
  1830. { generic type comparision }
  1831. begin
  1832. eq:=compare_defs_ext(def_from,def_to,currpt.left.nodetype,convtype,pdoper,cdoptions);
  1833. { when the types are not equal we need to check
  1834. some special case for parameter passing }
  1835. if (eq<te_equal) then
  1836. begin
  1837. if currpara.varspez in [vs_var,vs_out] then
  1838. begin
  1839. { para requires an equal type so the previous found
  1840. match was not good enough, reset to incompatible }
  1841. eq:=te_incompatible;
  1842. { var_para_allowed will return te_equal and te_convert_l1 to
  1843. make a difference for best matching }
  1844. var_para_allowed(eq,currpt.resulttype.def,currpara.vartype.def)
  1845. end
  1846. else
  1847. para_allowed(eq,currpt,def_to);
  1848. end;
  1849. end;
  1850. { when a procvar was changed to a call an exact much is
  1851. downgraded to equal. This way an overload call with the
  1852. procvar is choosen. See tb0471 (PFV) }
  1853. if (pt<>currpt) and (eq=te_exact) then
  1854. eq:=te_equal;
  1855. { increase correct counter }
  1856. case eq of
  1857. te_exact :
  1858. inc(hp^.exact_count);
  1859. te_equal :
  1860. inc(hp^.equal_count);
  1861. te_convert_l1 :
  1862. inc(hp^.cl1_count);
  1863. te_convert_l2 :
  1864. inc(hp^.cl2_count);
  1865. te_convert_l3 :
  1866. inc(hp^.cl3_count);
  1867. te_convert_operator :
  1868. inc(hp^.coper_count);
  1869. te_incompatible :
  1870. hp^.invalid:=true;
  1871. else
  1872. internalerror(200212072);
  1873. end;
  1874. { stop checking when an incompatible parameter is found }
  1875. if hp^.invalid then
  1876. begin
  1877. { store the current parameter info for
  1878. a nice error message when no procedure is found }
  1879. hp^.wrongparaidx:=paraidx;
  1880. hp^.wrongparanr:=currparanr;
  1881. break;
  1882. end;
  1883. {$ifdef EXTDEBUG}
  1884. { store equal in node tree for dump }
  1885. currpara.eqval:=eq;
  1886. {$endif EXTDEBUG}
  1887. { maybe release temp currpt }
  1888. if releasecurrpt then
  1889. currpt.free;
  1890. { next parameter in the call tree }
  1891. pt:=tcallparanode(pt.right);
  1892. { next parameter for definition, only goto next para
  1893. if we're out of the varargs }
  1894. if not(po_varargs in hp^.data.procoptions) or
  1895. (currparanr<=hp^.data.maxparacount) then
  1896. begin
  1897. { Ignore vs_hidden parameters }
  1898. repeat
  1899. dec(paraidx);
  1900. until (paraidx<0) or not(vo_is_hidden_para in tparavarsym(hp^.data.paras[paraidx]).varoptions);
  1901. end;
  1902. dec(currparanr);
  1903. end;
  1904. if not(hp^.invalid) and
  1905. (assigned(pt) or (paraidx>=0) or (currparanr<>0)) then
  1906. internalerror(200212141);
  1907. { next candidate }
  1908. hp:=hp^.next;
  1909. end;
  1910. end;
  1911. function is_better_candidate(currpd,bestpd:pcandidate):integer;
  1912. var
  1913. res : integer;
  1914. begin
  1915. {
  1916. Return values:
  1917. > 0 when currpd is better than bestpd
  1918. < 0 when bestpd is better than currpd
  1919. = 0 when both are equal
  1920. To choose the best candidate we use the following order:
  1921. - Incompatible flag
  1922. - (Smaller) Number of convert operator parameters.
  1923. - (Smaller) Number of convertlevel 2 parameters.
  1924. - (Smaller) Number of convertlevel 1 parameters.
  1925. - (Bigger) Number of exact parameters.
  1926. - (Smaller) Number of equal parameters.
  1927. - (Smaller) Total of ordinal distance. For example, the distance of a word
  1928. to a byte is 65535-255=65280.
  1929. }
  1930. if bestpd^.invalid then
  1931. begin
  1932. if currpd^.invalid then
  1933. res:=0
  1934. else
  1935. res:=1;
  1936. end
  1937. else
  1938. if currpd^.invalid then
  1939. res:=-1
  1940. else
  1941. begin
  1942. { less operator parameters? }
  1943. res:=(bestpd^.coper_count-currpd^.coper_count);
  1944. if (res=0) then
  1945. begin
  1946. { less cl3 parameters? }
  1947. res:=(bestpd^.cl3_count-currpd^.cl3_count);
  1948. if (res=0) then
  1949. begin
  1950. { less cl2 parameters? }
  1951. res:=(bestpd^.cl2_count-currpd^.cl2_count);
  1952. if (res=0) then
  1953. begin
  1954. { less cl1 parameters? }
  1955. res:=(bestpd^.cl1_count-currpd^.cl1_count);
  1956. if (res=0) then
  1957. begin
  1958. { more exact parameters? }
  1959. res:=(currpd^.exact_count-bestpd^.exact_count);
  1960. if (res=0) then
  1961. begin
  1962. { less equal parameters? }
  1963. res:=(bestpd^.equal_count-currpd^.equal_count);
  1964. if (res=0) then
  1965. begin
  1966. { smaller ordinal distance? }
  1967. if (currpd^.ordinal_distance<bestpd^.ordinal_distance) then
  1968. res:=1
  1969. else
  1970. if (currpd^.ordinal_distance>bestpd^.ordinal_distance) then
  1971. res:=-1
  1972. else
  1973. res:=0;
  1974. end;
  1975. end;
  1976. end;
  1977. end;
  1978. end;
  1979. end;
  1980. end;
  1981. is_better_candidate:=res;
  1982. end;
  1983. function tcallcandidates.choose_best(var bestpd:tabstractprocdef):integer;
  1984. var
  1985. besthpstart,
  1986. hp : pcandidate;
  1987. cntpd,
  1988. res : integer;
  1989. begin
  1990. {
  1991. Returns the number of candidates left and the
  1992. first candidate is returned in pdbest
  1993. }
  1994. { Setup the first procdef as best, only count it as a result
  1995. when it is valid }
  1996. bestpd:=FProcs^.data;
  1997. if FProcs^.invalid then
  1998. cntpd:=0
  1999. else
  2000. cntpd:=1;
  2001. if assigned(FProcs^.next) then
  2002. begin
  2003. besthpstart:=FProcs;
  2004. hp:=FProcs^.next;
  2005. while assigned(hp) do
  2006. begin
  2007. res:=is_better_candidate(hp,besthpstart);
  2008. if (res>0) then
  2009. begin
  2010. { hp is better, flag all procs to be incompatible }
  2011. while (besthpstart<>hp) do
  2012. begin
  2013. besthpstart^.invalid:=true;
  2014. besthpstart:=besthpstart^.next;
  2015. end;
  2016. { besthpstart is already set to hp }
  2017. bestpd:=besthpstart^.data;
  2018. cntpd:=1;
  2019. end
  2020. else
  2021. if (res<0) then
  2022. begin
  2023. { besthpstart is better, flag current hp to be incompatible }
  2024. hp^.invalid:=true;
  2025. end
  2026. else
  2027. begin
  2028. { res=0, both are valid }
  2029. if not hp^.invalid then
  2030. inc(cntpd);
  2031. end;
  2032. hp:=hp^.next;
  2033. end;
  2034. end;
  2035. result:=cntpd;
  2036. end;
  2037. procedure tcallcandidates.find_wrong_para;
  2038. var
  2039. currparanr : smallint;
  2040. hp : pcandidate;
  2041. pt : tcallparanode;
  2042. wrongpara : tparavarsym;
  2043. begin
  2044. { Only process the first overloaded procdef }
  2045. hp:=FProcs;
  2046. { Find callparanode corresponding to the argument }
  2047. pt:=tcallparanode(FParanode);
  2048. currparanr:=FParalength;
  2049. while assigned(pt) and
  2050. (currparanr>hp^.wrongparanr) do
  2051. begin
  2052. pt:=tcallparanode(pt.right);
  2053. dec(currparanr);
  2054. end;
  2055. if (currparanr<>hp^.wrongparanr) or
  2056. not assigned(pt) then
  2057. internalerror(200212094);
  2058. { Show error message, when it was a var or out parameter
  2059. guess that it is a missing typeconv }
  2060. wrongpara:=tparavarsym(hp^.data.paras[hp^.wrongparaidx]);
  2061. if wrongpara.varspez in [vs_var,vs_out] then
  2062. begin
  2063. { Maybe passing the correct type but passing a const to var parameter }
  2064. if (compare_defs(pt.resulttype.def,wrongpara.vartype.def,pt.nodetype)<>te_incompatible) and
  2065. not valid_for_var(pt.left,true) then
  2066. CGMessagePos(pt.left.fileinfo,type_e_variable_id_expected)
  2067. else
  2068. CGMessagePos2(pt.left.fileinfo,parser_e_call_by_ref_without_typeconv,
  2069. FullTypeName(pt.left.resulttype.def,wrongpara.vartype.def),
  2070. FullTypeName(wrongpara.vartype.def,pt.left.resulttype.def))
  2071. end
  2072. else
  2073. CGMessagePos3(pt.left.fileinfo,type_e_wrong_parameter_type,tostr(hp^.wrongparanr),
  2074. FullTypeName(pt.left.resulttype.def,wrongpara.vartype.def),
  2075. FullTypeName(wrongpara.vartype.def,pt.left.resulttype.def));
  2076. end;
  2077. procedure check_hints(const srsym: tsym; const symoptions: tsymoptions);
  2078. begin
  2079. if not assigned(srsym) then
  2080. internalerror(200602051);
  2081. if sp_hint_deprecated in symoptions then
  2082. Message1(sym_w_deprecated_symbol,srsym.realname);
  2083. if sp_hint_platform in symoptions then
  2084. Message1(sym_w_non_portable_symbol,srsym.realname);
  2085. if sp_hint_unimplemented in symoptions then
  2086. Message1(sym_w_non_implemented_symbol,srsym.realname);
  2087. end;
  2088. procedure check_ranges(const location: tfileposinfo; source: tnode; destdef: tdef);
  2089. begin
  2090. { check if the assignment may cause a range check error }
  2091. { if its not explicit, and only if the values are }
  2092. { ordinals, enumdef and floatdef }
  2093. if assigned(destdef) and
  2094. (destdef.deftype in [enumdef,orddef,floatdef]) and
  2095. not is_boolean(destdef) and
  2096. assigned(source.resulttype.def) and
  2097. (source.resulttype.def.deftype in [enumdef,orddef,floatdef]) and
  2098. not is_boolean(source.resulttype.def) and
  2099. not is_constrealnode(source) then
  2100. begin
  2101. if (destdef.size < source.resulttype.def.size) then
  2102. begin
  2103. if (cs_check_range in aktlocalswitches) then
  2104. MessagePos(location,type_w_smaller_possible_range_check)
  2105. else
  2106. MessagePos(location,type_h_smaller_possible_range_check);
  2107. end;
  2108. end;
  2109. end;
  2110. end.