htypechk.pas 118 KB

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