htypechk.pas 108 KB

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