htypechk.pas 100 KB

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