htypechk.pas 109 KB

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