htypechk.pas 100 KB

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