htypechk.pas 87 KB

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