htypechk.pas 96 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544
  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; singlevariant: boolean):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,cclasses,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.typ of
  163. formaldef,
  164. recorddef,
  165. variantdef :
  166. begin
  167. allowed:=true;
  168. end;
  169. procvardef :
  170. begin
  171. if (rd.typ 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.typ 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.typ=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 current_settings.localswitches) and
  203. is_mmx_able_array(ld)) or
  204. ((cs_support_vectors in current_settings.globalswitches) 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.typ 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.typ 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.typ in [orddef,enumdef,floatdef]) then
  292. exit;
  293. {$ifdef SUPPORT_MMX}
  294. if (cs_mmx in current_settings.localswitches) 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.typ in [orddef,enumdef,floatdef]) then
  303. exit;
  304. {$ifdef SUPPORT_MMX}
  305. if (cs_mmx in current_settings.localswitches) 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.SymList.count of
  323. 1 : begin
  324. ld:=tparavarsym(pf.parast.SymList[0]).vardef;
  325. { assignment is a special case }
  326. if optoken=_ASSIGNMENT then
  327. begin
  328. eq:=compare_defs_ext(ld,pf.returndef,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.SymList[0]).vardef;
  348. rd:=tparavarsym(pf.parast.SymList[1]).vardef;
  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.resultdef;
  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,false);
  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.pass_typecheck }
  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.resultdef;
  450. rd:=tbinarynode(t).right.resultdef;
  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,false);
  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.pass_typecheck }
  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).resultdef.typ = 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 [staticvarsym,localvarsym,paravarsym]) and
  588. (tabstractvarsym(tloadnode(p).symtableentry).varregable <> vr_none) and
  589. ((not records_only) or
  590. (tabstractvarsym(tloadnode(p).symtableentry).vardef.typ = 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^.typedef.typ = 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 current_settings.modeswitches) or
  666. (m_mac_procvar in current_settings.modeswitches)) and
  667. (p.nodetype=typeconvn) and
  668. is_voidpointer(p.resultdef) 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.typ=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_referred_not_inited,vs_written,vs_readwritten),
  685. { vs_declared -> ... }
  686. (vs_none,vs_declared,vs_initialised,vs_read,vs_read_not_warned,vs_referred_not_inited,vs_written,vs_readwritten),
  687. { vs_initialised -> ... }
  688. (vs_none,vs_initialised,vs_initialised,vs_read,vs_read,vs_read,vs_written,vs_readwritten),
  689. { vs_read -> ... }
  690. (vs_none,vs_read,vs_read,vs_read,vs_read,vs_read,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_read_not_warned,vs_readwritten,vs_readwritten),
  693. { vs_referred_not_inited }
  694. (vs_none,vs_referred_not_inited,vs_read,vs_read,vs_read,vs_referred_not_inited,vs_readwritten,vs_readwritten),
  695. { vs_written -> ... }
  696. (vs_none,vs_written,vs_written,vs_readwritten,vs_readwritten,vs_written,vs_written,vs_readwritten),
  697. { vs_readwritten -> ... }
  698. (vs_none,vs_readwritten,vs_readwritten,vs_readwritten,vs_readwritten,vs_readwritten,vs_readwritten,vs_readwritten));
  699. var
  700. hsym : tabstractvarsym;
  701. begin
  702. { make sure we can still warn about uninitialised use after high(v), @v etc }
  703. if (newstate = vs_read) and
  704. not(vsf_must_be_valid in varstateflags) then
  705. newstate := vs_referred_not_inited;
  706. while assigned(p) do
  707. begin
  708. case p.nodetype of
  709. typeconvn :
  710. begin
  711. case ttypeconvnode(p).convtype of
  712. tc_cchar_2_pchar,
  713. tc_cstring_2_pchar,
  714. tc_array_2_pointer :
  715. exclude(varstateflags,vsf_must_be_valid);
  716. tc_pchar_2_string,
  717. tc_pointer_2_array :
  718. include(varstateflags,vsf_must_be_valid);
  719. end;
  720. p:=tunarynode(p).left;
  721. end;
  722. subscriptn :
  723. begin
  724. if is_class_or_interface(tunarynode(p).left.resultdef) then
  725. newstate := vs_read;
  726. p:=tunarynode(p).left;
  727. end;
  728. vecn:
  729. begin
  730. set_varstate(tbinarynode(p).right,vs_read,[vsf_must_be_valid]);
  731. if (newstate in [vs_read,vs_readwritten]) or
  732. not(tunarynode(p).left.resultdef.typ in [stringdef,arraydef]) then
  733. include(varstateflags,vsf_must_be_valid)
  734. else if (newstate = vs_written) then
  735. exclude(varstateflags,vsf_must_be_valid);
  736. p:=tunarynode(p).left;
  737. end;
  738. { do not parse calln }
  739. calln :
  740. break;
  741. loadn :
  742. begin
  743. if (tloadnode(p).symtableentry.typ in [localvarsym,paravarsym,staticvarsym]) then
  744. begin
  745. hsym:=tabstractvarsym(tloadnode(p).symtableentry);
  746. if (vsf_must_be_valid in varstateflags) and
  747. (hsym.varstate in [vs_declared,vs_read_not_warned,vs_referred_not_inited]) then
  748. begin
  749. { Give warning/note for uninitialized locals }
  750. if assigned(hsym.owner) and
  751. not(vo_is_external in hsym.varoptions) and
  752. (hsym.owner.symtabletype in [parasymtable,localsymtable,staticsymtable]) and
  753. ((hsym.owner=current_procinfo.procdef.localst) or
  754. (hsym.owner=current_procinfo.procdef.parast)) then
  755. begin
  756. if (vo_is_funcret in hsym.varoptions) then
  757. begin
  758. if (vsf_use_hints in varstateflags) then
  759. CGMessage(sym_h_function_result_uninitialized)
  760. else
  761. CGMessage(sym_w_function_result_uninitialized)
  762. end
  763. else
  764. begin
  765. if tloadnode(p).symtable.symtabletype=localsymtable then
  766. begin
  767. if (vsf_use_hints in varstateflags) then
  768. CGMessage1(sym_h_uninitialized_local_variable,hsym.realname)
  769. else
  770. CGMessage1(sym_w_uninitialized_local_variable,hsym.realname);
  771. end
  772. else
  773. begin
  774. if (vsf_use_hints in varstateflags) then
  775. CGMessage1(sym_h_uninitialized_variable,hsym.realname)
  776. else
  777. CGMessage1(sym_w_uninitialized_variable,hsym.realname);
  778. end;
  779. end;
  780. end
  781. else if (newstate = vs_read) then
  782. newstate := vs_read_not_warned;
  783. end;
  784. hsym.varstate := vstrans[hsym.varstate,newstate];
  785. end;
  786. break;
  787. end;
  788. callparan :
  789. internalerror(200310081);
  790. else
  791. break;
  792. end;{case }
  793. end;
  794. end;
  795. procedure set_unique(p : tnode);
  796. begin
  797. while assigned(p) do
  798. begin
  799. case p.nodetype of
  800. vecn:
  801. begin
  802. include(p.flags,nf_callunique);
  803. break;
  804. end;
  805. typeconvn,
  806. subscriptn,
  807. derefn:
  808. p:=tunarynode(p).left;
  809. else
  810. break;
  811. end;
  812. end;
  813. end;
  814. function valid_for_assign(p:tnode;opts:TValidAssigns; report_errors: boolean):boolean;
  815. var
  816. hp2,
  817. hp : tnode;
  818. gotstring,
  819. gotsubscript,
  820. gotrecord,
  821. gotpointer,
  822. gotvec,
  823. gotclass,
  824. gotdynarray,
  825. gotderef : boolean;
  826. fromdef,
  827. todef : tdef;
  828. errmsg : longint;
  829. begin
  830. if valid_const in opts then
  831. errmsg:=type_e_variable_id_expected
  832. else
  833. errmsg:=type_e_argument_cant_be_assigned;
  834. result:=false;
  835. gotsubscript:=false;
  836. gotvec:=false;
  837. gotderef:=false;
  838. gotrecord:=false;
  839. gotclass:=false;
  840. gotpointer:=false;
  841. gotdynarray:=false;
  842. gotstring:=false;
  843. hp:=p;
  844. if not(valid_void in opts) and
  845. is_void(hp.resultdef) then
  846. begin
  847. if report_errors then
  848. CGMessagePos(hp.fileinfo,errmsg);
  849. exit;
  850. end;
  851. while assigned(hp) do
  852. begin
  853. { property allowed? calln has a property check itself }
  854. if (nf_isproperty in hp.flags) then
  855. begin
  856. if (hp.nodetype=calln) then
  857. begin
  858. { check return type }
  859. case hp.resultdef.typ of
  860. pointerdef :
  861. gotpointer:=true;
  862. objectdef :
  863. gotclass:=is_class_or_interface(hp.resultdef);
  864. recorddef :
  865. gotrecord:=true;
  866. classrefdef :
  867. gotclass:=true;
  868. stringdef :
  869. gotstring:=true;
  870. end;
  871. if (valid_property in opts) then
  872. begin
  873. { don't allow writing to calls that will create
  874. temps like calls that return a structure and we
  875. are assigning to a member }
  876. if (valid_const in opts) or
  877. not(
  878. (gotsubscript and gotrecord) or
  879. (gotstring and gotvec)
  880. ) then
  881. result:=true
  882. else
  883. if report_errors then
  884. CGMessagePos(hp.fileinfo,errmsg);
  885. end
  886. else
  887. begin
  888. { 1. if it returns a pointer and we've found a deref,
  889. 2. if it returns a class or record and a subscription or with is found
  890. 3. if the address is needed of a field (subscriptn) }
  891. if (gotpointer and gotderef) or
  892. (gotstring and gotvec) or
  893. (
  894. (gotclass or gotrecord) and
  895. (gotsubscript)
  896. ) or
  897. (
  898. (gotvec and gotdynarray)
  899. ) or
  900. (
  901. (Valid_Addr in opts) and
  902. (hp.nodetype=subscriptn)
  903. ) then
  904. result:=true
  905. else
  906. if report_errors then
  907. CGMessagePos(hp.fileinfo,errmsg);
  908. end;
  909. end
  910. else
  911. result:=true;
  912. exit;
  913. end;
  914. if (Valid_Const in opts) and is_constnode(hp) then
  915. begin
  916. result:=true;
  917. exit;
  918. end;
  919. case hp.nodetype of
  920. temprefn :
  921. begin
  922. valid_for_assign := true;
  923. exit;
  924. end;
  925. derefn :
  926. begin
  927. gotderef:=true;
  928. hp:=tderefnode(hp).left;
  929. end;
  930. typeconvn :
  931. begin
  932. { typecast sizes must match, exceptions:
  933. - implicit typecast made by absolute
  934. - from formaldef
  935. - from void
  936. - from/to open array
  937. - typecast from pointer to array }
  938. fromdef:=ttypeconvnode(hp).left.resultdef;
  939. todef:=hp.resultdef;
  940. if not((nf_absolute in ttypeconvnode(hp).flags) or
  941. (fromdef.typ=formaldef) or
  942. is_void(fromdef) or
  943. is_open_array(fromdef) or
  944. is_open_array(todef) or
  945. ((fromdef.typ=pointerdef) and (todef.typ=arraydef)) or
  946. ((fromdef.typ = objectdef) and (todef.typ = objectdef) and
  947. (tobjectdef(fromdef).is_related(tobjectdef(todef))))) and
  948. (fromdef.size<>todef.size) then
  949. begin
  950. { in TP it is allowed to typecast to smaller types. But the variable can't
  951. be in a register }
  952. if (m_tp7 in current_settings.modeswitches) or
  953. (todef.size<fromdef.size) then
  954. make_not_regable(hp,vr_addr)
  955. else
  956. if report_errors then
  957. CGMessagePos2(hp.fileinfo,type_e_typecast_wrong_size_for_assignment,tostr(fromdef.size),tostr(todef.size));
  958. end;
  959. { don't allow assignments to typeconvs that need special code }
  960. if not(gotsubscript or gotvec or gotderef) and
  961. not(ttypeconvnode(hp).assign_allowed) then
  962. begin
  963. if report_errors then
  964. CGMessagePos(hp.fileinfo,errmsg);
  965. exit;
  966. end;
  967. case hp.resultdef.typ of
  968. pointerdef :
  969. gotpointer:=true;
  970. objectdef :
  971. gotclass:=is_class_or_interface(hp.resultdef);
  972. classrefdef :
  973. gotclass:=true;
  974. arraydef :
  975. begin
  976. { pointer -> array conversion is done then we need to see it
  977. as a deref, because a ^ is then not required anymore }
  978. if (ttypeconvnode(hp).left.resultdef.typ=pointerdef) then
  979. gotderef:=true;
  980. end;
  981. end;
  982. hp:=ttypeconvnode(hp).left;
  983. end;
  984. vecn :
  985. begin
  986. if { only check for first (= outermost) vec node }
  987. not gotvec and
  988. not(valid_packed in opts) and
  989. (tvecnode(hp).left.resultdef.typ = arraydef) and
  990. (ado_IsBitPacked in tarraydef(tvecnode(hp).left.resultdef).arrayoptions) and
  991. (tarraydef(tvecnode(hp).left.resultdef).elepackedbitsize mod 8 <> 0) then
  992. begin
  993. if report_errors then
  994. if (valid_property in opts) then
  995. CGMessagePos(hp.fileinfo,parser_e_packed_element_no_loop)
  996. else
  997. CGMessagePos(hp.fileinfo,parser_e_packed_element_no_var_addr);
  998. exit;
  999. end;
  1000. gotvec:=true;
  1001. { accesses to dyn. arrays override read only access in delphi }
  1002. if (m_delphi in current_settings.modeswitches) and is_dynamic_array(tunarynode(hp).left.resultdef) then
  1003. gotdynarray:=true;
  1004. hp:=tunarynode(hp).left;
  1005. end;
  1006. blockn :
  1007. begin
  1008. hp2:=tblocknode(hp).statements;
  1009. if assigned(hp2) then
  1010. begin
  1011. if hp2.nodetype<>statementn then
  1012. internalerror(2006110801);
  1013. while assigned(tstatementnode(hp2).next) do
  1014. hp2:=tstatementnode(hp2).next;
  1015. hp:=tstatementnode(hp2).statement;
  1016. end
  1017. else
  1018. begin
  1019. if report_errors then
  1020. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  1021. exit;
  1022. end;
  1023. end;
  1024. asn :
  1025. begin
  1026. { asn can't be assigned directly, it returns the value in a register instead
  1027. of reference. }
  1028. if not(gotsubscript or gotderef or gotvec) then
  1029. begin
  1030. if report_errors then
  1031. CGMessagePos(hp.fileinfo,errmsg);
  1032. exit;
  1033. end;
  1034. hp:=tunarynode(hp).left;
  1035. end;
  1036. subscriptn :
  1037. begin
  1038. { only check first (= outermost) subscriptn }
  1039. if not gotsubscript and
  1040. not(valid_packed in opts) and
  1041. is_packed_record_or_object(tsubscriptnode(hp).left.resultdef) then
  1042. begin
  1043. if report_errors then
  1044. if (valid_property in opts) then
  1045. CGMessagePos(hp.fileinfo,parser_e_packed_element_no_loop)
  1046. else
  1047. CGMessagePos(hp.fileinfo,parser_e_packed_element_no_var_addr);
  1048. exit;
  1049. end;
  1050. gotsubscript:=true;
  1051. { loop counter? }
  1052. if not(Valid_Const in opts) and
  1053. (vo_is_loop_counter in tsubscriptnode(hp).vs.varoptions) then
  1054. begin
  1055. if report_errors then
  1056. CGMessage1(parser_e_illegal_assignment_to_count_var,tsubscriptnode(hp).vs.realname)
  1057. else
  1058. exit;
  1059. end;
  1060. { a class/interface access is an implicit }
  1061. { dereferencing }
  1062. hp:=tsubscriptnode(hp).left;
  1063. if is_class_or_interface(hp.resultdef) then
  1064. gotderef:=true;
  1065. end;
  1066. muln,
  1067. divn,
  1068. andn,
  1069. xorn,
  1070. orn,
  1071. notn,
  1072. subn,
  1073. addn :
  1074. begin
  1075. { Allow operators on a pointer, or an integer
  1076. and a pointer typecast and deref has been found }
  1077. if ((hp.resultdef.typ=pointerdef) or
  1078. (is_integer(hp.resultdef) and gotpointer)) and
  1079. gotderef then
  1080. result:=true
  1081. else
  1082. { Temp strings are stored in memory, for compatibility with
  1083. delphi only }
  1084. if (m_delphi in current_settings.modeswitches) and
  1085. ((valid_addr in opts) or
  1086. (valid_const in opts)) and
  1087. (hp.resultdef.typ=stringdef) then
  1088. result:=true
  1089. else
  1090. if report_errors then
  1091. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  1092. exit;
  1093. end;
  1094. niln,
  1095. pointerconstn :
  1096. begin
  1097. { to support e.g. @tmypointer(0)^.data; see tests/tbs/tb0481 }
  1098. if gotderef then
  1099. result:=true
  1100. else
  1101. if report_errors then
  1102. CGMessagePos(hp.fileinfo,type_e_no_assign_to_addr);
  1103. exit;
  1104. end;
  1105. addrn :
  1106. begin
  1107. if gotderef then
  1108. result:=true
  1109. else
  1110. if report_errors then
  1111. CGMessagePos(hp.fileinfo,type_e_no_assign_to_addr);
  1112. exit;
  1113. end;
  1114. calln :
  1115. begin
  1116. { check return type }
  1117. case hp.resultdef.typ of
  1118. arraydef :
  1119. begin
  1120. { dynamic arrays are allowed when there is also a
  1121. vec node }
  1122. if is_dynamic_array(hp.resultdef) and
  1123. gotvec then
  1124. begin
  1125. gotderef:=true;
  1126. gotpointer:=true;
  1127. end;
  1128. end;
  1129. pointerdef :
  1130. gotpointer:=true;
  1131. objectdef :
  1132. gotclass:=is_class_or_interface(hp.resultdef);
  1133. recorddef, { handle record like class it needs a subscription }
  1134. classrefdef :
  1135. gotclass:=true;
  1136. stringdef :
  1137. gotstring:=true;
  1138. end;
  1139. { 1. if it returns a pointer and we've found a deref,
  1140. 2. if it returns a class or record and a subscription or with is found
  1141. 3. string is returned }
  1142. if (gotstring and gotvec) or
  1143. (gotpointer and gotderef) or
  1144. (gotclass and gotsubscript) then
  1145. result:=true
  1146. else
  1147. { Temp strings are stored in memory, for compatibility with
  1148. delphi only }
  1149. if (m_delphi in current_settings.modeswitches) and
  1150. (valid_addr in opts) and
  1151. (hp.resultdef.typ=stringdef) then
  1152. result:=true
  1153. else
  1154. if ([valid_const,valid_addr] * opts = [valid_const]) then
  1155. result:=true
  1156. else
  1157. if report_errors then
  1158. CGMessagePos(hp.fileinfo,errmsg);
  1159. exit;
  1160. end;
  1161. inlinen :
  1162. begin
  1163. if ((valid_const in opts) and
  1164. (tinlinenode(hp).inlinenumber in [in_typeof_x]))
  1165. {$ifdef SUPPORT_UNALIGNED}
  1166. or (tinlinenode(hp).inlinenumber in [in_unaligned_x])
  1167. {$endif SUPPORT_UNALIGNED}
  1168. then
  1169. result:=true
  1170. else
  1171. if report_errors then
  1172. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  1173. exit;
  1174. end;
  1175. dataconstn:
  1176. begin
  1177. { only created internally, so no additional checks necessary }
  1178. result:=true;
  1179. exit;
  1180. end;
  1181. loadn :
  1182. begin
  1183. case tloadnode(hp).symtableentry.typ of
  1184. absolutevarsym,
  1185. staticvarsym,
  1186. localvarsym,
  1187. paravarsym :
  1188. begin
  1189. { loop counter? }
  1190. if not(Valid_Const in opts) and
  1191. not gotderef and
  1192. (vo_is_loop_counter in tabstractvarsym(tloadnode(hp).symtableentry).varoptions) then
  1193. if report_errors then
  1194. CGMessage1(parser_e_illegal_assignment_to_count_var,tloadnode(hp).symtableentry.realname)
  1195. else
  1196. exit;
  1197. { read-only variable? }
  1198. if (tabstractvarsym(tloadnode(hp).symtableentry).varspez=vs_const) then
  1199. begin
  1200. { allow p^:= constructions with p is const parameter }
  1201. if gotderef or gotdynarray or (Valid_Const in opts) or
  1202. (nf_isinternal_ignoreconst in tloadnode(hp).flags) then
  1203. result:=true
  1204. else
  1205. if report_errors then
  1206. CGMessagePos(tloadnode(hp).fileinfo,type_e_no_assign_to_const);
  1207. exit;
  1208. end;
  1209. result:=true;
  1210. exit;
  1211. end;
  1212. procsym :
  1213. begin
  1214. if (Valid_Const in opts) then
  1215. result:=true
  1216. else
  1217. if report_errors then
  1218. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  1219. exit;
  1220. end;
  1221. labelsym :
  1222. begin
  1223. if (Valid_Addr in opts) then
  1224. result:=true
  1225. else
  1226. if report_errors then
  1227. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  1228. exit;
  1229. end;
  1230. constsym:
  1231. begin
  1232. if (tconstsym(tloadnode(hp).symtableentry).consttyp=constresourcestring) and
  1233. (valid_addr in opts) then
  1234. result:=true
  1235. else
  1236. if report_errors then
  1237. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  1238. exit;
  1239. end;
  1240. else
  1241. begin
  1242. if report_errors then
  1243. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  1244. exit;
  1245. end;
  1246. end;
  1247. end;
  1248. else
  1249. begin
  1250. if report_errors then
  1251. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  1252. exit;
  1253. end;
  1254. end;
  1255. end;
  1256. end;
  1257. function valid_for_var(p:tnode; report_errors: boolean):boolean;
  1258. begin
  1259. valid_for_var:=valid_for_assign(p,[],report_errors);
  1260. end;
  1261. function valid_for_formal_var(p : tnode; report_errors: boolean) : boolean;
  1262. begin
  1263. valid_for_formal_var:=valid_for_assign(p,[valid_void],report_errors);
  1264. end;
  1265. function valid_for_formal_const(p : tnode; report_errors: boolean) : boolean;
  1266. begin
  1267. valid_for_formal_const:=(p.resultdef.typ=formaldef) or
  1268. valid_for_assign(p,[valid_void,valid_const,valid_property],report_errors);
  1269. end;
  1270. function valid_for_assignment(p:tnode; report_errors: boolean):boolean;
  1271. begin
  1272. valid_for_assignment:=valid_for_assign(p,[valid_property,valid_packed],report_errors);
  1273. end;
  1274. function valid_for_loopvar(p:tnode; report_errors: boolean):boolean;
  1275. begin
  1276. valid_for_loopvar:=valid_for_assign(p,[valid_property],report_errors);
  1277. end;
  1278. function valid_for_addr(p : tnode; report_errors: boolean) : boolean;
  1279. begin
  1280. result:=valid_for_assign(p,[valid_const,valid_addr,valid_void],report_errors);
  1281. end;
  1282. procedure var_para_allowed(var eq:tequaltype;def_from,def_to:Tdef);
  1283. begin
  1284. { Note: eq must be already valid, it will only be updated! }
  1285. case def_to.typ of
  1286. formaldef :
  1287. begin
  1288. { all types can be passed to a formaldef,
  1289. but it is not the prefered way }
  1290. eq:=te_convert_l2;
  1291. end;
  1292. orddef :
  1293. begin
  1294. { allows conversion from word to integer and
  1295. byte to shortint, but only for TP7 compatibility }
  1296. if (m_tp7 in current_settings.modeswitches) and
  1297. (def_from.typ=orddef) and
  1298. (def_from.size=def_to.size) then
  1299. eq:=te_convert_l1;
  1300. end;
  1301. arraydef :
  1302. begin
  1303. if is_open_array(def_to) then
  1304. begin
  1305. if is_dynamic_array(def_from) and
  1306. equal_defs(tarraydef(def_from).elementdef,tarraydef(def_to).elementdef) then
  1307. eq:=te_convert_l2
  1308. else
  1309. if equal_defs(def_from,tarraydef(def_to).elementdef) then
  1310. eq:=te_convert_l2;
  1311. end;
  1312. end;
  1313. pointerdef :
  1314. begin
  1315. { an implicit pointer conversion is allowed }
  1316. if (def_from.typ=pointerdef) then
  1317. eq:=te_convert_l1;
  1318. end;
  1319. stringdef :
  1320. begin
  1321. { all shortstrings are allowed, size is not important }
  1322. if is_shortstring(def_from) and
  1323. is_shortstring(def_to) then
  1324. eq:=te_equal;
  1325. end;
  1326. objectdef :
  1327. begin
  1328. { child objects can be also passed }
  1329. { in non-delphi mode, otherwise }
  1330. { they must match exactly, except }
  1331. { if they are objects }
  1332. if (def_from.typ=objectdef) and
  1333. (
  1334. not(m_delphi in current_settings.modeswitches) or
  1335. (
  1336. (tobjectdef(def_from).objecttype=odt_object) and
  1337. (tobjectdef(def_to).objecttype=odt_object)
  1338. )
  1339. ) and
  1340. (tobjectdef(def_from).is_related(tobjectdef(def_to))) then
  1341. eq:=te_convert_l1;
  1342. end;
  1343. filedef :
  1344. begin
  1345. { an implicit file conversion is also allowed }
  1346. { from a typed file to an untyped one }
  1347. if (def_from.typ=filedef) and
  1348. (tfiledef(def_from).filetyp = ft_typed) and
  1349. (tfiledef(def_to).filetyp = ft_untyped) then
  1350. eq:=te_convert_l1;
  1351. end;
  1352. end;
  1353. end;
  1354. procedure para_allowed(var eq:tequaltype;p:tcallparanode;def_to:tdef);
  1355. begin
  1356. { Note: eq must be already valid, it will only be updated! }
  1357. case def_to.typ of
  1358. formaldef :
  1359. begin
  1360. { all types can be passed to a formaldef }
  1361. eq:=te_equal;
  1362. end;
  1363. stringdef :
  1364. begin
  1365. { to support ansi/long/wide strings in a proper way }
  1366. { string and string[10] are assumed as equal }
  1367. { when searching the correct overloaded procedure }
  1368. if (p.resultdef.typ=stringdef) and
  1369. (tstringdef(def_to).stringtype=tstringdef(p.resultdef).stringtype) then
  1370. eq:=te_equal
  1371. else
  1372. { Passing a constant char to ansistring or shortstring or
  1373. a widechar to widestring then handle it as equal. }
  1374. if (p.left.nodetype=ordconstn) and
  1375. (
  1376. is_char(p.resultdef) and
  1377. (is_shortstring(def_to) or is_ansistring(def_to))
  1378. ) or
  1379. (
  1380. is_widechar(p.resultdef) and
  1381. is_widestring(def_to)
  1382. ) then
  1383. eq:=te_equal
  1384. end;
  1385. setdef :
  1386. begin
  1387. { set can also be a not yet converted array constructor }
  1388. if (p.resultdef.typ=arraydef) and
  1389. is_array_constructor(p.resultdef) and
  1390. not is_variant_array(p.resultdef) then
  1391. eq:=te_equal;
  1392. end;
  1393. procvardef :
  1394. begin
  1395. { in tp7 mode proc -> procvar is allowed }
  1396. if ((m_tp_procvar in current_settings.modeswitches) or
  1397. (m_mac_procvar in current_settings.modeswitches)) and
  1398. (p.left.nodetype=calln) and
  1399. (proc_to_procvar_equal(tprocdef(tcallnode(p.left).procdefinition),tprocvardef(def_to))>=te_equal) then
  1400. eq:=te_equal
  1401. else
  1402. if (m_mac_procvar in current_settings.modeswitches) and
  1403. is_procvar_load(p.left) then
  1404. eq:=te_convert_l2;
  1405. end;
  1406. end;
  1407. end;
  1408. function allowenumop(nt:tnodetype):boolean;
  1409. begin
  1410. result:=(nt in [equaln,unequaln,ltn,lten,gtn,gten]) or
  1411. ((cs_allow_enum_calc in current_settings.localswitches) and
  1412. (nt in [addn,subn]));
  1413. end;
  1414. {****************************************************************************
  1415. TCallCandidates
  1416. ****************************************************************************}
  1417. constructor tcallcandidates.create(sym:tprocsym;st:TSymtable;ppn:tnode;isprop,ignorevis : boolean);
  1418. var
  1419. j : integer;
  1420. pd : tprocdef;
  1421. hp : pcandidate;
  1422. found,
  1423. has_overload_directive : boolean;
  1424. topclassh : tobjectdef;
  1425. srsymtable : TSymtable;
  1426. srprocsym : tprocsym;
  1427. pt : tcallparanode;
  1428. checkstack : psymtablestackitem;
  1429. hashedid : THashedIDString;
  1430. begin
  1431. if not assigned(sym) then
  1432. internalerror(200411015);
  1433. FProcSym:=sym;
  1434. FProcs:=nil;
  1435. FProccnt:=0;
  1436. FProcvisiblecnt:=0;
  1437. FParanode:=ppn;
  1438. FAllowVariant:=true;
  1439. { determine length of parameter list }
  1440. pt:=tcallparanode(ppn);
  1441. FParalength:=0;
  1442. while assigned(pt) do
  1443. begin
  1444. inc(FParalength);
  1445. pt:=tcallparanode(pt.right);
  1446. end;
  1447. { when the definition has overload directive set, we search for
  1448. overloaded definitions in the class, this only needs to be done once
  1449. for class entries as the tree keeps always the same }
  1450. if (not sym.overloadchecked) and
  1451. (sym.owner.symtabletype=ObjectSymtable) and
  1452. (po_overload in tprocdef(sym.ProcdefList[0]).procoptions) then
  1453. search_class_overloads(sym);
  1454. { when the class passed is defined in this unit we
  1455. need to use the scope of that class. This is a trick
  1456. that can be used to access protected members in other
  1457. units. At least kylix supports it this way (PFV) }
  1458. if assigned(st) and
  1459. (
  1460. (st.symtabletype=ObjectSymtable) or
  1461. ((st.symtabletype=withsymtable) and
  1462. (st.defowner.typ=objectdef))
  1463. ) and
  1464. (st.defowner.owner.symtabletype in [globalsymtable,staticsymtable]) and
  1465. st.defowner.owner.iscurrentunit then
  1466. topclassh:=tobjectdef(st.defowner)
  1467. else
  1468. begin
  1469. if assigned(current_procinfo) then
  1470. topclassh:=current_procinfo.procdef._class
  1471. else
  1472. topclassh:=nil;
  1473. end;
  1474. { link all procedures which have the same # of parameters }
  1475. for j:=0 to sym.ProcdefList.Count-1 do
  1476. begin
  1477. pd:=tprocdef(sym.ProcdefList[j]);
  1478. { Is the procdef visible? This needs to be checked on
  1479. procdef level since a symbol can contain both private and
  1480. public declarations. But the check should not be done
  1481. when the callnode is generated by a property
  1482. inherited overrides invisible anonymous inherited (FK) }
  1483. if isprop or ignorevis or
  1484. (pd.owner.symtabletype<>ObjectSymtable) or
  1485. pd.is_visible_for_object(topclassh,nil) then
  1486. begin
  1487. { we have at least one procedure that is visible }
  1488. inc(FProcvisiblecnt);
  1489. { only when the # of parameter are supported by the
  1490. procedure }
  1491. if (FParalength>=pd.minparacount) and
  1492. ((po_varargs in pd.procoptions) or { varargs }
  1493. (FParalength<=pd.maxparacount)) then
  1494. proc_add(pd);
  1495. end;
  1496. end;
  1497. { remember if the procedure is declared with the overload directive,
  1498. it's information is still needed also after all procs are removed }
  1499. has_overload_directive:=(po_overload in tprocdef(sym.ProcdefList[0]).procoptions);
  1500. { when the definition has overload directive set, we search for
  1501. overloaded definitions in the symtablestack. The found
  1502. entries are only added to the procs list and not the procsym, because
  1503. the list can change in every situation }
  1504. if has_overload_directive and
  1505. (sym.owner.symtabletype<>ObjectSymtable) then
  1506. begin
  1507. srsymtable:=sym.owner;
  1508. checkstack:=symtablestack.stack;
  1509. while assigned(checkstack) and
  1510. (checkstack^.symtable<>srsymtable) do
  1511. checkstack:=checkstack^.next;
  1512. { we've already processed the current symtable, start with
  1513. the next symtable in the stack }
  1514. if assigned(checkstack) then
  1515. checkstack:=checkstack^.next;
  1516. hashedid.id:=sym.name;
  1517. while assigned(checkstack) do
  1518. begin
  1519. srsymtable:=checkstack^.symtable;
  1520. if srsymtable.symtabletype in [localsymtable,staticsymtable,globalsymtable] then
  1521. begin
  1522. srprocsym:=tprocsym(srsymtable.FindWithHash(hashedid));
  1523. if assigned(srprocsym) and
  1524. (srprocsym.typ=procsym) then
  1525. begin
  1526. { if this visible procedure doesn't have overload we can stop
  1527. searching }
  1528. if not(po_overload in tprocdef(srprocsym.ProcdefList[0]).procoptions) and
  1529. tprocdef(srprocsym.ProcdefList[0]).is_visible_for_object(topclassh,nil) then
  1530. break;
  1531. { process all overloaded definitions }
  1532. for j:=0 to srprocsym.ProcdefList.Count-1 do
  1533. begin
  1534. pd:=tprocdef(srprocsym.ProcdefList[j]);
  1535. { only visible procedures need to be added }
  1536. if pd.is_visible_for_object(topclassh,nil) then
  1537. begin
  1538. { only when the # of parameter are supported by the
  1539. procedure }
  1540. if (FParalength>=pd.minparacount) and
  1541. ((po_varargs in pd.procoptions) or { varargs }
  1542. (FParalength<=pd.maxparacount)) then
  1543. begin
  1544. found:=false;
  1545. hp:=FProcs;
  1546. while assigned(hp) do
  1547. begin
  1548. { Only compare visible parameters for the user }
  1549. if compare_paras(hp^.data.paras,pd.paras,cp_value_equal_const,[cpo_ignorehidden])>=te_equal then
  1550. begin
  1551. found:=true;
  1552. break;
  1553. end;
  1554. hp:=hp^.next;
  1555. end;
  1556. if not found then
  1557. proc_add(pd);
  1558. end;
  1559. end;
  1560. end;
  1561. end;
  1562. end;
  1563. checkstack:=checkstack^.next;
  1564. end;
  1565. end;
  1566. end;
  1567. constructor tcallcandidates.create_operator(op:ttoken;ppn:tnode);
  1568. var
  1569. j : integer;
  1570. pd : tprocdef;
  1571. hp : pcandidate;
  1572. found : boolean;
  1573. srsymtable : TSymtable;
  1574. srprocsym : tprocsym;
  1575. pt : tcallparanode;
  1576. checkstack : psymtablestackitem;
  1577. hashedid : THashedIDString;
  1578. begin
  1579. FProcSym:=nil;
  1580. FProcs:=nil;
  1581. FProccnt:=0;
  1582. FProcvisiblecnt:=0;
  1583. FParanode:=ppn;
  1584. FAllowVariant:=false;
  1585. { determine length of parameter list }
  1586. pt:=tcallparanode(ppn);
  1587. FParalength:=0;
  1588. while assigned(pt) do
  1589. begin
  1590. if pt.resultdef.typ=variantdef then
  1591. FAllowVariant:=true;
  1592. inc(FParalength);
  1593. pt:=tcallparanode(pt.right);
  1594. end;
  1595. { we search all overloaded operator definitions in the symtablestack. The found
  1596. entries are only added to the procs list and not the procsym, because
  1597. the list can change in every situation }
  1598. hashedid.id:=overloaded_names[op];
  1599. checkstack:=symtablestack.stack;
  1600. while assigned(checkstack) do
  1601. begin
  1602. srsymtable:=checkstack^.symtable;
  1603. if srsymtable.symtabletype in [localsymtable,staticsymtable,globalsymtable] then
  1604. begin
  1605. srprocsym:=tprocsym(srsymtable.FindWithHash(hashedid));
  1606. if assigned(srprocsym) and
  1607. (srprocsym.typ=procsym) then
  1608. begin
  1609. { Store first procsym found }
  1610. if not assigned(FProcsym) then
  1611. FProcsym:=srprocsym;
  1612. { process all overloaded definitions }
  1613. for j:=0 to srprocsym.ProcdefList.Count-1 do
  1614. begin
  1615. pd:=tprocdef(srprocsym.ProcdefList[j]);
  1616. { only when the # of parameter are supported by the
  1617. procedure }
  1618. if (FParalength>=pd.minparacount) and
  1619. (FParalength<=pd.maxparacount) then
  1620. begin
  1621. found:=false;
  1622. hp:=FProcs;
  1623. while assigned(hp) do
  1624. begin
  1625. { Only compare visible parameters for the user }
  1626. if compare_paras(hp^.data.paras,pd.paras,cp_value_equal_const,[cpo_ignorehidden])>=te_equal then
  1627. begin
  1628. found:=true;
  1629. break;
  1630. end;
  1631. hp:=hp^.next;
  1632. end;
  1633. if not found then
  1634. proc_add(pd);
  1635. end;
  1636. end;
  1637. end;
  1638. end;
  1639. checkstack:=checkstack^.next;
  1640. end;
  1641. end;
  1642. destructor tcallcandidates.destroy;
  1643. var
  1644. hpnext,
  1645. hp : pcandidate;
  1646. begin
  1647. hp:=FProcs;
  1648. while assigned(hp) do
  1649. begin
  1650. hpnext:=hp^.next;
  1651. dispose(hp);
  1652. hp:=hpnext;
  1653. end;
  1654. end;
  1655. function tcallcandidates.proc_add(pd:tprocdef):pcandidate;
  1656. var
  1657. defaultparacnt : integer;
  1658. begin
  1659. { generate new candidate entry }
  1660. new(result);
  1661. fillchar(result^,sizeof(tcandidate),0);
  1662. result^.data:=pd;
  1663. result^.next:=FProcs;
  1664. FProcs:=result;
  1665. inc(FProccnt);
  1666. { Find last parameter, skip all default parameters
  1667. that are not passed. Ignore this skipping for varargs }
  1668. result^.firstparaidx:=pd.paras.count-1;
  1669. if not(po_varargs in pd.procoptions) then
  1670. begin
  1671. { ignore hidden parameters }
  1672. while (result^.firstparaidx>=0) and (vo_is_hidden_para in tparavarsym(pd.paras[result^.firstparaidx]).varoptions) do
  1673. dec(result^.firstparaidx);
  1674. defaultparacnt:=pd.maxparacount-FParalength;
  1675. if defaultparacnt>0 then
  1676. begin
  1677. if defaultparacnt>result^.firstparaidx+1 then
  1678. internalerror(200401141);
  1679. dec(result^.firstparaidx,defaultparacnt);
  1680. end;
  1681. end;
  1682. end;
  1683. procedure tcallcandidates.list(all:boolean);
  1684. var
  1685. hp : pcandidate;
  1686. begin
  1687. hp:=FProcs;
  1688. while assigned(hp) do
  1689. begin
  1690. if all or
  1691. (not hp^.invalid) then
  1692. MessagePos1(hp^.data.fileinfo,sym_h_param_list,hp^.data.fullprocname(false));
  1693. hp:=hp^.next;
  1694. end;
  1695. end;
  1696. {$ifdef EXTDEBUG}
  1697. procedure tcallcandidates.dump_info(lvl:longint);
  1698. function ParaTreeStr(p:tcallparanode):string;
  1699. begin
  1700. result:='';
  1701. while assigned(p) do
  1702. begin
  1703. if result<>'' then
  1704. result:=','+result;
  1705. result:=p.resultdef.typename+result;
  1706. p:=tcallparanode(p.right);
  1707. end;
  1708. end;
  1709. var
  1710. hp : pcandidate;
  1711. i : integer;
  1712. currpara : tparavarsym;
  1713. begin
  1714. if not CheckVerbosity(lvl) then
  1715. exit;
  1716. Comment(lvl+V_LineInfo,'Overloaded callnode: '+FProcSym.name+'('+ParaTreeStr(tcallparanode(FParaNode))+')');
  1717. hp:=FProcs;
  1718. while assigned(hp) do
  1719. begin
  1720. Comment(lvl,' '+hp^.data.fullprocname(false));
  1721. if (hp^.invalid) then
  1722. Comment(lvl,' invalid')
  1723. else
  1724. begin
  1725. Comment(lvl,' ex: '+tostr(hp^.exact_count)+
  1726. ' eq: '+tostr(hp^.equal_count)+
  1727. ' l1: '+tostr(hp^.cl1_count)+
  1728. ' l2: '+tostr(hp^.cl2_count)+
  1729. ' l3: '+tostr(hp^.cl3_count)+
  1730. ' oper: '+tostr(hp^.coper_count)+
  1731. ' ord: '+realtostr(hp^.ordinal_distance));
  1732. { Print parameters in left-right order }
  1733. for i:=0 to hp^.data.paras.count-1 do
  1734. begin
  1735. currpara:=tparavarsym(hp^.data.paras[i]);
  1736. if not(vo_is_hidden_para in currpara.varoptions) then
  1737. Comment(lvl,' - '+currpara.vardef.typename+' : '+EqualTypeName[currpara.eqval]);
  1738. end;
  1739. end;
  1740. hp:=hp^.next;
  1741. end;
  1742. end;
  1743. {$endif EXTDEBUG}
  1744. procedure tcallcandidates.get_information;
  1745. var
  1746. hp : pcandidate;
  1747. currpara : tparavarsym;
  1748. paraidx : integer;
  1749. currparanr : byte;
  1750. rfh,rth : bestreal;
  1751. objdef : tobjectdef;
  1752. def_from,
  1753. def_to : tdef;
  1754. currpt,
  1755. pt : tcallparanode;
  1756. eq : tequaltype;
  1757. convtype : tconverttype;
  1758. pdtemp,
  1759. pdoper : tprocdef;
  1760. releasecurrpt : boolean;
  1761. cdoptions : tcompare_defs_options;
  1762. begin
  1763. cdoptions:=[cdo_check_operator];
  1764. if FAllowVariant then
  1765. include(cdoptions,cdo_allow_variant);
  1766. { process all procs }
  1767. hp:=FProcs;
  1768. while assigned(hp) do
  1769. begin
  1770. { We compare parameters in reverse order (right to left),
  1771. the firstpara is already pointing to the last parameter
  1772. were we need to start comparing }
  1773. currparanr:=FParalength;
  1774. paraidx:=hp^.firstparaidx;
  1775. while (paraidx>=0) and (vo_is_hidden_para in tparavarsym(hp^.data.paras[paraidx]).varoptions) do
  1776. dec(paraidx);
  1777. pt:=tcallparanode(FParaNode);
  1778. while assigned(pt) and (paraidx>=0) do
  1779. begin
  1780. currpara:=tparavarsym(hp^.data.paras[paraidx]);
  1781. { currpt can be changed from loadn to calln when a procvar
  1782. is passed. This is to prevent that the change is permanent }
  1783. currpt:=pt;
  1784. releasecurrpt:=false;
  1785. { retrieve current parameter definitions to compares }
  1786. eq:=te_incompatible;
  1787. def_from:=currpt.resultdef;
  1788. def_to:=currpara.vardef;
  1789. if not(assigned(def_from)) then
  1790. internalerror(200212091);
  1791. if not(
  1792. assigned(def_to) or
  1793. ((po_varargs in hp^.data.procoptions) and
  1794. (currparanr>hp^.data.minparacount))
  1795. ) then
  1796. internalerror(200212092);
  1797. { Convert tp procvars when not expecting a procvar }
  1798. if (def_to.typ<>procvardef) and
  1799. (currpt.left.resultdef.typ=procvardef) then
  1800. begin
  1801. releasecurrpt:=true;
  1802. currpt:=tcallparanode(pt.getcopy);
  1803. if maybe_call_procvar(currpt.left,true) then
  1804. begin
  1805. currpt.resultdef:=currpt.left.resultdef;
  1806. def_from:=currpt.left.resultdef;
  1807. end;
  1808. end;
  1809. { If we expect a procvar and the left is loadnode that
  1810. returns a procdef we need to find the correct overloaded
  1811. procdef that matches the expected procvar. The loadnode
  1812. temporary returned the first procdef (PFV) }
  1813. if (def_to.typ=procvardef) and
  1814. (currpt.left.nodetype=loadn) and
  1815. (currpt.left.resultdef.typ=procdef) then
  1816. begin
  1817. pdtemp:=tprocsym(Tloadnode(currpt.left).symtableentry).Find_procdef_byprocvardef(Tprocvardef(def_to));
  1818. if assigned(pdtemp) then
  1819. begin
  1820. tloadnode(currpt.left).setprocdef(pdtemp);
  1821. currpt.resultdef:=currpt.left.resultdef;
  1822. def_from:=currpt.left.resultdef;
  1823. end;
  1824. end;
  1825. { varargs are always equal, but not exact }
  1826. if (po_varargs in hp^.data.procoptions) and
  1827. (currparanr>hp^.data.minparacount) and
  1828. not is_array_of_const(def_from) and
  1829. not is_array_constructor(def_from) then
  1830. begin
  1831. eq:=te_equal;
  1832. end
  1833. else
  1834. { same definition -> exact }
  1835. if (def_from=def_to) then
  1836. begin
  1837. eq:=te_exact;
  1838. end
  1839. else
  1840. { for value and const parameters check if a integer is constant or
  1841. included in other integer -> equal and calc ordinal_distance }
  1842. if not(currpara.varspez in [vs_var,vs_out]) and
  1843. is_integer(def_from) and
  1844. is_integer(def_to) and
  1845. is_in_limit(def_from,def_to) then
  1846. begin
  1847. eq:=te_equal;
  1848. hp^.ordinal_distance:=hp^.ordinal_distance+
  1849. abs(bestreal(torddef(def_from).low)-bestreal(torddef(def_to).low));
  1850. if (torddef(def_to).ordtype=u64bit) then
  1851. rth:=bestreal(qword(torddef(def_to).high))
  1852. else
  1853. rth:=bestreal(torddef(def_to).high);
  1854. if (torddef(def_from).ordtype=u64bit) then
  1855. rfh:=bestreal(qword(torddef(def_from).high))
  1856. else
  1857. rfh:=bestreal(torddef(def_from).high);
  1858. hp^.ordinal_distance:=hp^.ordinal_distance+abs(rth-rfh);
  1859. { Give wrong sign a small penalty, this is need to get a diffrence
  1860. from word->[longword,longint] }
  1861. if is_signed(def_from)<>is_signed(def_to) then
  1862. hp^.ordinal_distance:=hp^.ordinal_distance+1.0;
  1863. end
  1864. else
  1865. { for value and const parameters check precision of real, give
  1866. penalty for loosing of precision. var and out parameters must match exactly }
  1867. if not(currpara.varspez in [vs_var,vs_out]) and
  1868. is_real(def_from) and
  1869. is_real(def_to) then
  1870. begin
  1871. eq:=te_equal;
  1872. if is_extended(def_to) then
  1873. rth:=bestreal(4)
  1874. else
  1875. if is_double (def_to) then
  1876. rth:=bestreal(2)
  1877. else
  1878. rth:=bestreal(1);
  1879. if is_extended(def_from) then
  1880. rfh:=bestreal(4)
  1881. else
  1882. if is_double (def_from) then
  1883. rfh:=bestreal(2)
  1884. else
  1885. rfh:=bestreal(1);
  1886. { penalty for shrinking of precision }
  1887. if rth<rfh then
  1888. rfh:=(rfh-rth)*16
  1889. else
  1890. rfh:=rth-rfh;
  1891. hp^.ordinal_distance:=hp^.ordinal_distance+rfh;
  1892. end
  1893. else
  1894. { related object parameters also need to determine the distance between the current
  1895. object and the object we are comparing with. var and out parameters must match exactly }
  1896. if not(currpara.varspez in [vs_var,vs_out]) and
  1897. (def_from.typ=objectdef) and
  1898. (def_to.typ=objectdef) and
  1899. (tobjectdef(def_from).objecttype=tobjectdef(def_to).objecttype) and
  1900. tobjectdef(def_from).is_related(tobjectdef(def_to)) then
  1901. begin
  1902. eq:=te_convert_l1;
  1903. objdef:=tobjectdef(def_from);
  1904. while assigned(objdef) do
  1905. begin
  1906. if objdef=def_to then
  1907. break;
  1908. hp^.ordinal_distance:=hp^.ordinal_distance+1;
  1909. objdef:=objdef.childof;
  1910. end;
  1911. end
  1912. else
  1913. { generic type comparision }
  1914. begin
  1915. eq:=compare_defs_ext(def_from,def_to,currpt.left.nodetype,convtype,pdoper,cdoptions);
  1916. { when the types are not equal we need to check
  1917. some special case for parameter passing }
  1918. if (eq<te_equal) then
  1919. begin
  1920. if currpara.varspez in [vs_var,vs_out] then
  1921. begin
  1922. { para requires an equal type so the previous found
  1923. match was not good enough, reset to incompatible }
  1924. eq:=te_incompatible;
  1925. { var_para_allowed will return te_equal and te_convert_l1 to
  1926. make a difference for best matching }
  1927. var_para_allowed(eq,currpt.resultdef,currpara.vardef)
  1928. end
  1929. else
  1930. para_allowed(eq,currpt,def_to);
  1931. end;
  1932. end;
  1933. { when a procvar was changed to a call an exact much is
  1934. downgraded to equal. This way an overload call with the
  1935. procvar is choosen. See tb0471 (PFV) }
  1936. if (pt<>currpt) and (eq=te_exact) then
  1937. eq:=te_equal;
  1938. { increase correct counter }
  1939. case eq of
  1940. te_exact :
  1941. inc(hp^.exact_count);
  1942. te_equal :
  1943. inc(hp^.equal_count);
  1944. te_convert_l1 :
  1945. inc(hp^.cl1_count);
  1946. te_convert_l2 :
  1947. inc(hp^.cl2_count);
  1948. te_convert_l3 :
  1949. inc(hp^.cl3_count);
  1950. te_convert_operator :
  1951. inc(hp^.coper_count);
  1952. te_incompatible :
  1953. hp^.invalid:=true;
  1954. else
  1955. internalerror(200212072);
  1956. end;
  1957. { stop checking when an incompatible parameter is found }
  1958. if hp^.invalid then
  1959. begin
  1960. { store the current parameter info for
  1961. a nice error message when no procedure is found }
  1962. hp^.wrongparaidx:=paraidx;
  1963. hp^.wrongparanr:=currparanr;
  1964. break;
  1965. end;
  1966. {$ifdef EXTDEBUG}
  1967. { store equal in node tree for dump }
  1968. currpara.eqval:=eq;
  1969. {$endif EXTDEBUG}
  1970. { maybe release temp currpt }
  1971. if releasecurrpt then
  1972. currpt.free;
  1973. { next parameter in the call tree }
  1974. pt:=tcallparanode(pt.right);
  1975. { next parameter for definition, only goto next para
  1976. if we're out of the varargs }
  1977. if not(po_varargs in hp^.data.procoptions) or
  1978. (currparanr<=hp^.data.maxparacount) then
  1979. begin
  1980. { Ignore vs_hidden parameters }
  1981. repeat
  1982. dec(paraidx);
  1983. until (paraidx<0) or not(vo_is_hidden_para in tparavarsym(hp^.data.paras[paraidx]).varoptions);
  1984. end;
  1985. dec(currparanr);
  1986. end;
  1987. if not(hp^.invalid) and
  1988. (assigned(pt) or (paraidx>=0) or (currparanr<>0)) then
  1989. internalerror(200212141);
  1990. { next candidate }
  1991. hp:=hp^.next;
  1992. end;
  1993. end;
  1994. function get_variantequaltype(def: tdef): tvariantequaltype;
  1995. const
  1996. variantorddef_cl: array[tordtype] of tvariantequaltype =
  1997. (tve_incompatible,tve_byte,tve_word,tve_cardinal,tve_chari64,
  1998. tve_shortint,tve_smallint,tve_longint,tve_chari64,
  1999. tve_boolformal,tve_boolformal,tve_boolformal,tve_boolformal,
  2000. tve_chari64,tve_chari64,tve_dblcurrency);
  2001. {$warning fixme for 128 bit floats }
  2002. variantfloatdef_cl: array[tfloattype] of tvariantequaltype =
  2003. (tve_single,tve_dblcurrency,tve_extended,
  2004. tve_dblcurrency,tve_dblcurrency,tve_extended);
  2005. variantstringdef_cl: array[tstringtype] of tvariantequaltype =
  2006. (tve_sstring,tve_astring,tve_astring,tve_wstring);
  2007. begin
  2008. result:=tve_incompatible;
  2009. case def.typ of
  2010. orddef:
  2011. begin
  2012. result:=variantorddef_cl[torddef(def).ordtype];
  2013. end;
  2014. floatdef:
  2015. begin
  2016. result:=variantfloatdef_cl[tfloatdef(def).floattype];
  2017. end;
  2018. stringdef:
  2019. begin
  2020. result:=variantstringdef_cl[tstringdef(def).stringtype];
  2021. end;
  2022. formaldef:
  2023. begin
  2024. result:=tve_boolformal;
  2025. end;
  2026. else
  2027. internalerror(2006122804);
  2028. end
  2029. end;
  2030. function is_better_candidate(currpd,bestpd:pcandidate):integer;
  2031. var
  2032. res : integer;
  2033. begin
  2034. {
  2035. Return values:
  2036. > 0 when currpd is better than bestpd
  2037. < 0 when bestpd is better than currpd
  2038. = 0 when both are equal
  2039. To choose the best candidate we use the following order:
  2040. - Incompatible flag
  2041. - (Smaller) Number of convert operator parameters.
  2042. - (Smaller) Number of convertlevel 2 parameters.
  2043. - (Smaller) Number of convertlevel 1 parameters.
  2044. - (Bigger) Number of exact parameters.
  2045. - (Smaller) Number of equal parameters.
  2046. - (Smaller) Total of ordinal distance. For example, the distance of a word
  2047. to a byte is 65535-255=65280.
  2048. }
  2049. if bestpd^.invalid then
  2050. begin
  2051. if currpd^.invalid then
  2052. res:=0
  2053. else
  2054. res:=1;
  2055. end
  2056. else
  2057. if currpd^.invalid then
  2058. res:=-1
  2059. else
  2060. begin
  2061. { less operator parameters? }
  2062. res:=(bestpd^.coper_count-currpd^.coper_count);
  2063. if (res=0) then
  2064. begin
  2065. { less cl3 parameters? }
  2066. res:=(bestpd^.cl3_count-currpd^.cl3_count);
  2067. if (res=0) then
  2068. begin
  2069. { less cl2 parameters? }
  2070. res:=(bestpd^.cl2_count-currpd^.cl2_count);
  2071. if (res=0) then
  2072. begin
  2073. { less cl1 parameters? }
  2074. res:=(bestpd^.cl1_count-currpd^.cl1_count);
  2075. if (res=0) then
  2076. begin
  2077. { more exact parameters? }
  2078. res:=(currpd^.exact_count-bestpd^.exact_count);
  2079. if (res=0) then
  2080. begin
  2081. { less equal parameters? }
  2082. res:=(bestpd^.equal_count-currpd^.equal_count);
  2083. if (res=0) then
  2084. begin
  2085. { smaller ordinal distance? }
  2086. if (currpd^.ordinal_distance<bestpd^.ordinal_distance) then
  2087. res:=1
  2088. else
  2089. if (currpd^.ordinal_distance>bestpd^.ordinal_distance) then
  2090. res:=-1
  2091. else
  2092. res:=0;
  2093. end;
  2094. end;
  2095. end;
  2096. end;
  2097. end;
  2098. end;
  2099. end;
  2100. is_better_candidate:=res;
  2101. end;
  2102. { Delphi precedence rules extracted from test programs. Only valid if passing
  2103. a variant parameter to overloaded procedures expecting exactly one parameter.
  2104. single > (char, currency, int64, shortstring, ansistring, widestring, extended, double)
  2105. double/currency > (char, int64, shortstring, ansistring, widestring, extended)
  2106. extended > (char, int64, shortstring, ansistring, widestring)
  2107. longint/cardinal > (int64, shortstring, ansistring, widestring, extended, double, single, char, currency)
  2108. smallint > (longint, int64, shortstring, ansistring, widestring, extended, double single, char, currency);
  2109. word > (longint, cardinal, int64, shortstring, ansistring, widestring, extended, double single, char, currency);
  2110. shortint > (longint, smallint, int64, shortstring, ansistring, widestring, extended, double, single, char, currency)
  2111. byte > (longint, cardinal, word, smallint, int64, shortstring, ansistring, widestring, extended, double, single, char, currency);
  2112. boolean/formal > (char, int64, shortstring, ansistring, widestring)
  2113. shortstring > (char, int64, ansistring, widestring)
  2114. ansistring > (char, int64, widestring)
  2115. widestring > (char, int64)
  2116. Relations not mentioned mean that they conflict: no decision possible }
  2117. function is_better_candidate_single_variant(currpd,bestpd:pcandidate):integer;
  2118. function calculate_relation(const currvcl, bestvcl, testvcl:
  2119. tvariantequaltype; const conflictvcls: tvariantequaltypes):integer;
  2120. begin
  2121. { if (bestvcl=conflictvcl) or
  2122. (currvcl=conflictvcl) then
  2123. result:=0
  2124. else if (bestvcl=testvcl) then
  2125. result:=-1
  2126. else result:=1 }
  2127. result:=1-2*ord(bestvcl=testvcl)+
  2128. ord(currvcl in conflictvcls)-ord(bestvcl in conflictvcls);
  2129. end;
  2130. var
  2131. paraidx,
  2132. res: integer;
  2133. currpara, bestpara: tparavarsym;
  2134. currvcl, bestvcl: tvariantequaltype;
  2135. begin
  2136. {
  2137. Return values:
  2138. > 0 when currpd is better than bestpd
  2139. < 0 when bestpd is better than currpd
  2140. = 0 when both are equal
  2141. }
  2142. if (currpd^.firstparaidx<>bestpd^.firstparaidx) then
  2143. internalerror(2006122801);
  2144. paraidx:=currpd^.firstparaidx;
  2145. while (paraidx>=0) and (vo_is_hidden_para in tparavarsym(currpd^.data.paras[paraidx]).varoptions) do
  2146. if (vo_is_hidden_para in tparavarsym(bestpd^.data.paras[paraidx]).varoptions) then
  2147. dec(paraidx)
  2148. else
  2149. internalerror(2006122802);
  2150. if (vo_is_hidden_para in tparavarsym(currpd^.data.paras[paraidx]).varoptions) then
  2151. internalerror(2006122803);
  2152. currpara:=tparavarsym(currpd^.data.paras[paraidx]);
  2153. bestpara:=tparavarsym(bestpd^.data.paras[paraidx]);
  2154. { if one of the parameters is a regular variant, fall back to the }
  2155. { default algorithm }
  2156. if (currpara.vardef.typ = variantdef) or
  2157. (bestpara.vardef.typ = variantdef) then
  2158. begin
  2159. result:=is_better_candidate(currpd,bestpd);
  2160. exit;
  2161. end;
  2162. currvcl:=get_variantequaltype(currpara.vardef);
  2163. bestvcl:=get_variantequaltype(bestpara.vardef);
  2164. { sanity check }
  2165. result:=-5;
  2166. { if both are the same, there is a conflict }
  2167. if (currvcl=bestvcl) then
  2168. result:=0
  2169. { boolean and formal are better than chari64str, but conflict with }
  2170. { everything else }
  2171. else if (currvcl=tve_boolformal) or
  2172. (bestvcl=tve_boolformal) then
  2173. if (currvcl=tve_boolformal) then
  2174. result:=ord(bestvcl in [tve_chari64,tve_sstring,tve_astring,tve_wstring])
  2175. else
  2176. result:=-ord(currvcl in [tve_chari64,tve_sstring,tve_astring,tve_wstring])
  2177. { byte is better than everything else (we assume both aren't byte, }
  2178. { since there's only one parameter and that one can't be the same) }
  2179. else if (currvcl=tve_byte) or
  2180. (bestvcl=tve_byte) then
  2181. result:=calculate_relation(currvcl,bestvcl,tve_byte,[tve_shortint])
  2182. { shortint conflicts with word and cardinal, but is better than }
  2183. { everything else but byte (which has already been handled) }
  2184. else if (currvcl=tve_shortint) or
  2185. (bestvcl=tve_shortint) then
  2186. result:=calculate_relation(currvcl,bestvcl,tve_shortint,[tve_word, tve_cardinal])
  2187. { word conflicts with smallint, but is better than everything else }
  2188. { but shortint and byte (which has already been handled) }
  2189. else if (currvcl=tve_word) or
  2190. (bestvcl=tve_word) then
  2191. result:=calculate_relation(currvcl,bestvcl,tve_word,[tve_smallint])
  2192. { smallint conflicts with cardinal, but is better than everything }
  2193. { which has not yet been tested }
  2194. else if (currvcl=tve_smallint) or
  2195. (bestvcl=tve_smallint) then
  2196. result:=calculate_relation(currvcl,bestvcl,tve_smallint,[tve_cardinal])
  2197. { cardinal conflicts with each longint and is better than everything }
  2198. { which has not yet been tested }
  2199. else if (currvcl = tve_cardinal) or
  2200. (bestvcl=tve_cardinal) then
  2201. result:=calculate_relation(currvcl,bestvcl,tve_cardinal,[tve_longint])
  2202. { longint is better than everything which has not yet been tested }
  2203. else if (currvcl=tve_longint) or
  2204. (bestvcl=tve_longint) then
  2205. { if bestvcl=tve_longint then
  2206. result:=-1
  2207. else
  2208. result:=1 }
  2209. result:=1-2*ord(bestvcl=tve_longint)
  2210. { single is better than everything left }
  2211. else if (currvcl=tve_single) or
  2212. (bestvcl=tve_single) then
  2213. result:=1-2*ord(bestvcl=tve_single)
  2214. { double/comp/currency are better than everything left, and conflict }
  2215. { with each other (but that's already tested) }
  2216. else if (currvcl=tve_dblcurrency) or
  2217. (bestvcl=tve_dblcurrency) then
  2218. result:=1-2*ord(bestvcl=tve_dblcurrency)
  2219. { extended is better than everything left }
  2220. else if (currvcl=tve_extended) or
  2221. (bestvcl=tve_extended) then
  2222. result:=1-2*ord(bestvcl=tve_extended)
  2223. { shortstring is better than everything left }
  2224. else if (currvcl=tve_sstring) or
  2225. (bestvcl=tve_sstring) then
  2226. result:=1-2*ord(bestvcl=tve_sstring)
  2227. { ansistring is better than everything left }
  2228. else if (currvcl=tve_astring) or
  2229. (bestvcl=tve_astring) then
  2230. result:=1-2*ord(bestvcl=tve_astring)
  2231. { widestring is better than everything left }
  2232. else if (currvcl=tve_wstring) or
  2233. (bestvcl=tve_wstring) then
  2234. result:=1-2*ord(bestvcl=tve_wstring);
  2235. { all possibilities should have been checked now }
  2236. if (result=-5) then
  2237. internalerror(2006122805);
  2238. end;
  2239. function tcallcandidates.choose_best(var bestpd:tabstractprocdef; singlevariant: boolean):integer;
  2240. var
  2241. besthpstart,
  2242. hp : pcandidate;
  2243. cntpd,
  2244. res : integer;
  2245. begin
  2246. {
  2247. Returns the number of candidates left and the
  2248. first candidate is returned in pdbest
  2249. }
  2250. { Setup the first procdef as best, only count it as a result
  2251. when it is valid }
  2252. bestpd:=FProcs^.data;
  2253. if FProcs^.invalid then
  2254. cntpd:=0
  2255. else
  2256. cntpd:=1;
  2257. if assigned(FProcs^.next) then
  2258. begin
  2259. besthpstart:=FProcs;
  2260. hp:=FProcs^.next;
  2261. while assigned(hp) do
  2262. begin
  2263. if not singlevariant then
  2264. res:=is_better_candidate(hp,besthpstart)
  2265. else
  2266. res:=is_better_candidate_single_variant(hp,besthpstart);
  2267. if (res>0) then
  2268. begin
  2269. { hp is better, flag all procs to be incompatible }
  2270. while (besthpstart<>hp) do
  2271. begin
  2272. besthpstart^.invalid:=true;
  2273. besthpstart:=besthpstart^.next;
  2274. end;
  2275. { besthpstart is already set to hp }
  2276. bestpd:=besthpstart^.data;
  2277. cntpd:=1;
  2278. end
  2279. else
  2280. if (res<0) then
  2281. begin
  2282. { besthpstart is better, flag current hp to be incompatible }
  2283. hp^.invalid:=true;
  2284. end
  2285. else
  2286. begin
  2287. { res=0, both are valid }
  2288. if not hp^.invalid then
  2289. inc(cntpd);
  2290. end;
  2291. hp:=hp^.next;
  2292. end;
  2293. end;
  2294. result:=cntpd;
  2295. end;
  2296. procedure tcallcandidates.find_wrong_para;
  2297. var
  2298. currparanr : smallint;
  2299. hp : pcandidate;
  2300. pt : tcallparanode;
  2301. wrongpara : tparavarsym;
  2302. begin
  2303. { Only process the first overloaded procdef }
  2304. hp:=FProcs;
  2305. { Find callparanode corresponding to the argument }
  2306. pt:=tcallparanode(FParanode);
  2307. currparanr:=FParalength;
  2308. while assigned(pt) and
  2309. (currparanr>hp^.wrongparanr) do
  2310. begin
  2311. pt:=tcallparanode(pt.right);
  2312. dec(currparanr);
  2313. end;
  2314. if (currparanr<>hp^.wrongparanr) or
  2315. not assigned(pt) then
  2316. internalerror(200212094);
  2317. { Show error message, when it was a var or out parameter
  2318. guess that it is a missing typeconv }
  2319. wrongpara:=tparavarsym(hp^.data.paras[hp^.wrongparaidx]);
  2320. if wrongpara.varspez in [vs_var,vs_out] then
  2321. begin
  2322. { Maybe passing the correct type but passing a const to var parameter }
  2323. if (compare_defs(pt.resultdef,wrongpara.vardef,pt.nodetype)<>te_incompatible) and
  2324. not valid_for_var(pt.left,true) then
  2325. CGMessagePos(pt.left.fileinfo,type_e_variable_id_expected)
  2326. else
  2327. CGMessagePos3(pt.left.fileinfo,parser_e_call_by_ref_without_typeconv,tostr(hp^.wrongparanr),
  2328. FullTypeName(pt.left.resultdef,wrongpara.vardef),
  2329. FullTypeName(wrongpara.vardef,pt.left.resultdef))
  2330. end
  2331. else
  2332. CGMessagePos3(pt.left.fileinfo,type_e_wrong_parameter_type,tostr(hp^.wrongparanr),
  2333. FullTypeName(pt.left.resultdef,wrongpara.vardef),
  2334. FullTypeName(wrongpara.vardef,pt.left.resultdef));
  2335. end;
  2336. procedure check_hints(const srsym: tsym; const symoptions: tsymoptions);
  2337. begin
  2338. if not assigned(srsym) then
  2339. internalerror(200602051);
  2340. if sp_hint_deprecated in symoptions then
  2341. Message1(sym_w_deprecated_symbol,srsym.realname);
  2342. if sp_hint_platform in symoptions then
  2343. Message1(sym_w_non_portable_symbol,srsym.realname);
  2344. if sp_hint_unimplemented in symoptions then
  2345. Message1(sym_w_non_implemented_symbol,srsym.realname);
  2346. end;
  2347. procedure check_ranges(const location: tfileposinfo; source: tnode; destdef: tdef);
  2348. begin
  2349. { check if the assignment may cause a range check error }
  2350. { if its not explicit, and only if the values are }
  2351. { ordinals, enumdef and floatdef }
  2352. if assigned(destdef) and
  2353. (destdef.typ in [enumdef,orddef,floatdef]) and
  2354. not is_boolean(destdef) and
  2355. assigned(source.resultdef) and
  2356. (source.resultdef.typ in [enumdef,orddef,floatdef]) and
  2357. not is_boolean(source.resultdef) and
  2358. not is_constrealnode(source) then
  2359. begin
  2360. if (destdef.size < source.resultdef.size) then
  2361. begin
  2362. if (cs_check_range in current_settings.localswitches) then
  2363. MessagePos(location,type_w_smaller_possible_range_check)
  2364. else
  2365. MessagePos(location,type_h_smaller_possible_range_check);
  2366. end;
  2367. end;
  2368. end;
  2369. end.