htypechk.pas 87 KB

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