htypechk.pas 119 KB

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