htypechk.pas 111 KB

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