htypechk.pas 99 KB

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