htypechk.pas 101 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655
  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. case newstate of
  814. vs_written:
  815. include(tloadnode(p).flags,nf_write);
  816. vs_readwritten:
  817. if not(nf_write in tloadnode(p).flags) then
  818. include(tloadnode(p).flags,nf_modify);
  819. end;
  820. break;
  821. end;
  822. callparan :
  823. internalerror(200310081);
  824. else
  825. break;
  826. end;{case }
  827. end;
  828. end;
  829. procedure set_unique(p : tnode);
  830. begin
  831. while assigned(p) do
  832. begin
  833. case p.nodetype of
  834. vecn:
  835. begin
  836. include(p.flags,nf_callunique);
  837. break;
  838. end;
  839. typeconvn,
  840. subscriptn,
  841. derefn:
  842. p:=tunarynode(p).left;
  843. else
  844. break;
  845. end;
  846. end;
  847. end;
  848. function valid_for_assign(p:tnode;opts:TValidAssigns; report_errors: boolean):boolean;
  849. var
  850. hp2,
  851. hp : tnode;
  852. gotstring,
  853. gotsubscript,
  854. gotrecord,
  855. gotpointer,
  856. gotvec,
  857. gotclass,
  858. gotdynarray,
  859. gotderef : boolean;
  860. fromdef,
  861. todef : tdef;
  862. errmsg,
  863. temp : longint;
  864. begin
  865. if valid_const in opts then
  866. errmsg:=type_e_variable_id_expected
  867. else if valid_property in opts then
  868. errmsg:=type_e_argument_cant_be_assigned
  869. else
  870. errmsg:=type_e_no_addr_of_constant;
  871. result:=false;
  872. gotsubscript:=false;
  873. gotvec:=false;
  874. gotderef:=false;
  875. gotrecord:=false;
  876. gotclass:=false;
  877. gotpointer:=false;
  878. gotdynarray:=false;
  879. gotstring:=false;
  880. hp:=p;
  881. if not(valid_void in opts) and
  882. is_void(hp.resultdef) then
  883. begin
  884. if report_errors then
  885. CGMessagePos(hp.fileinfo,errmsg);
  886. exit;
  887. end;
  888. while assigned(hp) do
  889. begin
  890. { property allowed? calln has a property check itself }
  891. if (nf_isproperty in hp.flags) then
  892. begin
  893. { check return type }
  894. case hp.resultdef.typ of
  895. pointerdef :
  896. gotpointer:=true;
  897. objectdef :
  898. gotclass:=is_class_or_interface(hp.resultdef);
  899. recorddef :
  900. gotrecord:=true;
  901. classrefdef :
  902. gotclass:=true;
  903. stringdef :
  904. gotstring:=true;
  905. end;
  906. if (valid_property in opts) then
  907. begin
  908. { don't allow writing to calls that will create
  909. temps like calls that return a structure and we
  910. are assigning to a member }
  911. if (valid_const in opts) or
  912. { if we got a deref, we won't modify the property itself }
  913. (gotderef) or
  914. { same when we got a class and subscript (= deref) }
  915. (gotclass and gotsubscript) or
  916. (
  917. not(gotsubscript and gotrecord) and
  918. not(gotstring and gotvec)
  919. ) then
  920. result:=true
  921. else
  922. if report_errors then
  923. CGMessagePos(hp.fileinfo,errmsg);
  924. end
  925. else
  926. begin
  927. { 1. if it returns a pointer and we've found a deref,
  928. 2. if it returns a class or record and a subscription or with is found
  929. 3. if the address is needed of a field (subscriptn, vecn) }
  930. if (gotpointer and gotderef) or
  931. (gotstring and gotvec) or
  932. (
  933. (gotclass or gotrecord) and
  934. (gotsubscript)
  935. ) or
  936. (
  937. (gotvec and gotdynarray)
  938. ) or
  939. (
  940. (Valid_Addr in opts) and
  941. (hp.nodetype in [subscriptn,vecn])
  942. ) then
  943. result:=true
  944. else
  945. if report_errors then
  946. CGMessagePos(hp.fileinfo,errmsg);
  947. end;
  948. exit;
  949. end;
  950. case hp.nodetype of
  951. temprefn :
  952. begin
  953. valid_for_assign := true;
  954. exit;
  955. end;
  956. derefn :
  957. begin
  958. gotderef:=true;
  959. hp:=tderefnode(hp).left;
  960. end;
  961. typeconvn :
  962. begin
  963. { typecast sizes must match, exceptions:
  964. - implicit typecast made by absolute
  965. - from formaldef
  966. - from void
  967. - from/to open array
  968. - typecast from pointer to array }
  969. fromdef:=ttypeconvnode(hp).left.resultdef;
  970. todef:=hp.resultdef;
  971. if not((nf_absolute in ttypeconvnode(hp).flags) or
  972. (fromdef.typ=formaldef) or
  973. is_void(fromdef) or
  974. is_open_array(fromdef) or
  975. is_open_array(todef) or
  976. ((fromdef.typ=pointerdef) and (todef.typ=arraydef)) or
  977. ((fromdef.typ = objectdef) and (todef.typ = objectdef) and
  978. (tobjectdef(fromdef).is_related(tobjectdef(todef))))) and
  979. (fromdef.size<>todef.size) then
  980. begin
  981. { in TP it is allowed to typecast to smaller types. But the variable can't
  982. be in a register }
  983. if (m_tp7 in current_settings.modeswitches) or
  984. (todef.size<fromdef.size) then
  985. make_not_regable(hp,[ra_addr_regable])
  986. else
  987. if report_errors then
  988. CGMessagePos2(hp.fileinfo,type_e_typecast_wrong_size_for_assignment,tostr(fromdef.size),tostr(todef.size));
  989. end;
  990. { don't allow assignments to typeconvs that need special code }
  991. if not(gotsubscript or gotvec or gotderef) and
  992. not(ttypeconvnode(hp).assign_allowed) then
  993. begin
  994. if report_errors then
  995. CGMessagePos(hp.fileinfo,errmsg);
  996. exit;
  997. end;
  998. case hp.resultdef.typ of
  999. pointerdef :
  1000. gotpointer:=true;
  1001. objectdef :
  1002. gotclass:=is_class_or_interface(hp.resultdef);
  1003. classrefdef :
  1004. gotclass:=true;
  1005. arraydef :
  1006. begin
  1007. { pointer -> array conversion is done then we need to see it
  1008. as a deref, because a ^ is then not required anymore }
  1009. if (ttypeconvnode(hp).left.resultdef.typ=pointerdef) then
  1010. gotderef:=true;
  1011. end;
  1012. end;
  1013. hp:=ttypeconvnode(hp).left;
  1014. end;
  1015. vecn :
  1016. begin
  1017. if { only check for first (= outermost) vec node }
  1018. not gotvec and
  1019. not(valid_packed in opts) and
  1020. (tvecnode(hp).left.resultdef.typ = arraydef) and
  1021. (ado_IsBitPacked in tarraydef(tvecnode(hp).left.resultdef).arrayoptions) and
  1022. ((tarraydef(tvecnode(hp).left.resultdef).elepackedbitsize mod 8 <> 0) or
  1023. (is_ordinal(tarraydef(tvecnode(hp).left.resultdef).elementdef) and
  1024. not ispowerof2(tarraydef(tvecnode(hp).left.resultdef).elepackedbitsize div 8,temp))) then
  1025. begin
  1026. if report_errors then
  1027. if (valid_property in opts) then
  1028. CGMessagePos(hp.fileinfo,parser_e_packed_element_no_loop)
  1029. else
  1030. CGMessagePos(hp.fileinfo,parser_e_packed_element_no_var_addr);
  1031. exit;
  1032. end;
  1033. gotvec:=true;
  1034. { accesses to dyn. arrays override read only access in delphi }
  1035. if (m_delphi in current_settings.modeswitches) and is_dynamic_array(tunarynode(hp).left.resultdef) then
  1036. gotdynarray:=true;
  1037. hp:=tunarynode(hp).left;
  1038. end;
  1039. blockn :
  1040. begin
  1041. hp2:=tblocknode(hp).statements;
  1042. if assigned(hp2) then
  1043. begin
  1044. if hp2.nodetype<>statementn then
  1045. internalerror(2006110801);
  1046. while assigned(tstatementnode(hp2).next) do
  1047. hp2:=tstatementnode(hp2).next;
  1048. hp:=tstatementnode(hp2).statement;
  1049. end
  1050. else
  1051. begin
  1052. if report_errors then
  1053. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  1054. exit;
  1055. end;
  1056. end;
  1057. asn :
  1058. begin
  1059. { asn can't be assigned directly, it returns the value in a register instead
  1060. of reference. }
  1061. if not(gotsubscript or gotderef or gotvec) then
  1062. begin
  1063. if report_errors then
  1064. CGMessagePos(hp.fileinfo,errmsg);
  1065. exit;
  1066. end;
  1067. hp:=tunarynode(hp).left;
  1068. end;
  1069. subscriptn :
  1070. begin
  1071. { only check first (= outermost) subscriptn }
  1072. if not gotsubscript and
  1073. not(valid_packed in opts) and
  1074. is_packed_record_or_object(tsubscriptnode(hp).left.resultdef) and
  1075. ((tsubscriptnode(hp).vs.fieldoffset mod 8 <> 0) or
  1076. (is_ordinal(tsubscriptnode(hp).resultdef) and
  1077. not ispowerof2(tsubscriptnode(hp).resultdef.packedbitsize div 8,temp))) then
  1078. begin
  1079. if report_errors then
  1080. if (valid_property in opts) then
  1081. CGMessagePos(hp.fileinfo,parser_e_packed_element_no_loop)
  1082. else
  1083. CGMessagePos(hp.fileinfo,parser_e_packed_element_no_var_addr);
  1084. exit;
  1085. end;
  1086. gotsubscript:=true;
  1087. { loop counter? }
  1088. if not(Valid_Const in opts) and
  1089. (vo_is_loop_counter in tsubscriptnode(hp).vs.varoptions) then
  1090. begin
  1091. if report_errors then
  1092. CGMessage1(parser_e_illegal_assignment_to_count_var,tsubscriptnode(hp).vs.realname)
  1093. else
  1094. exit;
  1095. end;
  1096. { a class/interface access is an implicit }
  1097. { dereferencing }
  1098. hp:=tsubscriptnode(hp).left;
  1099. if is_class_or_interface(hp.resultdef) then
  1100. gotderef:=true;
  1101. end;
  1102. muln,
  1103. divn,
  1104. andn,
  1105. xorn,
  1106. orn,
  1107. notn,
  1108. subn,
  1109. addn :
  1110. begin
  1111. { Allow operators on a pointer, or an integer
  1112. and a pointer typecast and deref has been found }
  1113. if ((hp.resultdef.typ=pointerdef) or
  1114. (is_integer(hp.resultdef) and gotpointer)) and
  1115. gotderef then
  1116. result:=true
  1117. else
  1118. { Temp strings are stored in memory, for compatibility with
  1119. delphi only }
  1120. if (m_delphi in current_settings.modeswitches) and
  1121. ((valid_addr in opts) or
  1122. (valid_const in opts)) and
  1123. (hp.resultdef.typ=stringdef) then
  1124. result:=true
  1125. else
  1126. if report_errors then
  1127. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  1128. exit;
  1129. end;
  1130. niln,
  1131. pointerconstn :
  1132. begin
  1133. { to support e.g. @tmypointer(0)^.data; see tests/tbs/tb0481 }
  1134. if gotderef then
  1135. result:=true
  1136. else
  1137. if report_errors then
  1138. CGMessagePos(hp.fileinfo,type_e_no_assign_to_addr);
  1139. exit;
  1140. end;
  1141. ordconstn,
  1142. realconstn :
  1143. begin
  1144. { these constants will be passed by value }
  1145. if report_errors then
  1146. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  1147. exit;
  1148. end;
  1149. setconstn,
  1150. stringconstn,
  1151. guidconstn :
  1152. begin
  1153. { these constants will be passed by reference }
  1154. if valid_const in opts then
  1155. result:=true
  1156. else
  1157. if report_errors then
  1158. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  1159. exit;
  1160. end;
  1161. addrn :
  1162. begin
  1163. if gotderef then
  1164. result:=true
  1165. else
  1166. if report_errors then
  1167. CGMessagePos(hp.fileinfo,type_e_no_assign_to_addr);
  1168. exit;
  1169. end;
  1170. calln :
  1171. begin
  1172. { check return type }
  1173. case hp.resultdef.typ of
  1174. arraydef :
  1175. begin
  1176. { dynamic arrays are allowed when there is also a
  1177. vec node }
  1178. if is_dynamic_array(hp.resultdef) and
  1179. gotvec then
  1180. begin
  1181. gotderef:=true;
  1182. gotpointer:=true;
  1183. end;
  1184. end;
  1185. pointerdef :
  1186. gotpointer:=true;
  1187. objectdef :
  1188. gotclass:=is_class_or_interface(hp.resultdef);
  1189. recorddef, { handle record like class it needs a subscription }
  1190. classrefdef :
  1191. gotclass:=true;
  1192. stringdef :
  1193. gotstring:=true;
  1194. end;
  1195. { 1. if it returns a pointer and we've found a deref,
  1196. 2. if it returns a class or record and a subscription or with is found
  1197. 3. string is returned }
  1198. if (gotstring and gotvec) or
  1199. (gotpointer and gotderef) or
  1200. (gotclass and gotsubscript) then
  1201. result:=true
  1202. else
  1203. { Temp strings are stored in memory, for compatibility with
  1204. delphi only }
  1205. if (m_delphi in current_settings.modeswitches) and
  1206. (valid_addr in opts) and
  1207. (hp.resultdef.typ=stringdef) then
  1208. result:=true
  1209. else
  1210. if ([valid_const,valid_addr] * opts = [valid_const]) then
  1211. result:=true
  1212. else
  1213. if report_errors then
  1214. CGMessagePos(hp.fileinfo,errmsg);
  1215. exit;
  1216. end;
  1217. inlinen :
  1218. begin
  1219. if ((valid_const in opts) and
  1220. (tinlinenode(hp).inlinenumber in [in_typeof_x]))
  1221. {$ifdef SUPPORT_UNALIGNED}
  1222. or (tinlinenode(hp).inlinenumber in [in_unaligned_x])
  1223. {$endif SUPPORT_UNALIGNED}
  1224. then
  1225. result:=true
  1226. else
  1227. if report_errors then
  1228. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  1229. exit;
  1230. end;
  1231. dataconstn:
  1232. begin
  1233. { only created internally, so no additional checks necessary }
  1234. result:=true;
  1235. exit;
  1236. end;
  1237. loadn :
  1238. begin
  1239. case tloadnode(hp).symtableentry.typ of
  1240. absolutevarsym,
  1241. staticvarsym,
  1242. localvarsym,
  1243. paravarsym :
  1244. begin
  1245. { loop counter? }
  1246. if not(Valid_Const in opts) and
  1247. not gotderef and
  1248. (vo_is_loop_counter in tabstractvarsym(tloadnode(hp).symtableentry).varoptions) then
  1249. if report_errors then
  1250. CGMessage1(parser_e_illegal_assignment_to_count_var,tloadnode(hp).symtableentry.realname)
  1251. else
  1252. exit;
  1253. { read-only variable? }
  1254. if (tabstractvarsym(tloadnode(hp).symtableentry).varspez=vs_const) then
  1255. begin
  1256. { allow p^:= constructions with p is const parameter }
  1257. if gotderef or gotdynarray or (Valid_Const in opts) or
  1258. (nf_isinternal_ignoreconst in tloadnode(hp).flags) then
  1259. result:=true
  1260. else
  1261. if report_errors then
  1262. CGMessagePos(tloadnode(hp).fileinfo,type_e_no_assign_to_const);
  1263. exit;
  1264. end;
  1265. result:=true;
  1266. exit;
  1267. end;
  1268. procsym :
  1269. begin
  1270. if (Valid_Const in opts) then
  1271. result:=true
  1272. else
  1273. if report_errors then
  1274. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  1275. exit;
  1276. end;
  1277. labelsym :
  1278. begin
  1279. if (Valid_Addr in opts) then
  1280. result:=true
  1281. else
  1282. if report_errors then
  1283. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  1284. exit;
  1285. end;
  1286. constsym:
  1287. begin
  1288. if (tconstsym(tloadnode(hp).symtableentry).consttyp=constresourcestring) and
  1289. (valid_addr in opts) then
  1290. result:=true
  1291. else
  1292. if report_errors then
  1293. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  1294. exit;
  1295. end;
  1296. else
  1297. begin
  1298. if report_errors then
  1299. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  1300. exit;
  1301. end;
  1302. end;
  1303. end;
  1304. else
  1305. begin
  1306. if report_errors then
  1307. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  1308. exit;
  1309. end;
  1310. end;
  1311. end;
  1312. end;
  1313. function valid_for_var(p:tnode; report_errors: boolean):boolean;
  1314. begin
  1315. valid_for_var:=valid_for_assign(p,[],report_errors);
  1316. end;
  1317. function valid_for_formal_var(p : tnode; report_errors: boolean) : boolean;
  1318. begin
  1319. valid_for_formal_var:=valid_for_assign(p,[valid_void],report_errors);
  1320. end;
  1321. function valid_for_formal_const(p : tnode; report_errors: boolean) : boolean;
  1322. begin
  1323. valid_for_formal_const:=(p.resultdef.typ=formaldef) or
  1324. valid_for_assign(p,[valid_void,valid_const,valid_property],report_errors);
  1325. end;
  1326. function valid_for_assignment(p:tnode; report_errors: boolean):boolean;
  1327. begin
  1328. valid_for_assignment:=valid_for_assign(p,[valid_property,valid_packed],report_errors);
  1329. end;
  1330. function valid_for_loopvar(p:tnode; report_errors: boolean):boolean;
  1331. begin
  1332. valid_for_loopvar:=valid_for_assign(p,[valid_property],report_errors);
  1333. end;
  1334. function valid_for_addr(p : tnode; report_errors: boolean) : boolean;
  1335. begin
  1336. result:=valid_for_assign(p,[valid_const,valid_addr,valid_void],report_errors);
  1337. end;
  1338. procedure var_para_allowed(var eq:tequaltype;def_from,def_to:Tdef; fromnode: tnode);
  1339. begin
  1340. { Note: eq must be already valid, it will only be updated! }
  1341. case def_to.typ of
  1342. formaldef :
  1343. begin
  1344. { all types can be passed to a formaldef,
  1345. but it is not the prefered way }
  1346. if not is_constnode(fromnode) then
  1347. eq:=te_convert_l2
  1348. else
  1349. eq:=te_incompatible;
  1350. end;
  1351. orddef :
  1352. begin
  1353. { allows conversion from word to integer and
  1354. byte to shortint, but only for TP7 compatibility }
  1355. if (m_tp7 in current_settings.modeswitches) and
  1356. (def_from.typ=orddef) and
  1357. (def_from.size=def_to.size) then
  1358. eq:=te_convert_l1;
  1359. end;
  1360. arraydef :
  1361. begin
  1362. if is_open_array(def_to) then
  1363. begin
  1364. if is_dynamic_array(def_from) and
  1365. equal_defs(tarraydef(def_from).elementdef,tarraydef(def_to).elementdef) then
  1366. eq:=te_convert_l2
  1367. else
  1368. if equal_defs(def_from,tarraydef(def_to).elementdef) then
  1369. eq:=te_convert_l2;
  1370. end;
  1371. end;
  1372. pointerdef :
  1373. begin
  1374. { an implicit pointer conversion is allowed }
  1375. if (def_from.typ=pointerdef) then
  1376. eq:=te_convert_l1;
  1377. end;
  1378. stringdef :
  1379. begin
  1380. { all shortstrings are allowed, size is not important }
  1381. if is_shortstring(def_from) and
  1382. is_shortstring(def_to) then
  1383. eq:=te_equal;
  1384. end;
  1385. objectdef :
  1386. begin
  1387. { child objects can be also passed }
  1388. { in non-delphi mode, otherwise }
  1389. { they must match exactly, except }
  1390. { if they are objects }
  1391. if (def_from.typ=objectdef) and
  1392. (
  1393. not(m_delphi in current_settings.modeswitches) or
  1394. (
  1395. (tobjectdef(def_from).objecttype=odt_object) and
  1396. (tobjectdef(def_to).objecttype=odt_object)
  1397. )
  1398. ) and
  1399. (tobjectdef(def_from).is_related(tobjectdef(def_to))) then
  1400. eq:=te_convert_l1;
  1401. end;
  1402. filedef :
  1403. begin
  1404. { an implicit file conversion is also allowed }
  1405. { from a typed file to an untyped one }
  1406. if (def_from.typ=filedef) and
  1407. (tfiledef(def_from).filetyp = ft_typed) and
  1408. (tfiledef(def_to).filetyp = ft_untyped) then
  1409. eq:=te_convert_l1;
  1410. end;
  1411. end;
  1412. end;
  1413. procedure para_allowed(var eq:tequaltype;p:tcallparanode;def_to:tdef);
  1414. begin
  1415. { Note: eq must be already valid, it will only be updated! }
  1416. case def_to.typ of
  1417. formaldef :
  1418. begin
  1419. { all types can be passed to a formaldef }
  1420. eq:=te_equal;
  1421. end;
  1422. stringdef :
  1423. begin
  1424. { to support ansi/long/wide strings in a proper way }
  1425. { string and string[10] are assumed as equal }
  1426. { when searching the correct overloaded procedure }
  1427. if (p.resultdef.typ=stringdef) and
  1428. (tstringdef(def_to).stringtype=tstringdef(p.resultdef).stringtype) then
  1429. eq:=te_equal
  1430. else
  1431. { Passing a constant char to ansistring or shortstring or
  1432. a widechar to widestring then handle it as equal. }
  1433. if (p.left.nodetype=ordconstn) and
  1434. (
  1435. is_char(p.resultdef) and
  1436. (is_shortstring(def_to) or is_ansistring(def_to))
  1437. ) or
  1438. (
  1439. is_widechar(p.resultdef) and
  1440. (is_widestring(def_to) or is_unicodestring(def_to))
  1441. ) then
  1442. eq:=te_equal
  1443. end;
  1444. setdef :
  1445. begin
  1446. { set can also be a not yet converted array constructor }
  1447. if (p.resultdef.typ=arraydef) and
  1448. is_array_constructor(p.resultdef) and
  1449. not is_variant_array(p.resultdef) then
  1450. eq:=te_equal;
  1451. end;
  1452. procvardef :
  1453. begin
  1454. { in tp7 mode proc -> procvar is allowed }
  1455. if ((m_tp_procvar in current_settings.modeswitches) or
  1456. (m_mac_procvar in current_settings.modeswitches)) and
  1457. (p.left.nodetype=calln) and
  1458. (proc_to_procvar_equal(tprocdef(tcallnode(p.left).procdefinition),tprocvardef(def_to))>=te_equal) then
  1459. eq:=te_equal
  1460. else
  1461. if (m_mac_procvar in current_settings.modeswitches) and
  1462. is_procvar_load(p.left) then
  1463. eq:=te_convert_l2;
  1464. end;
  1465. end;
  1466. end;
  1467. function allowenumop(nt:tnodetype):boolean;
  1468. begin
  1469. result:=(nt in [equaln,unequaln,ltn,lten,gtn,gten]) or
  1470. ((cs_allow_enum_calc in current_settings.localswitches) and
  1471. (nt in [addn,subn]));
  1472. end;
  1473. {****************************************************************************
  1474. TCallCandidates
  1475. ****************************************************************************}
  1476. constructor tcallcandidates.create(sym:tprocsym;st:TSymtable;ppn:tnode;isprop,ignorevis : boolean);
  1477. var
  1478. j : integer;
  1479. pd : tprocdef;
  1480. hp : pcandidate;
  1481. found,
  1482. has_overload_directive : boolean;
  1483. topclassh : tobjectdef;
  1484. srsymtable : TSymtable;
  1485. srprocsym : tprocsym;
  1486. pt : tcallparanode;
  1487. checkstack : psymtablestackitem;
  1488. hashedid : THashedIDString;
  1489. begin
  1490. if not assigned(sym) then
  1491. internalerror(200411015);
  1492. FProcSym:=sym;
  1493. FProcs:=nil;
  1494. FProccnt:=0;
  1495. FProcvisiblecnt:=0;
  1496. FParanode:=ppn;
  1497. FAllowVariant:=true;
  1498. { determine length of parameter list }
  1499. pt:=tcallparanode(ppn);
  1500. FParalength:=0;
  1501. while assigned(pt) do
  1502. begin
  1503. inc(FParalength);
  1504. pt:=tcallparanode(pt.right);
  1505. end;
  1506. { when the definition has overload directive set, we search for
  1507. overloaded definitions in the class, this only needs to be done once
  1508. for class entries as the tree keeps always the same }
  1509. if (not sym.overloadchecked) and
  1510. (sym.owner.symtabletype=ObjectSymtable) and
  1511. (po_overload in tprocdef(sym.ProcdefList[0]).procoptions) then
  1512. search_class_overloads(sym);
  1513. { when the class passed is defined in this unit we
  1514. need to use the scope of that class. This is a trick
  1515. that can be used to access protected members in other
  1516. units. At least kylix supports it this way (PFV) }
  1517. if assigned(st) and
  1518. (
  1519. (st.symtabletype=ObjectSymtable) or
  1520. ((st.symtabletype=withsymtable) and
  1521. (st.defowner.typ=objectdef))
  1522. ) and
  1523. (st.defowner.owner.symtabletype in [globalsymtable,staticsymtable]) and
  1524. st.defowner.owner.iscurrentunit then
  1525. topclassh:=tobjectdef(st.defowner)
  1526. else
  1527. begin
  1528. if assigned(current_procinfo) then
  1529. topclassh:=current_procinfo.procdef._class
  1530. else
  1531. topclassh:=nil;
  1532. end;
  1533. { link all procedures which have the same # of parameters }
  1534. for j:=0 to sym.ProcdefList.Count-1 do
  1535. begin
  1536. pd:=tprocdef(sym.ProcdefList[j]);
  1537. { Is the procdef visible? This needs to be checked on
  1538. procdef level since a symbol can contain both private and
  1539. public declarations. But the check should not be done
  1540. when the callnode is generated by a property
  1541. inherited overrides invisible anonymous inherited (FK) }
  1542. if isprop or ignorevis or
  1543. (pd.owner.symtabletype<>ObjectSymtable) or
  1544. pd.is_visible_for_object(topclassh,nil) then
  1545. begin
  1546. { we have at least one procedure that is visible }
  1547. inc(FProcvisiblecnt);
  1548. { only when the # of parameter are supported by the
  1549. procedure }
  1550. if (FParalength>=pd.minparacount) and
  1551. ((po_varargs in pd.procoptions) or { varargs }
  1552. (FParalength<=pd.maxparacount)) then
  1553. proc_add(sym,pd);
  1554. end;
  1555. end;
  1556. { remember if the procedure is declared with the overload directive,
  1557. it's information is still needed also after all procs are removed }
  1558. has_overload_directive:=(po_overload in tprocdef(sym.ProcdefList[0]).procoptions);
  1559. { when the definition has overload directive set, we search for
  1560. overloaded definitions in the symtablestack. The found
  1561. entries are only added to the procs list and not the procsym, because
  1562. the list can change in every situation }
  1563. if has_overload_directive and
  1564. (sym.owner.symtabletype<>ObjectSymtable) then
  1565. begin
  1566. srsymtable:=sym.owner;
  1567. checkstack:=symtablestack.stack;
  1568. while assigned(checkstack) and
  1569. (checkstack^.symtable<>srsymtable) do
  1570. checkstack:=checkstack^.next;
  1571. { we've already processed the current symtable, start with
  1572. the next symtable in the stack }
  1573. if assigned(checkstack) then
  1574. checkstack:=checkstack^.next;
  1575. hashedid.id:=sym.name;
  1576. while assigned(checkstack) do
  1577. begin
  1578. srsymtable:=checkstack^.symtable;
  1579. if srsymtable.symtabletype in [localsymtable,staticsymtable,globalsymtable] then
  1580. begin
  1581. srprocsym:=tprocsym(srsymtable.FindWithHash(hashedid));
  1582. if assigned(srprocsym) and
  1583. (srprocsym.typ=procsym) then
  1584. begin
  1585. { if this visible procedure doesn't have overload we can stop
  1586. searching }
  1587. if not(po_overload in tprocdef(srprocsym.ProcdefList[0]).procoptions) and
  1588. tprocdef(srprocsym.ProcdefList[0]).is_visible_for_object(topclassh,nil) then
  1589. break;
  1590. { process all overloaded definitions }
  1591. for j:=0 to srprocsym.ProcdefList.Count-1 do
  1592. begin
  1593. pd:=tprocdef(srprocsym.ProcdefList[j]);
  1594. { only visible procedures need to be added }
  1595. if pd.is_visible_for_object(topclassh,nil) then
  1596. begin
  1597. { only when the # of parameter are supported by the
  1598. procedure }
  1599. if (FParalength>=pd.minparacount) and
  1600. ((po_varargs in pd.procoptions) or { varargs }
  1601. (FParalength<=pd.maxparacount)) then
  1602. begin
  1603. found:=false;
  1604. hp:=FProcs;
  1605. while assigned(hp) do
  1606. begin
  1607. { Only compare visible parameters for the user }
  1608. if compare_paras(hp^.data.paras,pd.paras,cp_value_equal_const,[cpo_ignorehidden])>=te_equal then
  1609. begin
  1610. found:=true;
  1611. break;
  1612. end;
  1613. hp:=hp^.next;
  1614. end;
  1615. if not found then
  1616. proc_add(srprocsym,pd);
  1617. end;
  1618. end;
  1619. end;
  1620. end;
  1621. end;
  1622. checkstack:=checkstack^.next;
  1623. end;
  1624. end;
  1625. end;
  1626. constructor tcallcandidates.create_operator(op:ttoken;ppn:tnode);
  1627. var
  1628. j : integer;
  1629. pd : tprocdef;
  1630. hp : pcandidate;
  1631. found : boolean;
  1632. srsymtable : TSymtable;
  1633. srprocsym : tprocsym;
  1634. pt : tcallparanode;
  1635. checkstack : psymtablestackitem;
  1636. hashedid : THashedIDString;
  1637. begin
  1638. FProcSym:=nil;
  1639. FProcs:=nil;
  1640. FProccnt:=0;
  1641. FProcvisiblecnt:=0;
  1642. FParanode:=ppn;
  1643. FAllowVariant:=false;
  1644. { determine length of parameter list }
  1645. pt:=tcallparanode(ppn);
  1646. FParalength:=0;
  1647. while assigned(pt) do
  1648. begin
  1649. if pt.resultdef.typ=variantdef then
  1650. FAllowVariant:=true;
  1651. inc(FParalength);
  1652. pt:=tcallparanode(pt.right);
  1653. end;
  1654. { we search all overloaded operator definitions in the symtablestack. The found
  1655. entries are only added to the procs list and not the procsym, because
  1656. the list can change in every situation }
  1657. hashedid.id:=overloaded_names[op];
  1658. checkstack:=symtablestack.stack;
  1659. while assigned(checkstack) do
  1660. begin
  1661. srsymtable:=checkstack^.symtable;
  1662. if srsymtable.symtabletype in [localsymtable,staticsymtable,globalsymtable] then
  1663. begin
  1664. srprocsym:=tprocsym(srsymtable.FindWithHash(hashedid));
  1665. if assigned(srprocsym) and
  1666. (srprocsym.typ=procsym) then
  1667. begin
  1668. { Store first procsym found }
  1669. if not assigned(FProcsym) then
  1670. FProcsym:=srprocsym;
  1671. { process all overloaded definitions }
  1672. for j:=0 to srprocsym.ProcdefList.Count-1 do
  1673. begin
  1674. pd:=tprocdef(srprocsym.ProcdefList[j]);
  1675. { only when the # of parameter are supported by the
  1676. procedure }
  1677. if (FParalength>=pd.minparacount) and
  1678. (FParalength<=pd.maxparacount) then
  1679. begin
  1680. found:=false;
  1681. hp:=FProcs;
  1682. while assigned(hp) do
  1683. begin
  1684. { Only compare visible parameters for the user }
  1685. if compare_paras(hp^.data.paras,pd.paras,cp_value_equal_const,[cpo_ignorehidden])>=te_equal then
  1686. begin
  1687. found:=true;
  1688. break;
  1689. end;
  1690. hp:=hp^.next;
  1691. end;
  1692. if not found then
  1693. proc_add(srprocsym,pd);
  1694. end;
  1695. end;
  1696. end;
  1697. end;
  1698. checkstack:=checkstack^.next;
  1699. end;
  1700. end;
  1701. destructor tcallcandidates.destroy;
  1702. var
  1703. hpnext,
  1704. hp : pcandidate;
  1705. begin
  1706. hp:=FProcs;
  1707. while assigned(hp) do
  1708. begin
  1709. hpnext:=hp^.next;
  1710. dispose(hp);
  1711. hp:=hpnext;
  1712. end;
  1713. end;
  1714. function tcallcandidates.proc_add(ps:tprocsym;pd:tprocdef):pcandidate;
  1715. var
  1716. defaultparacnt : integer;
  1717. begin
  1718. { generate new candidate entry }
  1719. new(result);
  1720. fillchar(result^,sizeof(tcandidate),0);
  1721. result^.data:=pd;
  1722. result^.next:=FProcs;
  1723. FProcs:=result;
  1724. inc(FProccnt);
  1725. { Find last parameter, skip all default parameters
  1726. that are not passed. Ignore this skipping for varargs }
  1727. result^.firstparaidx:=pd.paras.count-1;
  1728. if not(po_varargs in pd.procoptions) then
  1729. begin
  1730. { ignore hidden parameters }
  1731. while (result^.firstparaidx>=0) and (vo_is_hidden_para in tparavarsym(pd.paras[result^.firstparaidx]).varoptions) do
  1732. dec(result^.firstparaidx);
  1733. defaultparacnt:=pd.maxparacount-FParalength;
  1734. if defaultparacnt>0 then
  1735. begin
  1736. if defaultparacnt>result^.firstparaidx+1 then
  1737. internalerror(200401141);
  1738. dec(result^.firstparaidx,defaultparacnt);
  1739. end;
  1740. end;
  1741. { Give a small penalty for overloaded methods not in
  1742. defined the current class/unit }
  1743. if ps.owner<>pd.owner then
  1744. result^.ordinal_distance:=result^.ordinal_distance+1.0;
  1745. end;
  1746. procedure tcallcandidates.list(all:boolean);
  1747. var
  1748. hp : pcandidate;
  1749. begin
  1750. hp:=FProcs;
  1751. while assigned(hp) do
  1752. begin
  1753. if all or
  1754. (not hp^.invalid) then
  1755. MessagePos1(hp^.data.fileinfo,sym_h_param_list,hp^.data.fullprocname(false));
  1756. hp:=hp^.next;
  1757. end;
  1758. end;
  1759. {$ifdef EXTDEBUG}
  1760. procedure tcallcandidates.dump_info(lvl:longint);
  1761. function ParaTreeStr(p:tcallparanode):string;
  1762. begin
  1763. result:='';
  1764. while assigned(p) do
  1765. begin
  1766. if result<>'' then
  1767. result:=','+result;
  1768. result:=p.resultdef.typename+result;
  1769. p:=tcallparanode(p.right);
  1770. end;
  1771. end;
  1772. var
  1773. hp : pcandidate;
  1774. i : integer;
  1775. currpara : tparavarsym;
  1776. begin
  1777. if not CheckVerbosity(lvl) then
  1778. exit;
  1779. Comment(lvl+V_LineInfo,'Overloaded callnode: '+FProcSym.name+'('+ParaTreeStr(tcallparanode(FParaNode))+')');
  1780. hp:=FProcs;
  1781. while assigned(hp) do
  1782. begin
  1783. Comment(lvl,' '+hp^.data.fullprocname(false));
  1784. if (hp^.invalid) then
  1785. Comment(lvl,' invalid')
  1786. else
  1787. begin
  1788. Comment(lvl,' ex: '+tostr(hp^.exact_count)+
  1789. ' eq: '+tostr(hp^.equal_count)+
  1790. ' l1: '+tostr(hp^.cl1_count)+
  1791. ' l2: '+tostr(hp^.cl2_count)+
  1792. ' l3: '+tostr(hp^.cl3_count)+
  1793. ' l4: '+tostr(hp^.cl4_count)+
  1794. ' l5: '+tostr(hp^.cl5_count)+
  1795. ' oper: '+tostr(hp^.coper_count)+
  1796. ' ord: '+realtostr(hp^.ordinal_distance));
  1797. { Print parameters in left-right order }
  1798. for i:=0 to hp^.data.paras.count-1 do
  1799. begin
  1800. currpara:=tparavarsym(hp^.data.paras[i]);
  1801. if not(vo_is_hidden_para in currpara.varoptions) then
  1802. Comment(lvl,' - '+currpara.vardef.typename+' : '+EqualTypeName[currpara.eqval]);
  1803. end;
  1804. end;
  1805. hp:=hp^.next;
  1806. end;
  1807. end;
  1808. {$endif EXTDEBUG}
  1809. procedure tcallcandidates.get_information;
  1810. var
  1811. hp : pcandidate;
  1812. currpara : tparavarsym;
  1813. paraidx : integer;
  1814. currparanr : byte;
  1815. rfh,rth : double;
  1816. objdef : tobjectdef;
  1817. def_from,
  1818. def_to : tdef;
  1819. currpt,
  1820. pt : tcallparanode;
  1821. eq : tequaltype;
  1822. convtype : tconverttype;
  1823. pdtemp,
  1824. pdoper : tprocdef;
  1825. releasecurrpt : boolean;
  1826. cdoptions : tcompare_defs_options;
  1827. n : tnode;
  1828. {$ifopt r+}{$define ena_r}{$r-}{$endif}
  1829. {$ifopt q+}{$define ena_q}{$q-}{$endif}
  1830. const
  1831. inf=1.0/0.0;
  1832. {$ifdef ena_r}{$r+}{$endif}
  1833. {$ifdef ena_q}{$q+}{$endif}
  1834. begin
  1835. cdoptions:=[cdo_check_operator];
  1836. if FAllowVariant then
  1837. include(cdoptions,cdo_allow_variant);
  1838. { process all procs }
  1839. hp:=FProcs;
  1840. while assigned(hp) do
  1841. begin
  1842. { We compare parameters in reverse order (right to left),
  1843. the firstpara is already pointing to the last parameter
  1844. were we need to start comparing }
  1845. currparanr:=FParalength;
  1846. paraidx:=hp^.firstparaidx;
  1847. while (paraidx>=0) and (vo_is_hidden_para in tparavarsym(hp^.data.paras[paraidx]).varoptions) do
  1848. dec(paraidx);
  1849. pt:=tcallparanode(FParaNode);
  1850. while assigned(pt) and (paraidx>=0) do
  1851. begin
  1852. currpara:=tparavarsym(hp^.data.paras[paraidx]);
  1853. { currpt can be changed from loadn to calln when a procvar
  1854. is passed. This is to prevent that the change is permanent }
  1855. currpt:=pt;
  1856. releasecurrpt:=false;
  1857. { retrieve current parameter definitions to compares }
  1858. eq:=te_incompatible;
  1859. def_from:=currpt.resultdef;
  1860. def_to:=currpara.vardef;
  1861. if not(assigned(def_from)) then
  1862. internalerror(200212091);
  1863. if not(
  1864. assigned(def_to) or
  1865. ((po_varargs in hp^.data.procoptions) and
  1866. (currparanr>hp^.data.minparacount))
  1867. ) then
  1868. internalerror(200212092);
  1869. { Convert tp procvars when not expecting a procvar }
  1870. if (currpt.left.resultdef.typ=procvardef) and
  1871. not(def_to.typ in [procvardef,formaldef]) and
  1872. { Only convert to call when there is no overload or the return type
  1873. is equal to the expected type. }
  1874. (
  1875. (count=1) or
  1876. equal_defs(tprocvardef(currpt.left.resultdef).returndef,def_to)
  1877. ) then
  1878. begin
  1879. releasecurrpt:=true;
  1880. currpt:=tcallparanode(pt.getcopy);
  1881. if maybe_call_procvar(currpt.left,true) then
  1882. begin
  1883. currpt.resultdef:=currpt.left.resultdef;
  1884. def_from:=currpt.left.resultdef;
  1885. end;
  1886. end;
  1887. { If we expect a procvar and the left is loadnode that
  1888. returns a procdef we need to find the correct overloaded
  1889. procdef that matches the expected procvar. The loadnode
  1890. temporary returned the first procdef (PFV) }
  1891. if (def_to.typ=procvardef) and
  1892. (currpt.left.nodetype=loadn) and
  1893. (currpt.left.resultdef.typ=procdef) then
  1894. begin
  1895. pdtemp:=tprocsym(Tloadnode(currpt.left).symtableentry).Find_procdef_byprocvardef(Tprocvardef(def_to));
  1896. if assigned(pdtemp) then
  1897. begin
  1898. tloadnode(currpt.left).setprocdef(pdtemp);
  1899. currpt.resultdef:=currpt.left.resultdef;
  1900. def_from:=currpt.left.resultdef;
  1901. end;
  1902. end;
  1903. { varargs are always equal, but not exact }
  1904. if (po_varargs in hp^.data.procoptions) and
  1905. (currparanr>hp^.data.minparacount) and
  1906. not is_array_of_const(def_from) and
  1907. not is_array_constructor(def_from) then
  1908. eq:=te_equal
  1909. else
  1910. { same definition -> exact }
  1911. if (def_from=def_to) then
  1912. eq:=te_exact
  1913. else
  1914. { for value and const parameters check if a integer is constant or
  1915. included in other integer -> equal and calc ordinal_distance }
  1916. if not(currpara.varspez in [vs_var,vs_out]) and
  1917. is_integer(def_from) and
  1918. is_integer(def_to) and
  1919. is_in_limit(def_from,def_to) then
  1920. begin
  1921. eq:=te_equal;
  1922. hp^.ordinal_distance:=hp^.ordinal_distance+
  1923. abs(bestreal(torddef(def_from).low)-bestreal(torddef(def_to).low));
  1924. rth:=bestreal(torddef(def_to).high);
  1925. rfh:=bestreal(torddef(def_from).high);
  1926. hp^.ordinal_distance:=hp^.ordinal_distance+abs(rth-rfh);
  1927. { Give wrong sign a small penalty, this is need to get a diffrence
  1928. from word->[longword,longint] }
  1929. if is_signed(def_from)<>is_signed(def_to) then
  1930. {$ifopt r+}{$define ena_rq}{$q-}{$r-}{$endif}
  1931. hp^.ordinal_distance:=nextafter(hp^.ordinal_distance,inf);
  1932. {$ifdef ena_rq}{$r+}{$q+}{$endif}
  1933. end
  1934. else
  1935. { for value and const parameters check precision of real, give
  1936. penalty for loosing of precision. var and out parameters must match exactly }
  1937. if not(currpara.varspez in [vs_var,vs_out]) and
  1938. is_real(def_from) and
  1939. is_real(def_to) then
  1940. begin
  1941. eq:=te_equal;
  1942. if is_extended(def_to) then
  1943. rth:=4
  1944. else
  1945. if is_double (def_to) then
  1946. rth:=2
  1947. else
  1948. rth:=1;
  1949. if is_extended(def_from) then
  1950. rfh:=4
  1951. else
  1952. if is_double (def_from) then
  1953. rfh:=2
  1954. else
  1955. rfh:=1;
  1956. { penalty for shrinking of precision }
  1957. if rth<rfh then
  1958. rfh:=(rfh-rth)*16
  1959. else
  1960. rfh:=rth-rfh;
  1961. hp^.ordinal_distance:=hp^.ordinal_distance+rfh;
  1962. end
  1963. else
  1964. { related object parameters also need to determine the distance between the current
  1965. object and the object we are comparing with. var and out parameters must match exactly }
  1966. if not(currpara.varspez in [vs_var,vs_out]) and
  1967. (def_from.typ=objectdef) and
  1968. (def_to.typ=objectdef) and
  1969. (tobjectdef(def_from).objecttype=tobjectdef(def_to).objecttype) and
  1970. tobjectdef(def_from).is_related(tobjectdef(def_to)) then
  1971. begin
  1972. eq:=te_convert_l1;
  1973. objdef:=tobjectdef(def_from);
  1974. while assigned(objdef) do
  1975. begin
  1976. if objdef=def_to then
  1977. break;
  1978. hp^.ordinal_distance:=hp^.ordinal_distance+1;
  1979. objdef:=objdef.childof;
  1980. end;
  1981. end
  1982. { compare_defs_ext compares sets and array constructors very poorly because
  1983. it has too little information. So we do explicitly a detailed comparisation,
  1984. see also bug #11288 (FK)
  1985. }
  1986. else if (def_to.typ=setdef) and is_array_constructor(currpt.left.resultdef) then
  1987. begin
  1988. n:=currpt.left.getcopy;
  1989. arrayconstructor_to_set(n);
  1990. eq:=compare_defs_ext(n.resultdef,def_to,n.nodetype,convtype,pdoper,cdoptions);
  1991. n.free;
  1992. end
  1993. else
  1994. { generic type comparision }
  1995. begin
  1996. eq:=compare_defs_ext(def_from,def_to,currpt.left.nodetype,convtype,pdoper,cdoptions);
  1997. { when the types are not equal we need to check
  1998. some special case for parameter passing }
  1999. if (eq<te_equal) then
  2000. begin
  2001. if currpara.varspez in [vs_var,vs_out] then
  2002. begin
  2003. { para requires an equal type so the previous found
  2004. match was not good enough, reset to incompatible }
  2005. eq:=te_incompatible;
  2006. { var_para_allowed will return te_equal and te_convert_l1 to
  2007. make a difference for best matching }
  2008. var_para_allowed(eq,currpt.resultdef,currpara.vardef,currpt.left)
  2009. end
  2010. else
  2011. para_allowed(eq,currpt,def_to);
  2012. end;
  2013. end;
  2014. { when a procvar was changed to a call an exact much is
  2015. downgraded to equal. This way an overload call with the
  2016. procvar is choosen. See tb0471 (PFV) }
  2017. if (pt<>currpt) and (eq=te_exact) then
  2018. eq:=te_equal;
  2019. { increase correct counter }
  2020. case eq of
  2021. te_exact :
  2022. inc(hp^.exact_count);
  2023. te_equal :
  2024. inc(hp^.equal_count);
  2025. te_convert_l1 :
  2026. inc(hp^.cl1_count);
  2027. te_convert_l2 :
  2028. inc(hp^.cl2_count);
  2029. te_convert_l3 :
  2030. inc(hp^.cl3_count);
  2031. te_convert_l4 :
  2032. inc(hp^.cl4_count);
  2033. te_convert_l5 :
  2034. inc(hp^.cl5_count);
  2035. te_convert_operator :
  2036. inc(hp^.coper_count);
  2037. te_incompatible :
  2038. hp^.invalid:=true;
  2039. else
  2040. internalerror(200212072);
  2041. end;
  2042. { stop checking when an incompatible parameter is found }
  2043. if hp^.invalid then
  2044. begin
  2045. { store the current parameter info for
  2046. a nice error message when no procedure is found }
  2047. hp^.wrongparaidx:=paraidx;
  2048. hp^.wrongparanr:=currparanr;
  2049. break;
  2050. end;
  2051. {$ifdef EXTDEBUG}
  2052. { store equal in node tree for dump }
  2053. currpara.eqval:=eq;
  2054. {$endif EXTDEBUG}
  2055. { maybe release temp currpt }
  2056. if releasecurrpt then
  2057. currpt.free;
  2058. { next parameter in the call tree }
  2059. pt:=tcallparanode(pt.right);
  2060. { next parameter for definition, only goto next para
  2061. if we're out of the varargs }
  2062. if not(po_varargs in hp^.data.procoptions) or
  2063. (currparanr<=hp^.data.maxparacount) then
  2064. begin
  2065. { Ignore vs_hidden parameters }
  2066. repeat
  2067. dec(paraidx);
  2068. until (paraidx<0) or not(vo_is_hidden_para in tparavarsym(hp^.data.paras[paraidx]).varoptions);
  2069. end;
  2070. dec(currparanr);
  2071. end;
  2072. if not(hp^.invalid) and
  2073. (assigned(pt) or (paraidx>=0) or (currparanr<>0)) then
  2074. internalerror(200212141);
  2075. { next candidate }
  2076. hp:=hp^.next;
  2077. end;
  2078. end;
  2079. function get_variantequaltype(def: tdef): tvariantequaltype;
  2080. const
  2081. variantorddef_cl: array[tordtype] of tvariantequaltype =
  2082. (tve_incompatible,tve_byte,tve_word,tve_cardinal,tve_chari64,
  2083. tve_shortint,tve_smallint,tve_longint,tve_chari64,
  2084. tve_boolformal,tve_boolformal,tve_boolformal,tve_boolformal,tve_boolformal,
  2085. tve_chari64,tve_chari64,tve_dblcurrency);
  2086. { TODO: fixme for 128 bit floats }
  2087. variantfloatdef_cl: array[tfloattype] of tvariantequaltype =
  2088. (tve_single,tve_dblcurrency,tve_extended,
  2089. tve_dblcurrency,tve_dblcurrency,tve_extended);
  2090. variantstringdef_cl: array[tstringtype] of tvariantequaltype =
  2091. (tve_sstring,tve_astring,tve_astring,tve_wstring,tve_ustring);
  2092. begin
  2093. case def.typ of
  2094. orddef:
  2095. begin
  2096. result:=variantorddef_cl[torddef(def).ordtype];
  2097. end;
  2098. floatdef:
  2099. begin
  2100. result:=variantfloatdef_cl[tfloatdef(def).floattype];
  2101. end;
  2102. stringdef:
  2103. begin
  2104. result:=variantstringdef_cl[tstringdef(def).stringtype];
  2105. end;
  2106. formaldef:
  2107. begin
  2108. result:=tve_boolformal;
  2109. end;
  2110. else
  2111. begin
  2112. result:=tve_incompatible;
  2113. end;
  2114. end
  2115. end;
  2116. function is_better_candidate(currpd,bestpd:pcandidate):integer;
  2117. var
  2118. res : integer;
  2119. begin
  2120. {
  2121. Return values:
  2122. > 0 when currpd is better than bestpd
  2123. < 0 when bestpd is better than currpd
  2124. = 0 when both are equal
  2125. To choose the best candidate we use the following order:
  2126. - Incompatible flag
  2127. - (Smaller) Number of convert operator parameters.
  2128. - (Smaller) Number of convertlevel 2 parameters.
  2129. - (Smaller) Number of convertlevel 1 parameters.
  2130. - (Bigger) Number of exact parameters.
  2131. - (Smaller) Number of equal parameters.
  2132. - (Smaller) Total of ordinal distance. For example, the distance of a word
  2133. to a byte is 65535-255=65280.
  2134. }
  2135. if bestpd^.invalid then
  2136. begin
  2137. if currpd^.invalid then
  2138. res:=0
  2139. else
  2140. res:=1;
  2141. end
  2142. else
  2143. if currpd^.invalid then
  2144. res:=-1
  2145. else
  2146. begin
  2147. { less operator parameters? }
  2148. res:=(bestpd^.coper_count-currpd^.coper_count);
  2149. if (res=0) then
  2150. begin
  2151. { less cl5 parameters? }
  2152. res:=(bestpd^.cl5_count-currpd^.cl5_count);
  2153. if (res=0) then
  2154. begin
  2155. { less cl4 parameters? }
  2156. res:=(bestpd^.cl4_count-currpd^.cl4_count);
  2157. if (res=0) then
  2158. begin
  2159. { less cl3 parameters? }
  2160. res:=(bestpd^.cl3_count-currpd^.cl3_count);
  2161. if (res=0) then
  2162. begin
  2163. { less cl2 parameters? }
  2164. res:=(bestpd^.cl2_count-currpd^.cl2_count);
  2165. if (res=0) then
  2166. begin
  2167. { less cl1 parameters? }
  2168. res:=(bestpd^.cl1_count-currpd^.cl1_count);
  2169. if (res=0) then
  2170. begin
  2171. { more exact parameters? }
  2172. res:=(currpd^.exact_count-bestpd^.exact_count);
  2173. if (res=0) then
  2174. begin
  2175. { less equal parameters? }
  2176. res:=(bestpd^.equal_count-currpd^.equal_count);
  2177. if (res=0) then
  2178. begin
  2179. { smaller ordinal distance? }
  2180. if (currpd^.ordinal_distance<bestpd^.ordinal_distance) then
  2181. res:=1
  2182. else
  2183. if (currpd^.ordinal_distance>bestpd^.ordinal_distance) then
  2184. res:=-1
  2185. else
  2186. res:=0;
  2187. end;
  2188. end;
  2189. end;
  2190. end;
  2191. end;
  2192. end;
  2193. end;
  2194. end;
  2195. end;
  2196. is_better_candidate:=res;
  2197. end;
  2198. { Delphi precedence rules extracted from test programs. Only valid if passing
  2199. a variant parameter to overloaded procedures expecting exactly one parameter.
  2200. single > (char, currency, int64, shortstring, ansistring, widestring, extended, double)
  2201. double/currency > (char, int64, shortstring, ansistring, widestring, extended)
  2202. extended > (char, int64, shortstring, ansistring, widestring)
  2203. longint/cardinal > (int64, shortstring, ansistring, widestring, extended, double, single, char, currency)
  2204. smallint > (longint, int64, shortstring, ansistring, widestring, extended, double single, char, currency);
  2205. word > (longint, cardinal, int64, shortstring, ansistring, widestring, extended, double single, char, currency);
  2206. shortint > (longint, smallint, int64, shortstring, ansistring, widestring, extended, double, single, char, currency)
  2207. byte > (longint, cardinal, word, smallint, int64, shortstring, ansistring, widestring, extended, double, single, char, currency);
  2208. boolean/formal > (char, int64, shortstring, ansistring, widestring)
  2209. shortstring > (char, int64, ansistring, widestring)
  2210. ansistring > (char, int64, widestring)
  2211. widestring > (char, int64)
  2212. Relations not mentioned mean that they conflict: no decision possible }
  2213. function is_better_candidate_single_variant(currpd,bestpd:pcandidate):integer;
  2214. function calculate_relation(const currvcl, bestvcl, testvcl:
  2215. tvariantequaltype; const conflictvcls: tvariantequaltypes):integer;
  2216. begin
  2217. { if (bestvcl=conflictvcl) or
  2218. (currvcl=conflictvcl) then
  2219. result:=0
  2220. else if (bestvcl=testvcl) then
  2221. result:=-1
  2222. else result:=1 }
  2223. result:=1-2*ord(bestvcl=testvcl)+
  2224. ord(currvcl in conflictvcls)-ord(bestvcl in conflictvcls);
  2225. end;
  2226. function getfirstrealparaidx(pd: pcandidate): integer;
  2227. begin
  2228. { can be different for currpd and bestpd in case of overloaded }
  2229. { functions, e.g. lowercase():char and lowercase():shortstring }
  2230. { (depending on the calling convention and parameter order) }
  2231. result:=pd^.firstparaidx;
  2232. while (result>=0) and (vo_is_hidden_para in tparavarsym(pd^.data.paras[result]).varoptions) do
  2233. dec(result);
  2234. if (vo_is_hidden_para in tparavarsym(pd^.data.paras[result]).varoptions) then
  2235. internalerror(2006122803);
  2236. end;
  2237. var
  2238. currpara, bestpara: tparavarsym;
  2239. currvcl, bestvcl: tvariantequaltype;
  2240. begin
  2241. {
  2242. Return values:
  2243. > 0 when currpd is better than bestpd
  2244. < 0 when bestpd is better than currpd
  2245. = 0 when both are equal
  2246. }
  2247. currpara:=tparavarsym(currpd^.data.paras[getfirstrealparaidx(currpd)]);
  2248. bestpara:=tparavarsym(bestpd^.data.paras[getfirstrealparaidx(bestpd)]);
  2249. { if one of the parameters is a regular variant, fall back to the }
  2250. { default algorithm }
  2251. if (currpara.vardef.typ = variantdef) or
  2252. (bestpara.vardef.typ = variantdef) then
  2253. begin
  2254. result:=is_better_candidate(currpd,bestpd);
  2255. exit;
  2256. end;
  2257. currvcl:=get_variantequaltype(currpara.vardef);
  2258. bestvcl:=get_variantequaltype(bestpara.vardef);
  2259. { sanity check }
  2260. result:=-5;
  2261. { if both are the same, there is a conflict }
  2262. if (currvcl=bestvcl) then
  2263. result:=0
  2264. { if one of the two cannot be used as variant, the other is better }
  2265. else if (bestvcl=tve_incompatible) then
  2266. result:=1
  2267. else if (currvcl=tve_incompatible) then
  2268. result:=-1
  2269. { boolean and formal are better than chari64str, but conflict with }
  2270. { everything else }
  2271. else if (currvcl=tve_boolformal) or
  2272. (bestvcl=tve_boolformal) then
  2273. if (currvcl=tve_boolformal) then
  2274. result:=ord(bestvcl in [tve_chari64,tve_sstring,tve_astring,tve_wstring,tve_ustring])
  2275. else
  2276. result:=-ord(currvcl in [tve_chari64,tve_sstring,tve_astring,tve_wstring,tve_ustring])
  2277. { byte is better than everything else (we assume both aren't byte, }
  2278. { since there's only one parameter and that one can't be the same) }
  2279. else if (currvcl=tve_byte) or
  2280. (bestvcl=tve_byte) then
  2281. result:=calculate_relation(currvcl,bestvcl,tve_byte,[tve_shortint])
  2282. { shortint conflicts with word and cardinal, but is better than }
  2283. { everything else but byte (which has already been handled) }
  2284. else if (currvcl=tve_shortint) or
  2285. (bestvcl=tve_shortint) then
  2286. result:=calculate_relation(currvcl,bestvcl,tve_shortint,[tve_word, tve_cardinal])
  2287. { word conflicts with smallint, but is better than everything else }
  2288. { but shortint and byte (which has already been handled) }
  2289. else if (currvcl=tve_word) or
  2290. (bestvcl=tve_word) then
  2291. result:=calculate_relation(currvcl,bestvcl,tve_word,[tve_smallint])
  2292. { smallint conflicts with cardinal, but is better than everything }
  2293. { which has not yet been tested }
  2294. else if (currvcl=tve_smallint) or
  2295. (bestvcl=tve_smallint) then
  2296. result:=calculate_relation(currvcl,bestvcl,tve_smallint,[tve_cardinal])
  2297. { cardinal conflicts with each longint and is better than everything }
  2298. { which has not yet been tested }
  2299. else if (currvcl=tve_cardinal) or
  2300. (bestvcl=tve_cardinal) then
  2301. result:=calculate_relation(currvcl,bestvcl,tve_cardinal,[tve_longint])
  2302. { longint is better than everything which has not yet been tested }
  2303. else if (currvcl=tve_longint) or
  2304. (bestvcl=tve_longint) then
  2305. { if bestvcl=tve_longint then
  2306. result:=-1
  2307. else
  2308. result:=1 }
  2309. result:=1-2*ord(bestvcl=tve_longint)
  2310. { single is better than everything left }
  2311. else if (currvcl=tve_single) or
  2312. (bestvcl=tve_single) then
  2313. result:=1-2*ord(bestvcl=tve_single)
  2314. { double/comp/currency are better than everything left, and conflict }
  2315. { with each other (but that's already tested) }
  2316. else if (currvcl=tve_dblcurrency) or
  2317. (bestvcl=tve_dblcurrency) then
  2318. result:=1-2*ord(bestvcl=tve_dblcurrency)
  2319. { extended is better than everything left }
  2320. else if (currvcl=tve_extended) or
  2321. (bestvcl=tve_extended) then
  2322. result:=1-2*ord(bestvcl=tve_extended)
  2323. { shortstring is better than everything left }
  2324. else if (currvcl=tve_sstring) or
  2325. (bestvcl=tve_sstring) then
  2326. result:=1-2*ord(bestvcl=tve_sstring)
  2327. { ansistring is better than everything left }
  2328. else if (currvcl=tve_astring) or
  2329. (bestvcl=tve_astring) then
  2330. result:=1-2*ord(bestvcl=tve_astring)
  2331. { widestring is better than everything left }
  2332. else if (currvcl=tve_wstring) or
  2333. (bestvcl=tve_wstring) then
  2334. result:=1-2*ord(bestvcl=tve_wstring)
  2335. { unicodestring is better than everything left }
  2336. else if (currvcl=tve_ustring) or
  2337. (bestvcl=tve_ustring) then
  2338. result:=1-2*ord(bestvcl=tve_ustring);
  2339. { all possibilities should have been checked now }
  2340. if (result=-5) then
  2341. internalerror(2006122805);
  2342. end;
  2343. function tcallcandidates.choose_best(var bestpd:tabstractprocdef; singlevariant: boolean):integer;
  2344. var
  2345. besthpstart,
  2346. hp : pcandidate;
  2347. cntpd,
  2348. res : integer;
  2349. begin
  2350. {
  2351. Returns the number of candidates left and the
  2352. first candidate is returned in pdbest
  2353. }
  2354. { Setup the first procdef as best, only count it as a result
  2355. when it is valid }
  2356. bestpd:=FProcs^.data;
  2357. if FProcs^.invalid then
  2358. cntpd:=0
  2359. else
  2360. cntpd:=1;
  2361. if assigned(FProcs^.next) then
  2362. begin
  2363. besthpstart:=FProcs;
  2364. hp:=FProcs^.next;
  2365. while assigned(hp) do
  2366. begin
  2367. if not singlevariant then
  2368. res:=is_better_candidate(hp,besthpstart)
  2369. else
  2370. res:=is_better_candidate_single_variant(hp,besthpstart);
  2371. if (res>0) then
  2372. begin
  2373. { hp is better, flag all procs to be incompatible }
  2374. while (besthpstart<>hp) do
  2375. begin
  2376. besthpstart^.invalid:=true;
  2377. besthpstart:=besthpstart^.next;
  2378. end;
  2379. { besthpstart is already set to hp }
  2380. bestpd:=besthpstart^.data;
  2381. cntpd:=1;
  2382. end
  2383. else
  2384. if (res<0) then
  2385. begin
  2386. { besthpstart is better, flag current hp to be incompatible }
  2387. hp^.invalid:=true;
  2388. end
  2389. else
  2390. begin
  2391. { res=0, both are valid }
  2392. if not hp^.invalid then
  2393. inc(cntpd);
  2394. end;
  2395. hp:=hp^.next;
  2396. end;
  2397. end;
  2398. result:=cntpd;
  2399. end;
  2400. procedure tcallcandidates.find_wrong_para;
  2401. var
  2402. currparanr : smallint;
  2403. hp : pcandidate;
  2404. pt : tcallparanode;
  2405. wrongpara : tparavarsym;
  2406. begin
  2407. { Only process the first overloaded procdef }
  2408. hp:=FProcs;
  2409. { Find callparanode corresponding to the argument }
  2410. pt:=tcallparanode(FParanode);
  2411. currparanr:=FParalength;
  2412. while assigned(pt) and
  2413. (currparanr>hp^.wrongparanr) do
  2414. begin
  2415. pt:=tcallparanode(pt.right);
  2416. dec(currparanr);
  2417. end;
  2418. if (currparanr<>hp^.wrongparanr) or
  2419. not assigned(pt) then
  2420. internalerror(200212094);
  2421. { Show error message, when it was a var or out parameter
  2422. guess that it is a missing typeconv }
  2423. wrongpara:=tparavarsym(hp^.data.paras[hp^.wrongparaidx]);
  2424. if wrongpara.varspez in [vs_var,vs_out] then
  2425. begin
  2426. { Maybe passing the correct type but passing a const to var parameter }
  2427. if (compare_defs(pt.resultdef,wrongpara.vardef,pt.nodetype)<>te_incompatible) and
  2428. not valid_for_var(pt.left,true) then
  2429. CGMessagePos(pt.left.fileinfo,type_e_variable_id_expected)
  2430. else
  2431. CGMessagePos3(pt.left.fileinfo,parser_e_call_by_ref_without_typeconv,tostr(hp^.wrongparanr),
  2432. FullTypeName(pt.left.resultdef,wrongpara.vardef),
  2433. FullTypeName(wrongpara.vardef,pt.left.resultdef))
  2434. end
  2435. else
  2436. CGMessagePos3(pt.left.fileinfo,type_e_wrong_parameter_type,tostr(hp^.wrongparanr),
  2437. FullTypeName(pt.left.resultdef,wrongpara.vardef),
  2438. FullTypeName(wrongpara.vardef,pt.left.resultdef));
  2439. end;
  2440. procedure check_hints(const srsym: tsym; const symoptions: tsymoptions);
  2441. begin
  2442. if not assigned(srsym) then
  2443. internalerror(200602051);
  2444. if sp_hint_deprecated in symoptions then
  2445. Message1(sym_w_deprecated_symbol,srsym.realname);
  2446. if sp_hint_experimental in symoptions then
  2447. Message1(sym_w_experimental_symbol,srsym.realname);
  2448. if sp_hint_platform in symoptions then
  2449. Message1(sym_w_non_portable_symbol,srsym.realname);
  2450. if sp_hint_unimplemented in symoptions then
  2451. Message1(sym_w_non_implemented_symbol,srsym.realname);
  2452. end;
  2453. procedure check_ranges(const location: tfileposinfo; source: tnode; destdef: tdef);
  2454. begin
  2455. if not(cs_check_ordinal_size in current_settings.localswitches) then
  2456. exit;
  2457. { check if the assignment may cause a range check error }
  2458. { if its not explicit, and only if the values are }
  2459. { ordinals, enumdef and floatdef }
  2460. if assigned(destdef) and
  2461. (destdef.typ in [enumdef,orddef,floatdef]) and
  2462. not is_boolean(destdef) and
  2463. assigned(source.resultdef) and
  2464. (source.resultdef.typ in [enumdef,orddef,floatdef]) and
  2465. not is_boolean(source.resultdef) and
  2466. not is_constrealnode(source) then
  2467. begin
  2468. if (destdef.size < source.resultdef.size) then
  2469. begin
  2470. if (cs_check_range in current_settings.localswitches) then
  2471. MessagePos(location,type_w_smaller_possible_range_check)
  2472. else
  2473. MessagePos(location,type_h_smaller_possible_range_check);
  2474. end;
  2475. end;
  2476. end;
  2477. end.