htypechk.pas 100 KB

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