htypechk.pas 99 KB

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