htypechk.pas 119 KB

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