htypechk.pas 98 KB

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