htypechk.pas 107 KB

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