htypechk.pas 99 KB

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