htypechk.pas 107 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. This unit exports some help routines for the type checking
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit htypechk;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. cclasses,tokens,cpuinfo,
  22. node,globtype,
  23. symconst,symtype,symdef,symsym,symbase;
  24. type
  25. Ttok2nodeRec=record
  26. tok : ttoken;
  27. nod : tnodetype;
  28. op_overloading_supported : boolean;
  29. end;
  30. pcandidate = ^tcandidate;
  31. tcandidate = record
  32. next : pcandidate;
  33. data : tprocdef;
  34. wrongparaidx,
  35. firstparaidx : integer;
  36. exact_count,
  37. equal_count,
  38. cl1_count,
  39. cl2_count,
  40. cl3_count,
  41. cl4_count,
  42. cl5_count,
  43. coper_count : integer; { should be signed }
  44. ordinal_distance : double;
  45. invalid : boolean;
  46. wrongparanr : byte;
  47. end;
  48. tcallcandidates = class
  49. private
  50. FProcsym : tprocsym;
  51. FProcsymtable : tsymtable;
  52. FOperator : ttoken;
  53. FCandidateProcs : pcandidate;
  54. FProcCnt : integer;
  55. FParaNode : tnode;
  56. FParaLength : smallint;
  57. FAllowVariant : boolean;
  58. procedure collect_overloads_in_struct(ProcdefOverloadList:TFPObjectList);
  59. procedure collect_overloads_in_units(ProcdefOverloadList:TFPObjectList; objcidcall,explicitunit: boolean);
  60. procedure create_candidate_list(ignorevisibility,allowdefaultparas,objcidcall,explicitunit:boolean);
  61. function proc_add(ps:tprocsym;pd:tprocdef;objcidcall: boolean):pcandidate;
  62. public
  63. constructor create(sym:tprocsym;st:TSymtable;ppn:tnode;ignorevisibility,allowdefaultparas,objcidcall,explicitunit:boolean);
  64. constructor create_operator(op:ttoken;ppn:tnode);
  65. destructor destroy;override;
  66. procedure list(all:boolean);
  67. {$ifdef EXTDEBUG}
  68. procedure dump_info(lvl:longint);
  69. {$endif EXTDEBUG}
  70. procedure get_information;
  71. function choose_best(var bestpd:tabstractprocdef; singlevariant: boolean):integer;
  72. procedure find_wrong_para;
  73. property Count:integer read FProcCnt;
  74. end;
  75. type
  76. tregableinfoflag = (
  77. // can be put in a register if it's the address of a var/out/const parameter
  78. ra_addr_regable,
  79. // orthogonal to above flag: the address of the node is taken and may
  80. // possibly escape the block in which this node is declared (e.g. a
  81. // local variable is passed as var parameter to another procedure)
  82. ra_addr_taken);
  83. tregableinfoflags = set of tregableinfoflag;
  84. const
  85. tok2nodes=24;
  86. tok2node:array[1..tok2nodes] of ttok2noderec=(
  87. (tok:_PLUS ;nod:addn;op_overloading_supported:true), { binary overloading supported }
  88. (tok:_MINUS ;nod:subn;op_overloading_supported:true), { binary and unary overloading supported }
  89. (tok:_STAR ;nod:muln;op_overloading_supported:true), { binary overloading supported }
  90. (tok:_SLASH ;nod:slashn;op_overloading_supported:true), { binary overloading supported }
  91. (tok:_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.typename,arraytokeninfo[optoken].str);
  454. candidates.free;
  455. ppn.free;
  456. t:=cnothingnode.create;
  457. exit;
  458. end;
  459. { Retrieve information about the candidates }
  460. candidates.get_information;
  461. {$ifdef EXTDEBUG}
  462. { Display info when multiple candidates are found }
  463. candidates.dump_info(V_Debug);
  464. {$endif EXTDEBUG}
  465. cand_cnt:=candidates.choose_best(tabstractprocdef(operpd),false);
  466. { exit when no overloads are found }
  467. if cand_cnt=0 then
  468. begin
  469. CGMessage2(parser_e_operator_not_overloaded_2,ld.typename,arraytokeninfo[optoken].str);
  470. candidates.free;
  471. ppn.free;
  472. t:=cnothingnode.create;
  473. exit;
  474. end;
  475. { Multiple candidates left? }
  476. if cand_cnt>1 then
  477. begin
  478. CGMessage(type_e_cant_choose_overload_function);
  479. {$ifdef EXTDEBUG}
  480. candidates.dump_info(V_Hint);
  481. {$else EXTDEBUG}
  482. candidates.list(false);
  483. {$endif EXTDEBUG}
  484. { we'll just use the first candidate to make the
  485. call }
  486. end;
  487. candidates.free;
  488. addsymref(operpd.procsym);
  489. { the nil as symtable signs firstcalln that this is
  490. an overloaded operator }
  491. t:=ccallnode.create(ppn,Tprocsym(operpd.procsym),nil,nil,[]);
  492. { we already know the procdef to use, so it can
  493. skip the overload choosing in callnode.pass_typecheck }
  494. tcallnode(t).procdefinition:=operpd;
  495. end;
  496. function isbinaryoverloaded(var t : tnode) : boolean;
  497. var
  498. rd,ld : tdef;
  499. optoken : ttoken;
  500. operpd : tprocdef;
  501. ht : tnode;
  502. ppn : tcallparanode;
  503. 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.typename,arraytokeninfo[optoken].str,rd.typename);
  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 in [vs_const,vs_constref]) 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_struct(ProcdefOverloadList:TFPObjectList);
  1600. var
  1601. j : integer;
  1602. pd : tprocdef;
  1603. srsym : tsym;
  1604. structdef : tabstractrecorddef;
  1605. hashedid : THashedIDString;
  1606. hasoverload : boolean;
  1607. begin
  1608. structdef:=tabstractrecorddef(fprocsym.owner.defowner);
  1609. hashedid.id:=fprocsym.name;
  1610. hasoverload:=false;
  1611. while assigned(structdef) do
  1612. begin
  1613. srsym:=tprocsym(structdef.symtable.FindWithHash(hashedid));
  1614. if assigned(srsym) and
  1615. { Delphi allows hiding a property by a procedure with the same name }
  1616. (srsym.typ=procsym) then
  1617. begin
  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. if (structdef.typ=objectdef) then
  1633. structdef:=tobjectdef(structdef).childof
  1634. else
  1635. structdef:=nil;
  1636. end;
  1637. end;
  1638. procedure tcallcandidates.collect_overloads_in_units(ProcdefOverloadList:TFPObjectList; objcidcall,explicitunit: boolean);
  1639. var
  1640. j : integer;
  1641. pd : tprocdef;
  1642. srsymtable : TSymtable;
  1643. srsym : tsym;
  1644. checkstack : psymtablestackitem;
  1645. hashedid : THashedIDString;
  1646. hasoverload : boolean;
  1647. begin
  1648. { we search all overloaded operator definitions in the symtablestack. The found
  1649. entries are only added to the procs list and not the procsym, because
  1650. the list can change in every situation }
  1651. if FOperator=NOTOKEN then
  1652. begin
  1653. if not objcidcall then
  1654. hashedid.id:=FProcsym.name
  1655. else
  1656. hashedid.id:=class_helper_prefix+FProcsym.name;
  1657. end
  1658. else
  1659. hashedid.id:=overloaded_names[FOperator];
  1660. checkstack:=symtablestack.stack;
  1661. if assigned(FProcsymtable) then
  1662. begin
  1663. while assigned(checkstack) and
  1664. (checkstack^.symtable<>FProcsymtable) do
  1665. checkstack:=checkstack^.next;
  1666. end;
  1667. while assigned(checkstack) do
  1668. begin
  1669. srsymtable:=checkstack^.symtable;
  1670. { if the unit in which the routine has to be searched has been
  1671. specified explicitly, stop searching after its symtable(s) have
  1672. been checked (can be both the static and the global symtable
  1673. in case it's the current unit itself) }
  1674. if explicitunit and
  1675. (FProcsymtable.symtabletype in [globalsymtable,staticsymtable]) and
  1676. (srsymtable.moduleid<>FProcsymtable.moduleid) then
  1677. break;
  1678. if srsymtable.symtabletype in [localsymtable,staticsymtable,globalsymtable] then
  1679. begin
  1680. srsym:=tprocsym(srsymtable.FindWithHash(hashedid));
  1681. if assigned(srsym) and
  1682. (srsym.typ=procsym) then
  1683. begin
  1684. { Store first procsym found }
  1685. if not assigned(FProcsym) then
  1686. FProcsym:=tprocsym(srsym);
  1687. { add all definitions }
  1688. hasoverload:=false;
  1689. for j:=0 to tprocsym(srsym).ProcdefList.Count-1 do
  1690. begin
  1691. pd:=tprocdef(tprocsym(srsym).ProcdefList[j]);
  1692. if po_overload in pd.procoptions then
  1693. hasoverload:=true;
  1694. ProcdefOverloadList.Add(tprocsym(srsym).ProcdefList[j]);
  1695. end;
  1696. { when there is no explicit overload we stop searching,
  1697. except for Objective-C methods called via id }
  1698. if not hasoverload and
  1699. not objcidcall then
  1700. break;
  1701. end;
  1702. end;
  1703. checkstack:=checkstack^.next
  1704. end;
  1705. end;
  1706. procedure tcallcandidates.create_candidate_list(ignorevisibility,allowdefaultparas,objcidcall,explicitunit:boolean);
  1707. var
  1708. j : integer;
  1709. pd : tprocdef;
  1710. hp : pcandidate;
  1711. pt : tcallparanode;
  1712. found : boolean;
  1713. contextstructdef : tabstractrecorddef;
  1714. ProcdefOverloadList : TFPObjectList;
  1715. begin
  1716. FCandidateProcs:=nil;
  1717. { Find all available overloads for this procsym }
  1718. ProcdefOverloadList:=TFPObjectList.Create(false);
  1719. if not objcidcall and
  1720. (FOperator=NOTOKEN) and
  1721. (FProcsym.owner.symtabletype in [objectsymtable,recordsymtable]) then
  1722. collect_overloads_in_struct(ProcdefOverloadList)
  1723. else
  1724. collect_overloads_in_units(ProcdefOverloadList,objcidcall,explicitunit);
  1725. { determine length of parameter list.
  1726. for operators also enable the variant-operators if
  1727. a variant parameter is passed }
  1728. FParalength:=0;
  1729. FAllowVariant:=(FOperator=NOTOKEN);
  1730. pt:=tcallparanode(FParaNode);
  1731. while assigned(pt) do
  1732. begin
  1733. if (pt.resultdef.typ=variantdef) then
  1734. FAllowVariant:=true;
  1735. inc(FParalength);
  1736. pt:=tcallparanode(pt.right);
  1737. end;
  1738. { when the class passed is defined in this unit we
  1739. need to use the scope of that class. This is a trick
  1740. that can be used to access protected members in other
  1741. units. At least kylix supports it this way (PFV) }
  1742. if assigned(FProcSymtable) and
  1743. (
  1744. (FProcSymtable.symtabletype in [ObjectSymtable,recordsymtable]) or
  1745. ((FProcSymtable.symtabletype=withsymtable) and
  1746. (FProcSymtable.defowner.typ in [objectdef,recorddef]))
  1747. ) and
  1748. (FProcSymtable.defowner.owner.symtabletype in [globalsymtable,staticsymtable]) and
  1749. FProcSymtable.defowner.owner.iscurrentunit then
  1750. contextstructdef:=tabstractrecorddef(FProcSymtable.defowner)
  1751. else
  1752. contextstructdef:=current_structdef;
  1753. { Process all found overloads }
  1754. for j:=0 to ProcdefOverloadList.Count-1 do
  1755. begin
  1756. pd:=tprocdef(ProcdefOverloadList[j]);
  1757. { only when the # of parameter are supported by the procedure and
  1758. it is visible }
  1759. if (FParalength>=pd.minparacount) and
  1760. (
  1761. (
  1762. allowdefaultparas and
  1763. (
  1764. (FParalength<=pd.maxparacount) or
  1765. (po_varargs in pd.procoptions)
  1766. )
  1767. ) or
  1768. (
  1769. not allowdefaultparas and
  1770. (FParalength=pd.maxparacount)
  1771. )
  1772. ) and
  1773. (
  1774. ignorevisibility or
  1775. not (pd.owner.symtabletype in [objectsymtable,recordsymtable]) or
  1776. is_visible_for_object(pd,contextstructdef)
  1777. ) then
  1778. begin
  1779. { don't add duplicates, only compare visible parameters for the user }
  1780. found:=false;
  1781. hp:=FCandidateProcs;
  1782. while assigned(hp) do
  1783. begin
  1784. if (compare_paras(hp^.data.paras,pd.paras,cp_value_equal_const,[cpo_ignorehidden])>=te_equal) and
  1785. (not(po_objc in pd.procoptions) or
  1786. (pd.messageinf.str^=hp^.data.messageinf.str^)) then
  1787. begin
  1788. found:=true;
  1789. break;
  1790. end;
  1791. hp:=hp^.next;
  1792. end;
  1793. if not found then
  1794. proc_add(fprocsym,pd,objcidcall);
  1795. end;
  1796. end;
  1797. ProcdefOverloadList.Free;
  1798. end;
  1799. function tcallcandidates.proc_add(ps:tprocsym;pd:tprocdef;objcidcall: boolean):pcandidate;
  1800. var
  1801. defaultparacnt : integer;
  1802. parentst : tsymtable;
  1803. begin
  1804. { generate new candidate entry }
  1805. new(result);
  1806. fillchar(result^,sizeof(tcandidate),0);
  1807. result^.data:=pd;
  1808. result^.next:=FCandidateProcs;
  1809. FCandidateProcs:=result;
  1810. inc(FProccnt);
  1811. { Find last parameter, skip all default parameters
  1812. that are not passed. Ignore this skipping for varargs }
  1813. result^.firstparaidx:=pd.paras.count-1;
  1814. if not(po_varargs in pd.procoptions) then
  1815. begin
  1816. { ignore hidden parameters }
  1817. while (result^.firstparaidx>=0) and (vo_is_hidden_para in tparavarsym(pd.paras[result^.firstparaidx]).varoptions) do
  1818. dec(result^.firstparaidx);
  1819. defaultparacnt:=pd.maxparacount-FParalength;
  1820. if defaultparacnt>0 then
  1821. begin
  1822. if defaultparacnt>result^.firstparaidx+1 then
  1823. internalerror(200401141);
  1824. dec(result^.firstparaidx,defaultparacnt);
  1825. end;
  1826. end;
  1827. { Give a small penalty for overloaded methods not in
  1828. defined the current class/unit }
  1829. parentst:=ps.owner;
  1830. { when calling Objective-C methods via id.method, then the found
  1831. procsym will be inside an arbitrary ObjectSymtable, and we don't
  1832. want togive the methods of that particular objcclass precedence over
  1833. other methods, so instead check against the symtable in which this
  1834. objcclass is defined }
  1835. if objcidcall then
  1836. parentst:=parentst.defowner.owner;
  1837. if (parentst<>pd.owner) then
  1838. result^.ordinal_distance:=result^.ordinal_distance+1.0;
  1839. end;
  1840. procedure tcallcandidates.list(all:boolean);
  1841. var
  1842. hp : pcandidate;
  1843. begin
  1844. hp:=FCandidateProcs;
  1845. while assigned(hp) do
  1846. begin
  1847. if all or
  1848. (not hp^.invalid) then
  1849. MessagePos1(hp^.data.fileinfo,sym_h_param_list,hp^.data.fullprocname(false));
  1850. hp:=hp^.next;
  1851. end;
  1852. end;
  1853. {$ifdef EXTDEBUG}
  1854. procedure tcallcandidates.dump_info(lvl:longint);
  1855. function ParaTreeStr(p:tcallparanode):string;
  1856. begin
  1857. result:='';
  1858. while assigned(p) do
  1859. begin
  1860. if result<>'' then
  1861. result:=','+result;
  1862. result:=p.resultdef.typename+result;
  1863. p:=tcallparanode(p.right);
  1864. end;
  1865. end;
  1866. var
  1867. hp : pcandidate;
  1868. i : integer;
  1869. currpara : tparavarsym;
  1870. begin
  1871. if not CheckVerbosity(lvl) then
  1872. exit;
  1873. Comment(lvl+V_LineInfo,'Overloaded callnode: '+FProcsym.name+'('+ParaTreeStr(tcallparanode(FParaNode))+')');
  1874. hp:=FCandidateProcs;
  1875. while assigned(hp) do
  1876. begin
  1877. Comment(lvl,' '+hp^.data.fullprocname(false));
  1878. if (hp^.invalid) then
  1879. Comment(lvl,' invalid')
  1880. else
  1881. begin
  1882. Comment(lvl,' ex: '+tostr(hp^.exact_count)+
  1883. ' eq: '+tostr(hp^.equal_count)+
  1884. ' l1: '+tostr(hp^.cl1_count)+
  1885. ' l2: '+tostr(hp^.cl2_count)+
  1886. ' l3: '+tostr(hp^.cl3_count)+
  1887. ' l4: '+tostr(hp^.cl4_count)+
  1888. ' l5: '+tostr(hp^.cl5_count)+
  1889. ' oper: '+tostr(hp^.coper_count)+
  1890. ' ord: '+realtostr(hp^.ordinal_distance));
  1891. { Print parameters in left-right order }
  1892. for i:=0 to hp^.data.paras.count-1 do
  1893. begin
  1894. currpara:=tparavarsym(hp^.data.paras[i]);
  1895. if not(vo_is_hidden_para in currpara.varoptions) then
  1896. Comment(lvl,' - '+currpara.vardef.typename+' : '+EqualTypeName[currpara.eqval]);
  1897. end;
  1898. end;
  1899. hp:=hp^.next;
  1900. end;
  1901. end;
  1902. {$endif EXTDEBUG}
  1903. procedure tcallcandidates.get_information;
  1904. var
  1905. hp : pcandidate;
  1906. currpara : tparavarsym;
  1907. paraidx : integer;
  1908. currparanr : byte;
  1909. rfh,rth : double;
  1910. objdef : tobjectdef;
  1911. def_from,
  1912. def_to : tdef;
  1913. currpt,
  1914. pt : tcallparanode;
  1915. eq : tequaltype;
  1916. convtype : tconverttype;
  1917. pdtemp,
  1918. pdoper : tprocdef;
  1919. releasecurrpt : boolean;
  1920. cdoptions : tcompare_defs_options;
  1921. n : tnode;
  1922. {$ifopt r+}{$define ena_r}{$r-}{$endif}
  1923. {$ifopt q+}{$define ena_q}{$q-}{$endif}
  1924. const
  1925. inf=1.0/0.0;
  1926. {$ifdef ena_r}{$r+}{$endif}
  1927. {$ifdef ena_q}{$q+}{$endif}
  1928. begin
  1929. cdoptions:=[cdo_check_operator];
  1930. if FAllowVariant then
  1931. include(cdoptions,cdo_allow_variant);
  1932. { process all procs }
  1933. hp:=FCandidateProcs;
  1934. while assigned(hp) do
  1935. begin
  1936. { We compare parameters in reverse order (right to left),
  1937. the firstpara is already pointing to the last parameter
  1938. were we need to start comparing }
  1939. currparanr:=FParalength;
  1940. paraidx:=hp^.firstparaidx;
  1941. while (paraidx>=0) and (vo_is_hidden_para in tparavarsym(hp^.data.paras[paraidx]).varoptions) do
  1942. dec(paraidx);
  1943. pt:=tcallparanode(FParaNode);
  1944. while assigned(pt) and (paraidx>=0) do
  1945. begin
  1946. currpara:=tparavarsym(hp^.data.paras[paraidx]);
  1947. { currpt can be changed from loadn to calln when a procvar
  1948. is passed. This is to prevent that the change is permanent }
  1949. currpt:=pt;
  1950. releasecurrpt:=false;
  1951. { retrieve current parameter definitions to compares }
  1952. eq:=te_incompatible;
  1953. def_from:=currpt.resultdef;
  1954. def_to:=currpara.vardef;
  1955. if not(assigned(def_from)) then
  1956. internalerror(200212091);
  1957. if not(
  1958. assigned(def_to) or
  1959. ((po_varargs in hp^.data.procoptions) and
  1960. (currparanr>hp^.data.minparacount))
  1961. ) then
  1962. internalerror(200212092);
  1963. { Convert tp procvars when not expecting a procvar }
  1964. if (currpt.left.resultdef.typ=procvardef) and
  1965. not(def_to.typ in [procvardef,formaldef]) and
  1966. { Only convert to call when there is no overload or the return type
  1967. is equal to the expected type. }
  1968. (
  1969. (count=1) or
  1970. equal_defs(tprocvardef(currpt.left.resultdef).returndef,def_to)
  1971. ) then
  1972. begin
  1973. releasecurrpt:=true;
  1974. currpt:=tcallparanode(pt.getcopy);
  1975. if maybe_call_procvar(currpt.left,true) then
  1976. begin
  1977. currpt.resultdef:=currpt.left.resultdef;
  1978. def_from:=currpt.left.resultdef;
  1979. end;
  1980. end;
  1981. { If we expect a procvar and the left is loadnode that
  1982. returns a procdef we need to find the correct overloaded
  1983. procdef that matches the expected procvar. The loadnode
  1984. temporary returned the first procdef (PFV) }
  1985. if (def_to.typ=procvardef) and
  1986. (currpt.left.nodetype=loadn) and
  1987. (currpt.left.resultdef.typ=procdef) then
  1988. begin
  1989. pdtemp:=tprocsym(Tloadnode(currpt.left).symtableentry).Find_procdef_byprocvardef(Tprocvardef(def_to));
  1990. if assigned(pdtemp) then
  1991. begin
  1992. tloadnode(currpt.left).setprocdef(pdtemp);
  1993. currpt.resultdef:=currpt.left.resultdef;
  1994. def_from:=currpt.left.resultdef;
  1995. end;
  1996. end;
  1997. { varargs are always equal, but not exact }
  1998. if (po_varargs in hp^.data.procoptions) and
  1999. (currparanr>hp^.data.minparacount) and
  2000. not is_array_of_const(def_from) and
  2001. not is_array_constructor(def_from) then
  2002. eq:=te_equal
  2003. else
  2004. { same definition -> exact }
  2005. if (def_from=def_to) then
  2006. eq:=te_exact
  2007. else
  2008. { for value and const parameters check if a integer is constant or
  2009. included in other integer -> equal and calc ordinal_distance }
  2010. if not(currpara.varspez in [vs_var,vs_out]) and
  2011. is_integer(def_from) and
  2012. is_integer(def_to) and
  2013. is_in_limit(def_from,def_to) then
  2014. begin
  2015. eq:=te_equal;
  2016. hp^.ordinal_distance:=hp^.ordinal_distance+
  2017. abs(bestreal(torddef(def_from).low)-bestreal(torddef(def_to).low));
  2018. rth:=bestreal(torddef(def_to).high);
  2019. rfh:=bestreal(torddef(def_from).high);
  2020. hp^.ordinal_distance:=hp^.ordinal_distance+abs(rth-rfh);
  2021. { Give wrong sign a small penalty, this is need to get a diffrence
  2022. from word->[longword,longint] }
  2023. if is_signed(def_from)<>is_signed(def_to) then
  2024. {$push}
  2025. {$r-}
  2026. {$q-}
  2027. hp^.ordinal_distance:=nextafter(hp^.ordinal_distance,inf);
  2028. {$pop}
  2029. end
  2030. else
  2031. { for value and const parameters check precision of real, give
  2032. penalty for loosing of precision. var and out parameters must match exactly }
  2033. if not(currpara.varspez in [vs_var,vs_out]) and
  2034. is_real(def_from) and
  2035. is_real(def_to) then
  2036. begin
  2037. eq:=te_equal;
  2038. if is_extended(def_to) then
  2039. rth:=4
  2040. else
  2041. if is_double (def_to) then
  2042. rth:=2
  2043. else
  2044. rth:=1;
  2045. if is_extended(def_from) then
  2046. rfh:=4
  2047. else
  2048. if is_double (def_from) then
  2049. rfh:=2
  2050. else
  2051. rfh:=1;
  2052. { penalty for shrinking of precision }
  2053. if rth<rfh then
  2054. rfh:=(rfh-rth)*16
  2055. else
  2056. rfh:=rth-rfh;
  2057. hp^.ordinal_distance:=hp^.ordinal_distance+rfh;
  2058. end
  2059. else
  2060. { related object parameters also need to determine the distance between the current
  2061. object and the object we are comparing with. var and out parameters must match exactly }
  2062. if not(currpara.varspez in [vs_var,vs_out]) and
  2063. (def_from.typ=objectdef) and
  2064. (def_to.typ=objectdef) and
  2065. (tobjectdef(def_from).objecttype=tobjectdef(def_to).objecttype) and
  2066. tobjectdef(def_from).is_related(tobjectdef(def_to)) then
  2067. begin
  2068. eq:=te_convert_l1;
  2069. objdef:=tobjectdef(def_from);
  2070. while assigned(objdef) do
  2071. begin
  2072. if objdef=def_to then
  2073. break;
  2074. hp^.ordinal_distance:=hp^.ordinal_distance+1;
  2075. objdef:=objdef.childof;
  2076. end;
  2077. end
  2078. { compare_defs_ext compares sets and array constructors very poorly because
  2079. it has too little information. So we do explicitly a detailed comparisation,
  2080. see also bug #11288 (FK)
  2081. }
  2082. else if (def_to.typ=setdef) and is_array_constructor(currpt.left.resultdef) then
  2083. begin
  2084. n:=currpt.left.getcopy;
  2085. arrayconstructor_to_set(n);
  2086. eq:=compare_defs_ext(n.resultdef,def_to,n.nodetype,convtype,pdoper,cdoptions);
  2087. n.free;
  2088. end
  2089. else
  2090. { generic type comparision }
  2091. begin
  2092. eq:=compare_defs_ext(def_from,def_to,currpt.left.nodetype,convtype,pdoper,cdoptions);
  2093. { when the types are not equal we need to check
  2094. some special case for parameter passing }
  2095. if (eq<te_equal) then
  2096. begin
  2097. if currpara.varspez in [vs_var,vs_out] then
  2098. begin
  2099. { para requires an equal type so the previous found
  2100. match was not good enough, reset to incompatible }
  2101. eq:=te_incompatible;
  2102. { var_para_allowed will return te_equal and te_convert_l1 to
  2103. make a difference for best matching }
  2104. var_para_allowed(eq,currpt.resultdef,currpara.vardef,currpt.left)
  2105. end
  2106. else
  2107. para_allowed(eq,currpt,def_to);
  2108. end;
  2109. end;
  2110. { univ parameters match if the size matches (don't override the
  2111. comparison result if it was ok, since a match based on the
  2112. "univ" character is the lowest possible match) }
  2113. if (eq=te_incompatible) and
  2114. currpara.univpara and
  2115. is_valid_univ_para_type(def_from) and
  2116. (def_from.size=def_to.size) then
  2117. eq:=te_convert_l5;
  2118. { when a procvar was changed to a call an exact match is
  2119. downgraded to equal. This way an overload call with the
  2120. procvar is choosen. See tb0471 (PFV) }
  2121. if (pt<>currpt) and (eq=te_exact) then
  2122. eq:=te_equal;
  2123. { increase correct counter }
  2124. case eq of
  2125. te_exact :
  2126. inc(hp^.exact_count);
  2127. te_equal :
  2128. inc(hp^.equal_count);
  2129. te_convert_l1 :
  2130. inc(hp^.cl1_count);
  2131. te_convert_l2 :
  2132. inc(hp^.cl2_count);
  2133. te_convert_l3 :
  2134. inc(hp^.cl3_count);
  2135. te_convert_l4 :
  2136. inc(hp^.cl4_count);
  2137. te_convert_l5 :
  2138. inc(hp^.cl5_count);
  2139. te_convert_operator :
  2140. inc(hp^.coper_count);
  2141. te_incompatible :
  2142. hp^.invalid:=true;
  2143. else
  2144. internalerror(200212072);
  2145. end;
  2146. { stop checking when an incompatible parameter is found }
  2147. if hp^.invalid then
  2148. begin
  2149. { store the current parameter info for
  2150. a nice error message when no procedure is found }
  2151. hp^.wrongparaidx:=paraidx;
  2152. hp^.wrongparanr:=currparanr;
  2153. break;
  2154. end;
  2155. {$ifdef EXTDEBUG}
  2156. { store equal in node tree for dump }
  2157. currpara.eqval:=eq;
  2158. {$endif EXTDEBUG}
  2159. { maybe release temp currpt }
  2160. if releasecurrpt then
  2161. currpt.free;
  2162. { next parameter in the call tree }
  2163. pt:=tcallparanode(pt.right);
  2164. { next parameter for definition, only goto next para
  2165. if we're out of the varargs }
  2166. if not(po_varargs in hp^.data.procoptions) or
  2167. (currparanr<=hp^.data.maxparacount) then
  2168. begin
  2169. { Ignore vs_hidden parameters }
  2170. repeat
  2171. dec(paraidx);
  2172. until (paraidx<0) or not(vo_is_hidden_para in tparavarsym(hp^.data.paras[paraidx]).varoptions);
  2173. end;
  2174. dec(currparanr);
  2175. end;
  2176. if not(hp^.invalid) and
  2177. (assigned(pt) or (paraidx>=0) or (currparanr<>0)) then
  2178. internalerror(200212141);
  2179. { next candidate }
  2180. hp:=hp^.next;
  2181. end;
  2182. end;
  2183. function get_variantequaltype(def: tdef): tvariantequaltype;
  2184. const
  2185. variantorddef_cl: array[tordtype] of tvariantequaltype =
  2186. (tve_incompatible,tve_byte,tve_word,tve_cardinal,tve_chari64,
  2187. tve_shortint,tve_smallint,tve_longint,tve_chari64,
  2188. tve_boolformal,tve_boolformal,tve_boolformal,tve_boolformal,tve_boolformal,
  2189. tve_chari64,tve_chari64,tve_dblcurrency);
  2190. { TODO: fixme for 128 bit floats }
  2191. variantfloatdef_cl: array[tfloattype] of tvariantequaltype =
  2192. (tve_single,tve_dblcurrency,tve_extended,tve_extended,
  2193. tve_dblcurrency,tve_dblcurrency,tve_extended);
  2194. variantstringdef_cl: array[tstringtype] of tvariantequaltype =
  2195. (tve_sstring,tve_astring,tve_astring,tve_wstring,tve_ustring);
  2196. begin
  2197. case def.typ of
  2198. orddef:
  2199. begin
  2200. result:=variantorddef_cl[torddef(def).ordtype];
  2201. end;
  2202. floatdef:
  2203. begin
  2204. result:=variantfloatdef_cl[tfloatdef(def).floattype];
  2205. end;
  2206. stringdef:
  2207. begin
  2208. result:=variantstringdef_cl[tstringdef(def).stringtype];
  2209. end;
  2210. formaldef:
  2211. begin
  2212. result:=tve_boolformal;
  2213. end;
  2214. else
  2215. begin
  2216. result:=tve_incompatible;
  2217. end;
  2218. end
  2219. end;
  2220. function is_better_candidate(currpd,bestpd:pcandidate):integer;
  2221. var
  2222. res : integer;
  2223. begin
  2224. {
  2225. Return values:
  2226. > 0 when currpd is better than bestpd
  2227. < 0 when bestpd is better than currpd
  2228. = 0 when both are equal
  2229. To choose the best candidate we use the following order:
  2230. - Incompatible flag
  2231. - (Smaller) Number of convert operator parameters.
  2232. - (Smaller) Number of convertlevel 2 parameters.
  2233. - (Smaller) Number of convertlevel 1 parameters.
  2234. - (Bigger) Number of exact parameters.
  2235. - (Smaller) Number of equal parameters.
  2236. - (Smaller) Total of ordinal distance. For example, the distance of a word
  2237. to a byte is 65535-255=65280.
  2238. }
  2239. if bestpd^.invalid then
  2240. begin
  2241. if currpd^.invalid then
  2242. res:=0
  2243. else
  2244. res:=1;
  2245. end
  2246. else
  2247. if currpd^.invalid then
  2248. res:=-1
  2249. else
  2250. begin
  2251. { less operator parameters? }
  2252. res:=(bestpd^.coper_count-currpd^.coper_count);
  2253. if (res=0) then
  2254. begin
  2255. { less cl5 parameters? }
  2256. res:=(bestpd^.cl5_count-currpd^.cl5_count);
  2257. if (res=0) then
  2258. begin
  2259. { less cl4 parameters? }
  2260. res:=(bestpd^.cl4_count-currpd^.cl4_count);
  2261. if (res=0) then
  2262. begin
  2263. { less cl3 parameters? }
  2264. res:=(bestpd^.cl3_count-currpd^.cl3_count);
  2265. if (res=0) then
  2266. begin
  2267. { less cl2 parameters? }
  2268. res:=(bestpd^.cl2_count-currpd^.cl2_count);
  2269. if (res=0) then
  2270. begin
  2271. { less cl1 parameters? }
  2272. res:=(bestpd^.cl1_count-currpd^.cl1_count);
  2273. if (res=0) then
  2274. begin
  2275. { more exact parameters? }
  2276. res:=(currpd^.exact_count-bestpd^.exact_count);
  2277. if (res=0) then
  2278. begin
  2279. { less equal parameters? }
  2280. res:=(bestpd^.equal_count-currpd^.equal_count);
  2281. if (res=0) then
  2282. begin
  2283. { smaller ordinal distance? }
  2284. if (currpd^.ordinal_distance<bestpd^.ordinal_distance) then
  2285. res:=1
  2286. else
  2287. if (currpd^.ordinal_distance>bestpd^.ordinal_distance) then
  2288. res:=-1
  2289. else
  2290. res:=0;
  2291. end;
  2292. end;
  2293. end;
  2294. end;
  2295. end;
  2296. end;
  2297. end;
  2298. end;
  2299. end;
  2300. is_better_candidate:=res;
  2301. end;
  2302. { Delphi precedence rules extracted from test programs. Only valid if passing
  2303. a variant parameter to overloaded procedures expecting exactly one parameter.
  2304. single > (char, currency, int64, shortstring, ansistring, widestring, extended, double)
  2305. double/currency > (char, int64, shortstring, ansistring, widestring, extended)
  2306. extended > (char, int64, shortstring, ansistring, widestring)
  2307. longint/cardinal > (int64, shortstring, ansistring, widestring, extended, double, single, char, currency)
  2308. smallint > (longint, int64, shortstring, ansistring, widestring, extended, double single, char, currency);
  2309. word > (longint, cardinal, int64, shortstring, ansistring, widestring, extended, double single, char, currency);
  2310. shortint > (longint, smallint, int64, shortstring, ansistring, widestring, extended, double, single, char, currency)
  2311. byte > (longint, cardinal, word, smallint, int64, shortstring, ansistring, widestring, extended, double, single, char, currency);
  2312. boolean/formal > (char, int64, shortstring, ansistring, widestring)
  2313. shortstring > (char, int64, ansistring, widestring)
  2314. ansistring > (char, int64, widestring)
  2315. widestring > (char, int64)
  2316. Relations not mentioned mean that they conflict: no decision possible }
  2317. function is_better_candidate_single_variant(currpd,bestpd:pcandidate):integer;
  2318. function calculate_relation(const currvcl, bestvcl, testvcl:
  2319. tvariantequaltype; const conflictvcls: tvariantequaltypes):integer;
  2320. begin
  2321. { if (bestvcl=conflictvcl) or
  2322. (currvcl=conflictvcl) then
  2323. result:=0
  2324. else if (bestvcl=testvcl) then
  2325. result:=-1
  2326. else result:=1 }
  2327. result:=1-2*ord(bestvcl=testvcl)+
  2328. ord(currvcl in conflictvcls)-ord(bestvcl in conflictvcls);
  2329. end;
  2330. function getfirstrealparaidx(pd: pcandidate): integer;
  2331. begin
  2332. { can be different for currpd and bestpd in case of overloaded }
  2333. { functions, e.g. lowercase():char and lowercase():shortstring }
  2334. { (depending on the calling convention and parameter order) }
  2335. result:=pd^.firstparaidx;
  2336. while (result>=0) and (vo_is_hidden_para in tparavarsym(pd^.data.paras[result]).varoptions) do
  2337. dec(result);
  2338. if (vo_is_hidden_para in tparavarsym(pd^.data.paras[result]).varoptions) then
  2339. internalerror(2006122803);
  2340. end;
  2341. var
  2342. currpara, bestpara: tparavarsym;
  2343. currvcl, bestvcl: tvariantequaltype;
  2344. begin
  2345. {
  2346. Return values:
  2347. > 0 when currpd is better than bestpd
  2348. < 0 when bestpd is better than currpd
  2349. = 0 when both are equal
  2350. }
  2351. currpara:=tparavarsym(currpd^.data.paras[getfirstrealparaidx(currpd)]);
  2352. bestpara:=tparavarsym(bestpd^.data.paras[getfirstrealparaidx(bestpd)]);
  2353. { if one of the parameters is a regular variant, fall back to the }
  2354. { default algorithm }
  2355. if (currpara.vardef.typ = variantdef) or
  2356. (bestpara.vardef.typ = variantdef) then
  2357. begin
  2358. result:=is_better_candidate(currpd,bestpd);
  2359. exit;
  2360. end;
  2361. currvcl:=get_variantequaltype(currpara.vardef);
  2362. bestvcl:=get_variantequaltype(bestpara.vardef);
  2363. { sanity check }
  2364. result:=-5;
  2365. { if both are the same, there is a conflict }
  2366. if (currvcl=bestvcl) then
  2367. result:=0
  2368. { if one of the two cannot be used as variant, the other is better }
  2369. else if (bestvcl=tve_incompatible) then
  2370. result:=1
  2371. else if (currvcl=tve_incompatible) then
  2372. result:=-1
  2373. { boolean and formal are better than chari64str, but conflict with }
  2374. { everything else }
  2375. else if (currvcl=tve_boolformal) or
  2376. (bestvcl=tve_boolformal) then
  2377. if (currvcl=tve_boolformal) then
  2378. result:=ord(bestvcl in [tve_chari64,tve_sstring,tve_astring,tve_wstring,tve_ustring])
  2379. else
  2380. result:=-ord(currvcl in [tve_chari64,tve_sstring,tve_astring,tve_wstring,tve_ustring])
  2381. { byte is better than everything else (we assume both aren't byte, }
  2382. { since there's only one parameter and that one can't be the same) }
  2383. else if (currvcl=tve_byte) or
  2384. (bestvcl=tve_byte) then
  2385. result:=calculate_relation(currvcl,bestvcl,tve_byte,[tve_shortint])
  2386. { shortint conflicts with word and cardinal, but is better than }
  2387. { everything else but byte (which has already been handled) }
  2388. else if (currvcl=tve_shortint) or
  2389. (bestvcl=tve_shortint) then
  2390. result:=calculate_relation(currvcl,bestvcl,tve_shortint,[tve_word, tve_cardinal])
  2391. { word conflicts with smallint, but is better than everything else }
  2392. { but shortint and byte (which has already been handled) }
  2393. else if (currvcl=tve_word) or
  2394. (bestvcl=tve_word) then
  2395. result:=calculate_relation(currvcl,bestvcl,tve_word,[tve_smallint])
  2396. { smallint conflicts with cardinal, but is better than everything }
  2397. { which has not yet been tested }
  2398. else if (currvcl=tve_smallint) or
  2399. (bestvcl=tve_smallint) then
  2400. result:=calculate_relation(currvcl,bestvcl,tve_smallint,[tve_cardinal])
  2401. { cardinal conflicts with each longint and is better than everything }
  2402. { which has not yet been tested }
  2403. else if (currvcl=tve_cardinal) or
  2404. (bestvcl=tve_cardinal) then
  2405. result:=calculate_relation(currvcl,bestvcl,tve_cardinal,[tve_longint])
  2406. { longint is better than everything which has not yet been tested }
  2407. else if (currvcl=tve_longint) or
  2408. (bestvcl=tve_longint) then
  2409. { if bestvcl=tve_longint then
  2410. result:=-1
  2411. else
  2412. result:=1 }
  2413. result:=1-2*ord(bestvcl=tve_longint)
  2414. { single is better than everything left }
  2415. else if (currvcl=tve_single) or
  2416. (bestvcl=tve_single) then
  2417. result:=1-2*ord(bestvcl=tve_single)
  2418. { double/comp/currency are better than everything left, and conflict }
  2419. { with each other (but that's already tested) }
  2420. else if (currvcl=tve_dblcurrency) or
  2421. (bestvcl=tve_dblcurrency) then
  2422. result:=1-2*ord(bestvcl=tve_dblcurrency)
  2423. { extended is better than everything left }
  2424. else if (currvcl=tve_extended) or
  2425. (bestvcl=tve_extended) then
  2426. result:=1-2*ord(bestvcl=tve_extended)
  2427. { shortstring is better than everything left }
  2428. else if (currvcl=tve_sstring) or
  2429. (bestvcl=tve_sstring) then
  2430. result:=1-2*ord(bestvcl=tve_sstring)
  2431. { ansistring is better than everything left }
  2432. else if (currvcl=tve_astring) or
  2433. (bestvcl=tve_astring) then
  2434. result:=1-2*ord(bestvcl=tve_astring)
  2435. { widestring is better than everything left }
  2436. else if (currvcl=tve_wstring) or
  2437. (bestvcl=tve_wstring) then
  2438. result:=1-2*ord(bestvcl=tve_wstring)
  2439. { unicodestring is better than everything left }
  2440. else if (currvcl=tve_ustring) or
  2441. (bestvcl=tve_ustring) then
  2442. result:=1-2*ord(bestvcl=tve_ustring);
  2443. { all possibilities should have been checked now }
  2444. if (result=-5) then
  2445. internalerror(2006122805);
  2446. end;
  2447. function tcallcandidates.choose_best(var bestpd:tabstractprocdef; singlevariant: boolean):integer;
  2448. var
  2449. besthpstart,
  2450. hp : pcandidate;
  2451. cntpd,
  2452. res : integer;
  2453. begin
  2454. {
  2455. Returns the number of candidates left and the
  2456. first candidate is returned in pdbest
  2457. }
  2458. { Setup the first procdef as best, only count it as a result
  2459. when it is valid }
  2460. bestpd:=FCandidateProcs^.data;
  2461. if FCandidateProcs^.invalid then
  2462. cntpd:=0
  2463. else
  2464. cntpd:=1;
  2465. if assigned(FCandidateProcs^.next) then
  2466. begin
  2467. besthpstart:=FCandidateProcs;
  2468. hp:=FCandidateProcs^.next;
  2469. while assigned(hp) do
  2470. begin
  2471. if not singlevariant then
  2472. res:=is_better_candidate(hp,besthpstart)
  2473. else
  2474. res:=is_better_candidate_single_variant(hp,besthpstart);
  2475. if (res>0) then
  2476. begin
  2477. { hp is better, flag all procs to be incompatible }
  2478. while (besthpstart<>hp) do
  2479. begin
  2480. besthpstart^.invalid:=true;
  2481. besthpstart:=besthpstart^.next;
  2482. end;
  2483. { besthpstart is already set to hp }
  2484. bestpd:=besthpstart^.data;
  2485. cntpd:=1;
  2486. end
  2487. else
  2488. if (res<0) then
  2489. begin
  2490. { besthpstart is better, flag current hp to be incompatible }
  2491. hp^.invalid:=true;
  2492. end
  2493. else
  2494. begin
  2495. { res=0, both are valid }
  2496. if not hp^.invalid then
  2497. inc(cntpd);
  2498. end;
  2499. hp:=hp^.next;
  2500. end;
  2501. end;
  2502. result:=cntpd;
  2503. end;
  2504. procedure tcallcandidates.find_wrong_para;
  2505. var
  2506. currparanr : smallint;
  2507. hp : pcandidate;
  2508. pt : tcallparanode;
  2509. wrongpara : tparavarsym;
  2510. begin
  2511. { Only process the first overloaded procdef }
  2512. hp:=FCandidateProcs;
  2513. { Find callparanode corresponding to the argument }
  2514. pt:=tcallparanode(FParanode);
  2515. currparanr:=FParalength;
  2516. while assigned(pt) and
  2517. (currparanr>hp^.wrongparanr) do
  2518. begin
  2519. pt:=tcallparanode(pt.right);
  2520. dec(currparanr);
  2521. end;
  2522. if (currparanr<>hp^.wrongparanr) or
  2523. not assigned(pt) then
  2524. internalerror(200212094);
  2525. { Show error message, when it was a var or out parameter
  2526. guess that it is a missing typeconv }
  2527. wrongpara:=tparavarsym(hp^.data.paras[hp^.wrongparaidx]);
  2528. if wrongpara.varspez in [vs_var,vs_out] then
  2529. begin
  2530. { Maybe passing the correct type but passing a const to var parameter }
  2531. if (compare_defs(pt.resultdef,wrongpara.vardef,pt.nodetype)<>te_incompatible) and
  2532. not valid_for_var(pt.left,true) then
  2533. CGMessagePos(pt.left.fileinfo,type_e_variable_id_expected)
  2534. else
  2535. CGMessagePos3(pt.left.fileinfo,parser_e_call_by_ref_without_typeconv,tostr(hp^.wrongparanr),
  2536. FullTypeName(pt.left.resultdef,wrongpara.vardef),
  2537. FullTypeName(wrongpara.vardef,pt.left.resultdef))
  2538. end
  2539. else
  2540. CGMessagePos3(pt.left.fileinfo,type_e_wrong_parameter_type,tostr(hp^.wrongparanr),
  2541. FullTypeName(pt.left.resultdef,wrongpara.vardef),
  2542. FullTypeName(wrongpara.vardef,pt.left.resultdef));
  2543. end;
  2544. procedure check_hints(const srsym: tsym; const symoptions: tsymoptions; const deprecatedmsg : pshortstring);
  2545. begin
  2546. if not assigned(srsym) then
  2547. internalerror(200602051);
  2548. if sp_hint_deprecated in symoptions then
  2549. if (sp_has_deprecated_msg in symoptions) and (deprecatedmsg <> nil) then
  2550. Message2(sym_w_deprecated_symbol_with_msg,srsym.realname,deprecatedmsg^)
  2551. else
  2552. Message1(sym_w_deprecated_symbol,srsym.realname);
  2553. if sp_hint_experimental in symoptions then
  2554. Message1(sym_w_experimental_symbol,srsym.realname);
  2555. if sp_hint_platform in symoptions then
  2556. Message1(sym_w_non_portable_symbol,srsym.realname);
  2557. if sp_hint_library in symoptions then
  2558. Message1(sym_w_library_symbol,srsym.realname);
  2559. if sp_hint_unimplemented in symoptions then
  2560. Message1(sym_w_non_implemented_symbol,srsym.realname);
  2561. end;
  2562. procedure check_ranges(const location: tfileposinfo; source: tnode; destdef: tdef);
  2563. begin
  2564. if not(cs_check_ordinal_size in current_settings.localswitches) then
  2565. exit;
  2566. { check if the assignment may cause a range check error }
  2567. { if its not explicit, and only if the values are }
  2568. { ordinals, enumdef and floatdef }
  2569. if assigned(destdef) and
  2570. (destdef.typ in [enumdef,orddef,floatdef]) and
  2571. not is_boolean(destdef) and
  2572. assigned(source.resultdef) and
  2573. (source.resultdef.typ in [enumdef,orddef,floatdef]) and
  2574. not is_boolean(source.resultdef) and
  2575. not is_constrealnode(source) then
  2576. begin
  2577. if ((destdef.size < source.resultdef.size) and
  2578. { s80real and sc80real have a different size but the same precision }
  2579. not((destdef.typ=floatdef) and
  2580. (source.resultdef.typ=floatdef) and
  2581. (tfloatdef(source.resultdef).floattype in [s80real,sc80real]) and
  2582. (tfloatdef(destdef).floattype in [s80real,sc80real]))) or
  2583. ((destdef.typ<>floatdef) and
  2584. (source.resultdef.typ<>floatdef) and
  2585. not is_in_limit(source.resultdef,destdef)) then
  2586. begin
  2587. if (cs_check_range in current_settings.localswitches) then
  2588. MessagePos(location,type_w_smaller_possible_range_check)
  2589. else
  2590. MessagePos(location,type_h_smaller_possible_range_check);
  2591. end;
  2592. end;
  2593. end;
  2594. end.