htypechk.pas 99 KB

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