ncnv.pas 127 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447
  1. {
  2. Copyright (c) 2000-2002 by Florian Klaempfl
  3. Type checking and register allocation for type converting nodes
  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 ncnv;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. node,
  22. symtype,
  23. defutil,defcmp,
  24. nld
  25. ;
  26. type
  27. ttypeconvnode = class(tunarynode)
  28. totypedef : tdef;
  29. totypedefderef : tderef;
  30. convtype : tconverttype;
  31. warn_pointer_to_signed: boolean;
  32. constructor create(node : tnode;def:tdef);virtual;
  33. constructor create_explicit(node : tnode;def:tdef);
  34. constructor create_internal(node : tnode;def:tdef);
  35. constructor create_proc_to_procvar(node : tnode);
  36. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  37. procedure ppuwrite(ppufile:tcompilerppufile);override;
  38. procedure buildderefimpl;override;
  39. procedure derefimpl;override;
  40. function dogetcopy : tnode;override;
  41. function actualtargetnode: tnode;override;
  42. procedure printnodeinfo(var t : text);override;
  43. function pass_1 : tnode;override;
  44. function pass_typecheck:tnode;override;
  45. function simplify:tnode; override;
  46. procedure mark_write;override;
  47. function docompare(p: tnode) : boolean; override;
  48. function retains_value_location:boolean;
  49. function assign_allowed:boolean;
  50. procedure second_call_helper(c : tconverttype);
  51. private
  52. function typecheck_int_to_int : tnode;
  53. function typecheck_cord_to_pointer : tnode;
  54. function typecheck_chararray_to_string : tnode;
  55. function typecheck_string_to_chararray : tnode;
  56. function typecheck_char_to_string : tnode;
  57. function typecheck_char_to_chararray : tnode;
  58. function typecheck_int_to_real : tnode;
  59. function typecheck_real_to_real : tnode;
  60. function typecheck_real_to_currency : tnode;
  61. function typecheck_cchar_to_pchar : tnode;
  62. function typecheck_cstring_to_pchar : tnode;
  63. function typecheck_cstring_to_int : tnode;
  64. function typecheck_char_to_char : tnode;
  65. function typecheck_arrayconstructor_to_set : tnode;
  66. function typecheck_set_to_set : tnode;
  67. function typecheck_pchar_to_string : tnode;
  68. function typecheck_interface_to_string : tnode;
  69. function typecheck_interface_to_guid : tnode;
  70. function typecheck_dynarray_to_openarray : tnode;
  71. function typecheck_pwchar_to_string : tnode;
  72. function typecheck_variant_to_dynarray : tnode;
  73. function typecheck_dynarray_to_variant : tnode;
  74. function typecheck_call_helper(c : tconverttype) : tnode;
  75. function typecheck_variant_to_enum : tnode;
  76. function typecheck_enum_to_variant : tnode;
  77. function typecheck_proc_to_procvar : tnode;
  78. function typecheck_variant_to_interface : tnode;
  79. function typecheck_interface_to_variant : tnode;
  80. function typecheck_array_2_dynarray : tnode;
  81. protected
  82. function first_int_to_int : tnode;virtual;
  83. function first_cstring_to_pchar : tnode;virtual;
  84. function first_cstring_to_int : tnode;virtual;
  85. function first_string_to_chararray : tnode;virtual;
  86. function first_char_to_string : tnode;virtual;
  87. function first_nothing : tnode;virtual;
  88. function first_array_to_pointer : tnode;virtual;
  89. function first_int_to_real : tnode;virtual;
  90. function first_real_to_real : tnode;virtual;
  91. function first_pointer_to_array : tnode;virtual;
  92. function first_cchar_to_pchar : tnode;virtual;
  93. function first_bool_to_int : tnode;virtual;
  94. function first_int_to_bool : tnode;virtual;
  95. function first_bool_to_bool : tnode;virtual;
  96. function first_proc_to_procvar : tnode;virtual;
  97. function first_nil_to_methodprocvar : tnode;virtual;
  98. function first_set_to_set : tnode;virtual;
  99. function first_cord_to_pointer : tnode;virtual;
  100. function first_ansistring_to_pchar : tnode;virtual;
  101. function first_arrayconstructor_to_set : tnode;virtual;
  102. function first_class_to_intf : tnode;virtual;
  103. function first_char_to_char : tnode;virtual;
  104. function first_string_to_string : tnode;virtual;
  105. function first_call_helper(c : tconverttype) : tnode;
  106. { these wrapper are necessary, because the first_* stuff is called }
  107. { through a table. Without the wrappers override wouldn't have }
  108. { any effect }
  109. function _first_int_to_int : tnode;
  110. function _first_cstring_to_pchar : tnode;
  111. function _first_cstring_to_int : tnode;
  112. function _first_string_to_chararray : tnode;
  113. function _first_char_to_string : tnode;
  114. function _first_nothing : tnode;
  115. function _first_array_to_pointer : tnode;
  116. function _first_int_to_real : tnode;
  117. function _first_real_to_real: tnode;
  118. function _first_pointer_to_array : tnode;
  119. function _first_cchar_to_pchar : tnode;
  120. function _first_bool_to_int : tnode;
  121. function _first_int_to_bool : tnode;
  122. function _first_bool_to_bool : tnode;
  123. function _first_proc_to_procvar : tnode;
  124. function _first_nil_to_methodprocvar : tnode;
  125. function _first_cord_to_pointer : tnode;
  126. function _first_ansistring_to_pchar : tnode;
  127. function _first_arrayconstructor_to_set : tnode;
  128. function _first_class_to_intf : tnode;
  129. function _first_char_to_char : tnode;
  130. function _first_set_to_set : tnode;
  131. function _first_string_to_string : tnode;
  132. procedure _second_int_to_int;virtual;
  133. procedure _second_string_to_string;virtual;
  134. procedure _second_cstring_to_pchar;virtual;
  135. procedure _second_cstring_to_int;virtual;
  136. procedure _second_string_to_chararray;virtual;
  137. procedure _second_array_to_pointer;virtual;
  138. procedure _second_pointer_to_array;virtual;
  139. procedure _second_chararray_to_string;virtual;
  140. procedure _second_char_to_string;virtual;
  141. procedure _second_int_to_real;virtual;
  142. procedure _second_real_to_real;virtual;
  143. procedure _second_cord_to_pointer;virtual;
  144. procedure _second_proc_to_procvar;virtual;
  145. procedure _second_nil_to_methodprocvar;virtual;
  146. procedure _second_bool_to_int;virtual;
  147. procedure _second_int_to_bool;virtual;
  148. procedure _second_bool_to_bool;virtual;
  149. procedure _second_set_to_set;virtual;
  150. procedure _second_ansistring_to_pchar;virtual;
  151. procedure _second_class_to_intf;virtual;
  152. procedure _second_char_to_char;virtual;
  153. procedure _second_nothing; virtual;
  154. procedure second_int_to_int;virtual;abstract;
  155. procedure second_string_to_string;virtual;abstract;
  156. procedure second_cstring_to_pchar;virtual;abstract;
  157. procedure second_cstring_to_int;virtual;abstract;
  158. procedure second_string_to_chararray;virtual;abstract;
  159. procedure second_array_to_pointer;virtual;abstract;
  160. procedure second_pointer_to_array;virtual;abstract;
  161. procedure second_chararray_to_string;virtual;abstract;
  162. procedure second_char_to_string;virtual;abstract;
  163. procedure second_int_to_real;virtual;abstract;
  164. procedure second_real_to_real;virtual;abstract;
  165. procedure second_cord_to_pointer;virtual;abstract;
  166. procedure second_proc_to_procvar;virtual;abstract;
  167. procedure second_nil_to_methodprocvar;virtual;abstract;
  168. procedure second_bool_to_int;virtual;abstract;
  169. procedure second_int_to_bool;virtual;abstract;
  170. procedure second_bool_to_bool;virtual;abstract;
  171. procedure second_set_to_set;virtual;abstract;
  172. procedure second_ansistring_to_pchar;virtual;abstract;
  173. procedure second_class_to_intf;virtual;abstract;
  174. procedure second_char_to_char;virtual;abstract;
  175. procedure second_nothing; virtual;abstract;
  176. end;
  177. ttypeconvnodeclass = class of ttypeconvnode;
  178. tasnode = class(tbinarynode)
  179. call: tnode;
  180. constructor create(l,r : tnode);virtual;
  181. function pass_1 : tnode;override;
  182. function pass_typecheck:tnode;override;
  183. function dogetcopy: tnode;override;
  184. function docompare(p: tnode): boolean; override;
  185. destructor destroy; override;
  186. end;
  187. tasnodeclass = class of tasnode;
  188. tisnode = class(tbinarynode)
  189. constructor create(l,r : tnode);virtual;
  190. function pass_1 : tnode;override;
  191. function pass_typecheck:tnode;override;
  192. procedure pass_generate_code;override;
  193. end;
  194. tisnodeclass = class of tisnode;
  195. var
  196. ctypeconvnode : ttypeconvnodeclass;
  197. casnode : tasnodeclass;
  198. cisnode : tisnodeclass;
  199. procedure inserttypeconv(var p:tnode;def:tdef);
  200. procedure inserttypeconv_internal(var p:tnode;def:tdef);
  201. procedure arrayconstructor_to_set(var p : tnode);
  202. procedure insert_varargstypeconv(var p : tnode; iscvarargs: boolean);
  203. implementation
  204. uses
  205. globtype,systems,constexp,
  206. cutils,verbose,globals,widestr,
  207. symconst,symdef,symsym,symbase,symtable,
  208. ncon,ncal,nset,nadd,ninl,nmem,nmat,nbas,nutils,
  209. cgbase,procinfo,
  210. htypechk,pass_1,cpuinfo;
  211. {*****************************************************************************
  212. Helpers
  213. *****************************************************************************}
  214. procedure inserttypeconv(var p:tnode;def:tdef);
  215. begin
  216. if not assigned(p.resultdef) then
  217. begin
  218. typecheckpass(p);
  219. if codegenerror then
  220. exit;
  221. end;
  222. { don't insert obsolete type conversions }
  223. if equal_defs(p.resultdef,def) then
  224. p.resultdef:=def
  225. else
  226. begin
  227. p:=ctypeconvnode.create(p,def);
  228. typecheckpass(p);
  229. end;
  230. end;
  231. procedure inserttypeconv_internal(var p:tnode;def:tdef);
  232. begin
  233. if not assigned(p.resultdef) then
  234. begin
  235. typecheckpass(p);
  236. if codegenerror then
  237. exit;
  238. end;
  239. { don't insert obsolete type conversions }
  240. if equal_defs(p.resultdef,def) then
  241. p.resultdef:=def
  242. else
  243. begin
  244. p:=ctypeconvnode.create_internal(p,def);
  245. typecheckpass(p);
  246. end;
  247. end;
  248. {*****************************************************************************
  249. Array constructor to Set Conversion
  250. *****************************************************************************}
  251. procedure arrayconstructor_to_set(var p : tnode);
  252. var
  253. constp : tsetconstnode;
  254. buildp,
  255. p2,p3,p4 : tnode;
  256. hdef : tdef;
  257. constset : Pconstset;
  258. constsetlo,
  259. constsethi : TConstExprInt;
  260. procedure update_constsethi(def:tdef; maybetruncenumrange: boolean);
  261. begin
  262. if (def.typ=orddef) and
  263. ((torddef(def).high>=constsethi) or
  264. (torddef(def).low <=constsetlo)) then
  265. begin
  266. if torddef(def).ordtype=uwidechar then
  267. begin
  268. constsethi:=255;
  269. constsetlo:=0;
  270. if hdef=nil then
  271. hdef:=def;
  272. end
  273. else
  274. begin
  275. if (torddef(def).high>=constsethi) then
  276. constsethi:=torddef(def).high;
  277. if (torddef(def).low<=constsetlo) then
  278. constsetlo:=torddef(def).low;
  279. if hdef=nil then
  280. begin
  281. if (constsethi>255) or
  282. (torddef(def).low<0) then
  283. hdef:=u8inttype
  284. else
  285. hdef:=def;
  286. end;
  287. if constsethi>255 then
  288. constsethi:=255;
  289. if constsetlo<0 then
  290. constsetlo:=0;
  291. end;
  292. end
  293. else if (def.typ=enumdef) and
  294. ((tenumdef(def).max>=constsethi) or
  295. (tenumdef(def).min<=constsetlo)) then
  296. begin
  297. if hdef=nil then
  298. hdef:=def;
  299. if (tenumdef(def).max>=constsethi) then
  300. constsethi:=tenumdef(def).max;
  301. if (tenumdef(def).min<=constsetlo) then
  302. constsetlo:=tenumdef(def).min;
  303. { for constant set elements, delphi allows the usage of elements of enumerations which
  304. have value>255 if there is no element with a value > 255 used }
  305. if (maybetruncenumrange) and
  306. (m_delphi in current_settings.modeswitches) then
  307. begin
  308. if constsethi>255 then
  309. constsethi:=255;
  310. if constsetlo<0 then
  311. constsetlo:=0;
  312. end;
  313. end;
  314. end;
  315. procedure do_set(pos : longint);
  316. begin
  317. if (pos and not $ff)<>0 then
  318. Message(parser_e_illegal_set_expr);
  319. if pos>constsethi then
  320. constsethi:=pos;
  321. if pos<constsetlo then
  322. constsetlo:=pos;
  323. if pos in constset^ then
  324. Message(parser_e_illegal_set_expr);
  325. include(constset^,pos);
  326. end;
  327. var
  328. l : Longint;
  329. lr,hr : TConstExprInt;
  330. hp : tarrayconstructornode;
  331. begin
  332. if p.nodetype<>arrayconstructorn then
  333. internalerror(200205105);
  334. new(constset);
  335. constset^:=[];
  336. hdef:=nil;
  337. { make sure to set constsetlo correctly for empty sets }
  338. if assigned(tarrayconstructornode(p).left) then
  339. constsetlo:=high(aint)
  340. else
  341. constsetlo:=0;
  342. constsethi:=0;
  343. constp:=csetconstnode.create(nil,hdef);
  344. constp.value_set:=constset;
  345. buildp:=constp;
  346. hp:=tarrayconstructornode(p);
  347. if assigned(hp.left) then
  348. begin
  349. while assigned(hp) do
  350. begin
  351. p4:=nil; { will contain the tree to create the set }
  352. {split a range into p2 and p3 }
  353. if hp.left.nodetype=arrayconstructorrangen then
  354. begin
  355. p2:=tarrayconstructorrangenode(hp.left).left;
  356. p3:=tarrayconstructorrangenode(hp.left).right;
  357. tarrayconstructorrangenode(hp.left).left:=nil;
  358. tarrayconstructorrangenode(hp.left).right:=nil;
  359. end
  360. else
  361. begin
  362. p2:=hp.left;
  363. hp.left:=nil;
  364. p3:=nil;
  365. end;
  366. typecheckpass(p2);
  367. set_varstate(p2,vs_read,[vsf_must_be_valid]);
  368. if assigned(p3) then
  369. begin
  370. typecheckpass(p3);
  371. set_varstate(p3,vs_read,[vsf_must_be_valid]);
  372. end;
  373. if codegenerror then
  374. break;
  375. current_filepos:=p2.fileinfo;
  376. case p2.resultdef.typ of
  377. enumdef,
  378. orddef:
  379. begin
  380. { widechars are not yet supported }
  381. if is_widechar(p2.resultdef) then
  382. begin
  383. inserttypeconv(p2,cchartype);
  384. if (p2.nodetype<>ordconstn) then
  385. incompatibletypes(cwidechartype,cchartype);
  386. end;
  387. getrange(p2.resultdef,lr,hr);
  388. if assigned(p3) then
  389. begin
  390. if is_widechar(p3.resultdef) then
  391. begin
  392. inserttypeconv(p3,cchartype);
  393. if (p3.nodetype<>ordconstn) then
  394. begin
  395. current_filepos:=p3.fileinfo;
  396. incompatibletypes(cwidechartype,cchartype);
  397. end;
  398. end;
  399. { this isn't good, you'll get problems with
  400. type t010 = 0..10;
  401. ts = set of t010;
  402. var s : ts;b : t010
  403. begin s:=[1,2,b]; end.
  404. if is_integer(p3^.resultdef) then
  405. begin
  406. inserttypeconv(p3,u8bitdef);
  407. end;
  408. }
  409. if assigned(hdef) and not(equal_defs(hdef,p3.resultdef)) then
  410. begin
  411. CGMessagePos(p3.fileinfo,type_e_typeconflict_in_set);
  412. end
  413. else
  414. begin
  415. if (p2.nodetype=ordconstn) and (p3.nodetype=ordconstn) then
  416. begin
  417. if not(is_integer(p3.resultdef)) then
  418. hdef:=p3.resultdef
  419. else
  420. begin
  421. inserttypeconv(p3,u8inttype);
  422. inserttypeconv(p2,u8inttype);
  423. end;
  424. for l:=tordconstnode(p2).value.svalue to tordconstnode(p3).value.svalue do
  425. do_set(l);
  426. p2.free;
  427. p3.free;
  428. end
  429. else
  430. begin
  431. update_constsethi(p2.resultdef,false);
  432. inserttypeconv(p2,hdef);
  433. update_constsethi(p3.resultdef,false);
  434. inserttypeconv(p3,hdef);
  435. if assigned(hdef) then
  436. inserttypeconv(p3,hdef)
  437. else
  438. inserttypeconv(p3,u8inttype);
  439. p4:=csetelementnode.create(p2,p3);
  440. end;
  441. end;
  442. end
  443. else
  444. begin
  445. { Single value }
  446. if p2.nodetype=ordconstn then
  447. begin
  448. if not(is_integer(p2.resultdef)) then
  449. update_constsethi(p2.resultdef,true);
  450. if assigned(hdef) then
  451. inserttypeconv(p2,hdef)
  452. else
  453. inserttypeconv(p2,u8inttype);
  454. do_set(tordconstnode(p2).value.svalue);
  455. p2.free;
  456. end
  457. else
  458. begin
  459. update_constsethi(p2.resultdef,false);
  460. if assigned(hdef) then
  461. inserttypeconv(p2,hdef)
  462. else
  463. inserttypeconv(p2,u8inttype);
  464. p4:=csetelementnode.create(p2,nil);
  465. end;
  466. end;
  467. end;
  468. stringdef :
  469. begin
  470. { if we've already set elements which are constants }
  471. { throw an error }
  472. if ((hdef=nil) and assigned(buildp)) or
  473. not(is_char(hdef)) then
  474. CGMessage(type_e_typeconflict_in_set)
  475. else
  476. for l:=1 to length(pshortstring(tstringconstnode(p2).value_str)^) do
  477. do_set(ord(pshortstring(tstringconstnode(p2).value_str)^[l]));
  478. if hdef=nil then
  479. hdef:=cchartype;
  480. p2.free;
  481. end;
  482. else
  483. CGMessage(type_e_ordinal_expr_expected);
  484. end;
  485. { insert the set creation tree }
  486. if assigned(p4) then
  487. buildp:=caddnode.create(addn,buildp,p4);
  488. { load next and dispose current node }
  489. p2:=hp;
  490. hp:=tarrayconstructornode(tarrayconstructornode(p2).right);
  491. tarrayconstructornode(p2).right:=nil;
  492. p2.free;
  493. end;
  494. if (hdef=nil) then
  495. hdef:=u8inttype;
  496. end
  497. else
  498. begin
  499. { empty set [], only remove node }
  500. p.free;
  501. end;
  502. { set the initial set type }
  503. constp.resultdef:=tsetdef.create(hdef,constsetlo.svalue,constsethi.svalue);
  504. { determine the resultdef for the tree }
  505. typecheckpass(buildp);
  506. { set the new tree }
  507. p:=buildp;
  508. end;
  509. procedure insert_varargstypeconv(var p : tnode; iscvarargs: boolean);
  510. begin
  511. if not(iscvarargs) and
  512. (p.nodetype=stringconstn) then
  513. p:=ctypeconvnode.create_internal(p,cansistringtype)
  514. else
  515. case p.resultdef.typ of
  516. enumdef :
  517. p:=ctypeconvnode.create_internal(p,s32inttype);
  518. arraydef :
  519. begin
  520. if is_chararray(p.resultdef) then
  521. p:=ctypeconvnode.create_internal(p,charpointertype)
  522. else
  523. if is_widechararray(p.resultdef) then
  524. p:=ctypeconvnode.create_internal(p,widecharpointertype)
  525. else
  526. CGMessagePos1(p.fileinfo,type_e_wrong_type_in_array_constructor,p.resultdef.typename);
  527. end;
  528. orddef :
  529. begin
  530. if is_integer(p.resultdef) and
  531. not(is_64bitint(p.resultdef)) then
  532. p:=ctypeconvnode.create(p,s32inttype)
  533. else if is_void(p.resultdef) then
  534. CGMessagePos1(p.fileinfo,type_e_wrong_type_in_array_constructor,p.resultdef.typename)
  535. else if iscvarargs and is_currency(p.resultdef)
  536. and (current_settings.fputype<>fpu_none) then
  537. p:=ctypeconvnode.create(p,s64floattype);
  538. end;
  539. floatdef :
  540. if not(iscvarargs) then
  541. begin
  542. if not(is_currency(p.resultdef)) then
  543. p:=ctypeconvnode.create(p,pbestrealtype^);
  544. end
  545. else
  546. begin
  547. if is_constrealnode(p) and
  548. not(nf_explicit in p.flags) then
  549. MessagePos(p.fileinfo,type_w_double_c_varargs);
  550. if (tfloatdef(p.resultdef).floattype in [s32real,s64currency]) or
  551. (is_constrealnode(p) and
  552. not(nf_explicit in p.flags)) then
  553. p:=ctypeconvnode.create(p,s64floattype);
  554. end;
  555. procvardef :
  556. p:=ctypeconvnode.create(p,voidpointertype);
  557. stringdef:
  558. if iscvarargs then
  559. p:=ctypeconvnode.create(p,charpointertype);
  560. variantdef:
  561. if iscvarargs then
  562. CGMessagePos1(p.fileinfo,type_e_wrong_type_in_array_constructor,p.resultdef.typename);
  563. pointerdef:
  564. ;
  565. classrefdef:
  566. if iscvarargs then
  567. p:=ctypeconvnode.create(p,voidpointertype);
  568. objectdef :
  569. if iscvarargs or
  570. is_object(p.resultdef) then
  571. CGMessagePos1(p.fileinfo,type_e_wrong_type_in_array_constructor,p.resultdef.typename);
  572. else
  573. CGMessagePos1(p.fileinfo,type_e_wrong_type_in_array_constructor,p.resultdef.typename);
  574. end;
  575. typecheckpass(p);
  576. end;
  577. {*****************************************************************************
  578. TTYPECONVNODE
  579. *****************************************************************************}
  580. constructor ttypeconvnode.create(node : tnode;def:tdef);
  581. begin
  582. inherited create(typeconvn,node);
  583. convtype:=tc_none;
  584. totypedef:=def;
  585. if def=nil then
  586. internalerror(200103281);
  587. fileinfo:=node.fileinfo;
  588. {An attempt to convert the result of a floating point division
  589. (with the / operator) to an integer type will fail. Give a hint
  590. to use the div operator.}
  591. if (node.nodetype=slashn) and (def.typ=orddef) then
  592. cgmessage(type_h_use_div_for_int);
  593. {In expressions like int64:=longint+longint, an integer overflow could be avoided
  594. by simply converting the operands to int64 first. Give a hint to do this.}
  595. if (node.nodetype in [addn,subn,muln]) and
  596. (def.typ=orddef) and (node.resultdef<>nil) and (node.resultdef.typ=orddef) and
  597. ((Torddef(node.resultdef).low>=Torddef(def).low) and (Torddef(node.resultdef).high<=Torddef(def).high)) and
  598. ((Torddef(node.resultdef).low>Torddef(def).low) or (Torddef(node.resultdef).high<Torddef(def).high)) then
  599. case node.nodetype of
  600. addn:
  601. cgmessage1(type_h_convert_add_operands_to_prevent_overflow,def.gettypename);
  602. subn:
  603. cgmessage1(type_h_convert_sub_operands_to_prevent_overflow,def.gettypename);
  604. muln:
  605. cgmessage1(type_h_convert_mul_operands_to_prevent_overflow,def.gettypename);
  606. end;
  607. end;
  608. constructor ttypeconvnode.create_explicit(node : tnode;def:tdef);
  609. begin
  610. self.create(node,def);
  611. include(flags,nf_explicit);
  612. end;
  613. constructor ttypeconvnode.create_internal(node : tnode;def:tdef);
  614. begin
  615. self.create(node,def);
  616. { handle like explicit conversions }
  617. include(flags,nf_explicit);
  618. include(flags,nf_internal);
  619. end;
  620. constructor ttypeconvnode.create_proc_to_procvar(node : tnode);
  621. begin
  622. self.create(node,voidtype);
  623. convtype:=tc_proc_2_procvar;
  624. end;
  625. constructor ttypeconvnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  626. begin
  627. inherited ppuload(t,ppufile);
  628. ppufile.getderef(totypedefderef);
  629. convtype:=tconverttype(ppufile.getbyte);
  630. end;
  631. procedure ttypeconvnode.ppuwrite(ppufile:tcompilerppufile);
  632. begin
  633. inherited ppuwrite(ppufile);
  634. ppufile.putderef(totypedefderef);
  635. ppufile.putbyte(byte(convtype));
  636. end;
  637. procedure ttypeconvnode.buildderefimpl;
  638. begin
  639. inherited buildderefimpl;
  640. totypedefderef.build(totypedef);
  641. end;
  642. procedure ttypeconvnode.derefimpl;
  643. begin
  644. inherited derefimpl;
  645. totypedef:=tdef(totypedefderef.resolve);
  646. end;
  647. function ttypeconvnode.dogetcopy : tnode;
  648. var
  649. n : ttypeconvnode;
  650. begin
  651. n:=ttypeconvnode(inherited dogetcopy);
  652. n.convtype:=convtype;
  653. n.totypedef:=totypedef;
  654. dogetcopy:=n;
  655. end;
  656. procedure ttypeconvnode.printnodeinfo(var t : text);
  657. const
  658. convtyp2str : array[tconverttype] of pchar = (
  659. 'tc_none',
  660. 'tc_equal',
  661. 'tc_not_possible',
  662. 'tc_string_2_string',
  663. 'tc_char_2_string',
  664. 'tc_char_2_chararray',
  665. 'tc_pchar_2_string',
  666. 'tc_cchar_2_pchar',
  667. 'tc_cstring_2_pchar',
  668. 'tc_cstring_2_int',
  669. 'tc_ansistring_2_pchar',
  670. 'tc_string_2_chararray',
  671. 'tc_chararray_2_string',
  672. 'tc_array_2_pointer',
  673. 'tc_pointer_2_array',
  674. 'tc_int_2_int',
  675. 'tc_int_2_bool',
  676. 'tc_bool_2_bool',
  677. 'tc_bool_2_int',
  678. 'tc_real_2_real',
  679. 'tc_int_2_real',
  680. 'tc_real_2_currency',
  681. 'tc_proc_2_procvar',
  682. 'tc_nil_2_methodprocvar',
  683. 'tc_arrayconstructor_2_set',
  684. 'tc_set_2_set',
  685. 'tc_cord_2_pointer',
  686. 'tc_intf_2_string',
  687. 'tc_intf_2_guid',
  688. 'tc_class_2_intf',
  689. 'tc_char_2_char',
  690. 'tc_dynarray_2_openarray',
  691. 'tc_pwchar_2_string',
  692. 'tc_variant_2_dynarray',
  693. 'tc_dynarray_2_variant',
  694. 'tc_variant_2_enum',
  695. 'tc_enum_2_variant',
  696. 'tc_interface_2_variant',
  697. 'tc_variant_2_interface',
  698. 'tc_array_2_dynarray'
  699. );
  700. begin
  701. inherited printnodeinfo(t);
  702. write(t,', convtype = ',strpas(convtyp2str[convtype]));
  703. end;
  704. function ttypeconvnode.typecheck_cord_to_pointer : tnode;
  705. var
  706. t : tnode;
  707. begin
  708. result:=nil;
  709. if left.nodetype=ordconstn then
  710. begin
  711. { check if we have a valid pointer constant (JM) }
  712. {$if sizeof(pointer) > sizeof(TConstPtrUInt)}
  713. {$if sizeof(TConstPtrUInt) = 4}
  714. if (tordconstnode(left).value < int64(low(longint))) or
  715. (tordconstnode(left).value > int64(high(cardinal))) then
  716. CGMessage(parser_e_range_check_error);
  717. {$else} {$if sizeof(TConstPtrUInt) = 8}
  718. if (tordconstnode(left).value < int64(low(int64))) or
  719. (tordconstnode(left).value > int64(high(qword))) then
  720. CGMessage(parser_e_range_check_error);
  721. {$else}
  722. internalerror(2001020801);
  723. {$endif} {$endif}
  724. {$endif}
  725. t:=cpointerconstnode.create(TConstPtrUInt(tordconstnode(left).value.uvalue),resultdef);
  726. result:=t;
  727. end
  728. else
  729. internalerror(200104023);
  730. end;
  731. function ttypeconvnode.typecheck_chararray_to_string : tnode;
  732. var
  733. chartype : string[8];
  734. newblock : tblocknode;
  735. newstat : tstatementnode;
  736. restemp : ttempcreatenode;
  737. begin
  738. if is_widechar(tarraydef(left.resultdef).elementdef) then
  739. chartype:='widechar'
  740. else
  741. chartype:='char';
  742. if tstringdef(resultdef).stringtype=st_shortstring then
  743. begin
  744. newblock:=internalstatements(newstat);
  745. restemp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,false);
  746. addstatement(newstat,restemp);
  747. addstatement(newstat,ccallnode.createintern('fpc_'+chartype+'array_to_shortstr',
  748. ccallparanode.create(cordconstnode.create(
  749. ord(tarraydef(left.resultdef).lowrange=0),booltype,false),
  750. ccallparanode.create(left,ccallparanode.create(
  751. ctemprefnode.create(restemp),nil)))));
  752. addstatement(newstat,ctempdeletenode.create_normal_temp(restemp));
  753. addstatement(newstat,ctemprefnode.create(restemp));
  754. result:=newblock;
  755. end
  756. else
  757. result:=ccallnode.createinternres(
  758. 'fpc_'+chartype+'array_to_'+tstringdef(resultdef).stringtypname,
  759. ccallparanode.create(cordconstnode.create(
  760. ord(tarraydef(left.resultdef).lowrange=0),booltype,false),
  761. ccallparanode.create(left,nil)),resultdef);
  762. left:=nil;
  763. end;
  764. function ttypeconvnode.typecheck_string_to_chararray : tnode;
  765. var
  766. newblock : tblocknode;
  767. newstat : tstatementnode;
  768. restemp : ttempcreatenode;
  769. pchtemp : pchar;
  770. arrsize : aint;
  771. chartype : string[8];
  772. begin
  773. result := nil;
  774. with tarraydef(resultdef) do
  775. begin
  776. if highrange<lowrange then
  777. internalerror(200501051);
  778. arrsize := highrange-lowrange+1;
  779. end;
  780. if (left.nodetype = stringconstn) and
  781. (tstringconstnode(left).cst_type=cst_conststring) then
  782. begin
  783. { if the array of char is large enough we can use the string
  784. constant directly. This is handled in ncgcnv }
  785. if (arrsize>=tstringconstnode(left).len) and
  786. is_char(tarraydef(resultdef).elementdef) then
  787. begin
  788. { pad the constant string with #0 to the array len }
  789. { (2.0.x compatible) }
  790. if (arrsize>tstringconstnode(left).len) then
  791. begin
  792. pchtemp:=concatansistrings(tstringconstnode(left).value_str,pchar(StringOfChar(#0,arrsize-tstringconstnode(left).len)),tstringconstnode(left).len,arrsize-tstringconstnode(left).len);
  793. left.free;
  794. left:=cstringconstnode.createpchar(pchtemp,arrsize);
  795. typecheckpass(left);
  796. end;
  797. exit;
  798. end;
  799. { Convert to wide/short/ansistring and call default helper }
  800. if is_widechar(tarraydef(resultdef).elementdef) then
  801. inserttypeconv(left,cwidestringtype)
  802. else
  803. begin
  804. if tstringconstnode(left).len>255 then
  805. inserttypeconv(left,cansistringtype)
  806. else
  807. inserttypeconv(left,cshortstringtype);
  808. end;
  809. end;
  810. if is_widechar(tarraydef(resultdef).elementdef) then
  811. chartype:='widechar'
  812. else
  813. chartype:='char';
  814. newblock:=internalstatements(newstat);
  815. restemp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,false);
  816. addstatement(newstat,restemp);
  817. addstatement(newstat,ccallnode.createintern('fpc_'+tstringdef(left.resultdef).stringtypname+
  818. '_to_'+chartype+'array',ccallparanode.create(left,ccallparanode.create(
  819. ctemprefnode.create(restemp),nil))));
  820. addstatement(newstat,ctempdeletenode.create_normal_temp(restemp));
  821. addstatement(newstat,ctemprefnode.create(restemp));
  822. result:=newblock;
  823. left:=nil;
  824. end;
  825. function ttypeconvnode.typecheck_char_to_string : tnode;
  826. var
  827. procname: string[31];
  828. para : tcallparanode;
  829. hp : tstringconstnode;
  830. ws : pcompilerwidestring;
  831. newblock : tblocknode;
  832. newstat : tstatementnode;
  833. restemp : ttempcreatenode;
  834. begin
  835. result:=nil;
  836. { we can't do widechar to ansichar conversions at compile time, since }
  837. { this maps all non-ascii chars to '?' -> loses information }
  838. if (left.nodetype=ordconstn) and
  839. ((tstringdef(resultdef).stringtype in [st_widestring,st_unicodestring]) or
  840. (torddef(left.resultdef).ordtype=uchar) or
  841. { widechar >=128 is destroyed }
  842. (tordconstnode(left).value.uvalue<128)) then
  843. begin
  844. if (tstringdef(resultdef).stringtype in [st_widestring,st_unicodestring]) then
  845. begin
  846. initwidestring(ws);
  847. if torddef(left.resultdef).ordtype=uwidechar then
  848. concatwidestringchar(ws,tcompilerwidechar(tordconstnode(left).value.uvalue))
  849. else
  850. concatwidestringchar(ws,asciichar2unicode(chr(tordconstnode(left).value.uvalue)));
  851. hp:=cstringconstnode.createwstr(ws);
  852. hp.changestringtype(resultdef);
  853. donewidestring(ws);
  854. end
  855. else
  856. begin
  857. if (torddef(left.resultdef).ordtype=uwidechar) then
  858. hp:=cstringconstnode.createstr(unicode2asciichar(tcompilerwidechar(tordconstnode(left).value.uvalue)))
  859. else
  860. hp:=cstringconstnode.createstr(chr(tordconstnode(left).value.uvalue));
  861. tstringconstnode(hp).changestringtype(resultdef);
  862. end;
  863. result:=hp;
  864. end
  865. else
  866. { shortstrings are handled 'inline' (except for widechars) }
  867. if (tstringdef(resultdef).stringtype <> st_shortstring) or
  868. (torddef(left.resultdef).ordtype = uwidechar) then
  869. begin
  870. if (tstringdef(resultdef).stringtype <> st_shortstring) then
  871. begin
  872. { create the procname }
  873. if torddef(left.resultdef).ordtype<>uwidechar then
  874. procname := 'fpc_char_to_'
  875. else
  876. procname := 'fpc_uchar_to_';
  877. procname:=procname+tstringdef(resultdef).stringtypname;
  878. { and the parameter }
  879. para := ccallparanode.create(left,nil);
  880. { and finally the call }
  881. result := ccallnode.createinternres(procname,para,resultdef);
  882. end
  883. else
  884. begin
  885. newblock:=internalstatements(newstat);
  886. restemp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,false);
  887. addstatement(newstat,restemp);
  888. addstatement(newstat,ccallnode.createintern('fpc_wchar_to_shortstr',ccallparanode.create(left,ccallparanode.create(
  889. ctemprefnode.create(restemp),nil))));
  890. addstatement(newstat,ctempdeletenode.create_normal_temp(restemp));
  891. addstatement(newstat,ctemprefnode.create(restemp));
  892. result:=newblock;
  893. end;
  894. left := nil;
  895. end
  896. else
  897. begin
  898. { create word(byte(char) shl 8 or 1) for litte endian machines }
  899. { and word(byte(char) or 256) for big endian machines }
  900. left := ctypeconvnode.create_internal(left,u8inttype);
  901. if (target_info.endian = endian_little) then
  902. left := caddnode.create(orn,
  903. cshlshrnode.create(shln,left,cordconstnode.create(8,s32inttype,false)),
  904. cordconstnode.create(1,s32inttype,false))
  905. else
  906. left := caddnode.create(orn,left,
  907. cordconstnode.create(1 shl 8,s32inttype,false));
  908. left := ctypeconvnode.create_internal(left,u16inttype);
  909. typecheckpass(left);
  910. end;
  911. end;
  912. function ttypeconvnode.typecheck_char_to_chararray : tnode;
  913. begin
  914. if resultdef.size <> 1 then
  915. begin
  916. { convert first to string, then to chararray }
  917. inserttypeconv(left,cshortstringtype);
  918. inserttypeconv(left,resultdef);
  919. result:=left;
  920. left := nil;
  921. exit;
  922. end;
  923. result := nil;
  924. end;
  925. function ttypeconvnode.typecheck_char_to_char : tnode;
  926. var
  927. hp : tordconstnode;
  928. begin
  929. result:=nil;
  930. if (left.nodetype=ordconstn) and
  931. ((torddef(resultdef).ordtype<>uchar) or
  932. (torddef(left.resultdef).ordtype<>uwidechar) or
  933. { >= 128 is replaced by '?' currently -> loses information }
  934. (tordconstnode(left).value.uvalue<128)) then
  935. begin
  936. if (torddef(resultdef).ordtype=uchar) and
  937. (torddef(left.resultdef).ordtype=uwidechar) then
  938. begin
  939. hp:=cordconstnode.create(
  940. ord(unicode2asciichar(tcompilerwidechar(tordconstnode(left).value.uvalue))),
  941. cchartype,true);
  942. result:=hp;
  943. end
  944. else if (torddef(resultdef).ordtype=uwidechar) and
  945. (torddef(left.resultdef).ordtype=uchar) then
  946. begin
  947. hp:=cordconstnode.create(
  948. asciichar2unicode(chr(tordconstnode(left).value.uvalue)),
  949. cwidechartype,true);
  950. result:=hp;
  951. end
  952. else
  953. internalerror(200105131);
  954. exit;
  955. end;
  956. end;
  957. function ttypeconvnode.typecheck_int_to_int : tnode;
  958. var
  959. v : TConstExprInt;
  960. begin
  961. result:=nil;
  962. if left.nodetype=ordconstn then
  963. begin
  964. v:=tordconstnode(left).value;
  965. if is_currency(resultdef) then
  966. v:=v*10000;
  967. if (resultdef.typ=pointerdef) then
  968. result:=cpointerconstnode.create(TConstPtrUInt(v.uvalue),resultdef)
  969. else
  970. begin
  971. if is_currency(left.resultdef) then
  972. v:=v div 10000;
  973. result:=cordconstnode.create(v,resultdef,false);
  974. end;
  975. end
  976. else if left.nodetype=pointerconstn then
  977. begin
  978. v:=tpointerconstnode(left).value;
  979. if (resultdef.typ=pointerdef) then
  980. result:=cpointerconstnode.create(v.uvalue,resultdef)
  981. else
  982. begin
  983. if is_currency(resultdef) then
  984. v:=v*10000;
  985. result:=cordconstnode.create(v,resultdef,false);
  986. end;
  987. end
  988. else
  989. begin
  990. { multiply by 10000 for currency. We need to use getcopy to pass
  991. the argument because the current node is always disposed. Only
  992. inserting the multiply in the left node is not possible because
  993. it'll get in an infinite loop to convert int->currency }
  994. if is_currency(resultdef) then
  995. begin
  996. result:=caddnode.create(muln,getcopy,cordconstnode.create(10000,resultdef,false));
  997. include(result.flags,nf_is_currency);
  998. end
  999. else if is_currency(left.resultdef) then
  1000. begin
  1001. result:=cmoddivnode.create(divn,getcopy,cordconstnode.create(10000,resultdef,false));
  1002. include(result.flags,nf_is_currency);
  1003. end;
  1004. end;
  1005. end;
  1006. function ttypeconvnode.typecheck_int_to_real : tnode;
  1007. var
  1008. rv : bestreal;
  1009. begin
  1010. result:=nil;
  1011. if left.nodetype=ordconstn then
  1012. begin
  1013. rv:=tordconstnode(left).value;
  1014. if is_currency(resultdef) then
  1015. rv:=rv*10000.0
  1016. else if is_currency(left.resultdef) then
  1017. rv:=rv/10000.0;
  1018. result:=crealconstnode.create(rv,resultdef);
  1019. end
  1020. else
  1021. begin
  1022. { multiply by 10000 for currency. We need to use getcopy to pass
  1023. the argument because the current node is always disposed. Only
  1024. inserting the multiply in the left node is not possible because
  1025. it'll get in an infinite loop to convert int->currency }
  1026. if is_currency(resultdef) then
  1027. begin
  1028. result:=caddnode.create(muln,getcopy,crealconstnode.create(10000.0,resultdef));
  1029. include(result.flags,nf_is_currency);
  1030. end
  1031. else if is_currency(left.resultdef) then
  1032. begin
  1033. result:=caddnode.create(slashn,getcopy,crealconstnode.create(10000.0,resultdef));
  1034. include(result.flags,nf_is_currency);
  1035. end;
  1036. end;
  1037. end;
  1038. function ttypeconvnode.typecheck_real_to_currency : tnode;
  1039. begin
  1040. if not is_currency(resultdef) then
  1041. internalerror(200304221);
  1042. result:=nil;
  1043. left:=caddnode.create(muln,left,crealconstnode.create(10000.0,left.resultdef));
  1044. include(left.flags,nf_is_currency);
  1045. typecheckpass(left);
  1046. { Convert constants directly, else call Round() }
  1047. if left.nodetype=realconstn then
  1048. result:=cordconstnode.create(round(trealconstnode(left).value_real),resultdef,false)
  1049. else
  1050. begin
  1051. result:=ccallnode.createinternres('fpc_round_real',
  1052. ccallparanode.create(left,nil),resultdef);
  1053. left:=nil;
  1054. end;
  1055. end;
  1056. function ttypeconvnode.typecheck_real_to_real : tnode;
  1057. begin
  1058. result:=nil;
  1059. if is_currency(left.resultdef) and not(is_currency(resultdef)) then
  1060. begin
  1061. left:=caddnode.create(slashn,left,crealconstnode.create(10000.0,left.resultdef));
  1062. include(left.flags,nf_is_currency);
  1063. typecheckpass(left);
  1064. end
  1065. else
  1066. if is_currency(resultdef) and not(is_currency(left.resultdef)) then
  1067. begin
  1068. left:=caddnode.create(muln,left,crealconstnode.create(10000.0,left.resultdef));
  1069. include(left.flags,nf_is_currency);
  1070. typecheckpass(left);
  1071. end;
  1072. end;
  1073. function ttypeconvnode.typecheck_cchar_to_pchar : tnode;
  1074. begin
  1075. result:=nil;
  1076. if is_pwidechar(resultdef) then
  1077. inserttypeconv(left,cwidestringtype)
  1078. else
  1079. inserttypeconv(left,cshortstringtype);
  1080. { evaluate again, reset resultdef so the convert_typ
  1081. will be calculated again and cstring_to_pchar will
  1082. be used for futher conversion }
  1083. convtype:=tc_none;
  1084. result:=pass_typecheck;
  1085. end;
  1086. function ttypeconvnode.typecheck_cstring_to_pchar : tnode;
  1087. begin
  1088. result:=nil;
  1089. if is_pwidechar(resultdef) then
  1090. inserttypeconv(left,cwidestringtype)
  1091. else
  1092. if is_pchar(resultdef) and
  1093. (is_widestring(left.resultdef) or
  1094. is_unicodestring(left.resultdef)) then
  1095. begin
  1096. inserttypeconv(left,cansistringtype);
  1097. { the second pass of second_cstring_to_pchar expects a }
  1098. { strinconstn, but this may become a call to the }
  1099. { widestring manager in case left contains "high ascii" }
  1100. if (left.nodetype<>stringconstn) then
  1101. begin
  1102. result:=left;
  1103. left:=nil;
  1104. end;
  1105. end;
  1106. end;
  1107. function ttypeconvnode.typecheck_cstring_to_int : tnode;
  1108. var
  1109. fcc : cardinal;
  1110. pb : pbyte;
  1111. begin
  1112. result:=nil;
  1113. if left.nodetype<>stringconstn then
  1114. internalerror(200510012);
  1115. if tstringconstnode(left).len=4 then
  1116. begin
  1117. pb:=pbyte(tstringconstnode(left).value_str);
  1118. fcc:=(pb[0] shl 24) or (pb[1] shl 16) or (pb[2] shl 8) or pb[3];
  1119. result:=cordconstnode.create(fcc,u32inttype,false);
  1120. end
  1121. else
  1122. CGMessage2(type_e_illegal_type_conversion,left.resultdef.GetTypeName,resultdef.GetTypeName);
  1123. end;
  1124. function ttypeconvnode.typecheck_arrayconstructor_to_set : tnode;
  1125. var
  1126. hp : tnode;
  1127. begin
  1128. result:=nil;
  1129. if left.nodetype<>arrayconstructorn then
  1130. internalerror(5546);
  1131. { remove typeconv node }
  1132. hp:=left;
  1133. left:=nil;
  1134. { create a set constructor tree }
  1135. arrayconstructor_to_set(hp);
  1136. result:=hp;
  1137. end;
  1138. function ttypeconvnode.typecheck_set_to_set : tnode;
  1139. begin
  1140. result:=nil;
  1141. { constant sets can be converted by changing the type only }
  1142. if (left.nodetype=setconstn) then
  1143. begin
  1144. left.resultdef:=resultdef;
  1145. result:=left;
  1146. left:=nil;
  1147. exit;
  1148. end;
  1149. end;
  1150. function ttypeconvnode.typecheck_pchar_to_string : tnode;
  1151. var
  1152. newblock : tblocknode;
  1153. newstat : tstatementnode;
  1154. restemp : ttempcreatenode;
  1155. begin
  1156. if tstringdef(resultdef).stringtype=st_shortstring then
  1157. begin
  1158. newblock:=internalstatements(newstat);
  1159. restemp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,false);
  1160. addstatement(newstat,restemp);
  1161. addstatement(newstat,ccallnode.createintern('fpc_pchar_to_shortstr',ccallparanode.create(left,ccallparanode.create(
  1162. ctemprefnode.create(restemp),nil))));
  1163. addstatement(newstat,ctempdeletenode.create_normal_temp(restemp));
  1164. addstatement(newstat,ctemprefnode.create(restemp));
  1165. result:=newblock;
  1166. end
  1167. else
  1168. result := ccallnode.createinternres(
  1169. 'fpc_pchar_to_'+tstringdef(resultdef).stringtypname,
  1170. ccallparanode.create(left,nil),resultdef);
  1171. left:=nil;
  1172. end;
  1173. function ttypeconvnode.typecheck_interface_to_string : tnode;
  1174. begin
  1175. if assigned(tobjectdef(left.resultdef).iidstr) then
  1176. begin
  1177. if not(oo_has_valid_guid in tobjectdef(left.resultdef).objectoptions) then
  1178. CGMessage1(type_interface_has_no_guid,tobjectdef(left.resultdef).typename);
  1179. result:=cstringconstnode.createstr(tobjectdef(left.resultdef).iidstr^);
  1180. tstringconstnode(result).changestringtype(cshortstringtype);
  1181. end;
  1182. end;
  1183. function ttypeconvnode.typecheck_interface_to_guid : tnode;
  1184. begin
  1185. if assigned(tobjectdef(left.resultdef).iidguid) then
  1186. begin
  1187. if not(oo_has_valid_guid in tobjectdef(left.resultdef).objectoptions) then
  1188. CGMessage1(type_interface_has_no_guid,tobjectdef(left.resultdef).typename);
  1189. result:=cguidconstnode.create(tobjectdef(left.resultdef).iidguid^);
  1190. end;
  1191. end;
  1192. function ttypeconvnode.typecheck_dynarray_to_openarray : tnode;
  1193. begin
  1194. { a dynamic array is a pointer to an array, so to convert it to }
  1195. { an open array, we have to dereference it (JM) }
  1196. result := ctypeconvnode.create_internal(left,voidpointertype);
  1197. typecheckpass(result);
  1198. { left is reused }
  1199. left := nil;
  1200. result := cderefnode.create(result);
  1201. include(result.flags,nf_no_checkpointer);
  1202. result.resultdef := resultdef;
  1203. end;
  1204. function ttypeconvnode.typecheck_pwchar_to_string : tnode;
  1205. var
  1206. newblock : tblocknode;
  1207. newstat : tstatementnode;
  1208. restemp : ttempcreatenode;
  1209. begin
  1210. if tstringdef(resultdef).stringtype=st_shortstring then
  1211. begin
  1212. newblock:=internalstatements(newstat);
  1213. restemp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,false);
  1214. addstatement(newstat,restemp);
  1215. addstatement(newstat,ccallnode.createintern('fpc_pwidechar_to_shortstr',ccallparanode.create(left,ccallparanode.create(
  1216. ctemprefnode.create(restemp),nil))));
  1217. addstatement(newstat,ctempdeletenode.create_normal_temp(restemp));
  1218. addstatement(newstat,ctemprefnode.create(restemp));
  1219. result:=newblock;
  1220. end
  1221. else
  1222. result := ccallnode.createinternres(
  1223. 'fpc_pwidechar_to_'+tstringdef(resultdef).stringtypname,
  1224. ccallparanode.create(left,nil),resultdef);
  1225. left:=nil;
  1226. end;
  1227. function ttypeconvnode.typecheck_variant_to_dynarray : tnode;
  1228. begin
  1229. result := ccallnode.createinternres(
  1230. 'fpc_variant_to_dynarray',
  1231. ccallparanode.create(caddrnode.create_internal(crttinode.create(tstoreddef(resultdef),initrtti,rdt_normal)),
  1232. ccallparanode.create(left,nil)
  1233. ),resultdef);
  1234. typecheckpass(result);
  1235. left:=nil;
  1236. end;
  1237. function ttypeconvnode.typecheck_dynarray_to_variant : tnode;
  1238. begin
  1239. result := ccallnode.createinternres(
  1240. 'fpc_dynarray_to_variant',
  1241. ccallparanode.create(caddrnode.create_internal(crttinode.create(tstoreddef(left.resultdef),initrtti,rdt_normal)),
  1242. ccallparanode.create(ctypeconvnode.create_explicit(left,voidpointertype),nil)
  1243. ),resultdef);
  1244. typecheckpass(result);
  1245. left:=nil;
  1246. end;
  1247. function ttypeconvnode.typecheck_variant_to_interface : tnode;
  1248. begin
  1249. if tobjectdef(resultdef).is_related(tobjectdef(search_system_type('IDISPATCH').typedef)) then
  1250. result := ccallnode.createinternres(
  1251. 'fpc_variant_to_idispatch',
  1252. ccallparanode.create(left,nil)
  1253. ,resultdef)
  1254. else
  1255. result := ccallnode.createinternres(
  1256. 'fpc_variant_to_interface',
  1257. ccallparanode.create(left,nil)
  1258. ,resultdef);
  1259. typecheckpass(result);
  1260. left:=nil;
  1261. end;
  1262. function ttypeconvnode.typecheck_interface_to_variant : tnode;
  1263. begin
  1264. if tobjectdef(left.resultdef).is_related(tobjectdef(search_system_type('IDISPATCH').typedef)) then
  1265. result := ccallnode.createinternres(
  1266. 'fpc_idispatch_to_variant',
  1267. ccallparanode.create(left,nil)
  1268. ,resultdef)
  1269. else
  1270. result := ccallnode.createinternres(
  1271. 'fpc_interface_to_variant',
  1272. ccallparanode.create(left,nil)
  1273. ,resultdef);
  1274. typecheckpass(result);
  1275. left:=nil;
  1276. end;
  1277. function ttypeconvnode.typecheck_variant_to_enum : tnode;
  1278. begin
  1279. result := ctypeconvnode.create_internal(left,sinttype);
  1280. result := ctypeconvnode.create_internal(result,resultdef);
  1281. typecheckpass(result);
  1282. { left is reused }
  1283. left := nil;
  1284. end;
  1285. function ttypeconvnode.typecheck_enum_to_variant : tnode;
  1286. begin
  1287. result := ctypeconvnode.create_internal(left,sinttype);
  1288. result := ctypeconvnode.create_internal(result,cvarianttype);
  1289. typecheckpass(result);
  1290. { left is reused }
  1291. left := nil;
  1292. end;
  1293. function ttypeconvnode.typecheck_array_2_dynarray : tnode;
  1294. var
  1295. newstatement : tstatementnode;
  1296. temp : ttempcreatenode;
  1297. temp2 : ttempcreatenode;
  1298. begin
  1299. { create statements with call to getmem+initialize }
  1300. result:=internalstatements(newstatement);
  1301. { create temp for result }
  1302. temp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,true);
  1303. addstatement(newstatement,temp);
  1304. { get temp for array of lengths }
  1305. temp2:=ctempcreatenode.create(sinttype,sinttype.size,tt_persistent,false);
  1306. addstatement(newstatement,temp2);
  1307. { one dimensional }
  1308. addstatement(newstatement,cassignmentnode.create(
  1309. ctemprefnode.create_offset(temp2,0),
  1310. cordconstnode.create
  1311. (tarraydef(left.resultdef).highrange+1,s32inttype,true)));
  1312. { create call to fpc_dynarr_setlength }
  1313. addstatement(newstatement,ccallnode.createintern('fpc_dynarray_setlength',
  1314. ccallparanode.create(caddrnode.create_internal
  1315. (ctemprefnode.create(temp2)),
  1316. ccallparanode.create(cordconstnode.create
  1317. (1,s32inttype,true),
  1318. ccallparanode.create(caddrnode.create_internal
  1319. (crttinode.create(tstoreddef(resultdef),initrtti,rdt_normal)),
  1320. ccallparanode.create(
  1321. ctypeconvnode.create_internal(
  1322. ctemprefnode.create(temp),voidpointertype),
  1323. nil))))
  1324. ));
  1325. addstatement(newstatement,ctempdeletenode.create(temp2));
  1326. { copy ... }
  1327. addstatement(newstatement,cassignmentnode.create(
  1328. ctypeconvnode.create_internal(cderefnode.create(ctypeconvnode.create_internal(ctemprefnode.create(temp),voidpointertype)),left.resultdef),
  1329. left
  1330. ));
  1331. { left is reused }
  1332. left:=nil;
  1333. { the last statement should return the value as
  1334. location and type, this is done be referencing the
  1335. temp and converting it first from a persistent temp to
  1336. normal temp }
  1337. addstatement(newstatement,ctempdeletenode.create_normal_temp(temp));
  1338. addstatement(newstatement,ctemprefnode.create(temp));
  1339. end;
  1340. procedure copyparasym(p:TObject;arg:pointer);
  1341. var
  1342. newparast : TSymtable absolute arg;
  1343. vs : tparavarsym;
  1344. begin
  1345. if tsym(p).typ<>paravarsym then
  1346. exit;
  1347. with tparavarsym(p) do
  1348. begin
  1349. vs:=tparavarsym.create(realname,paranr,varspez,vardef,varoptions);
  1350. vs.defaultconstsym:=defaultconstsym;
  1351. newparast.insert(vs);
  1352. end;
  1353. end;
  1354. function ttypeconvnode.typecheck_proc_to_procvar : tnode;
  1355. var
  1356. pd : tabstractprocdef;
  1357. begin
  1358. result:=nil;
  1359. pd:=tabstractprocdef(left.resultdef);
  1360. { create procvardef }
  1361. resultdef:=tprocvardef.create(pd.parast.symtablelevel);
  1362. tprocvardef(resultdef).proctypeoption:=pd.proctypeoption;
  1363. tprocvardef(resultdef).proccalloption:=pd.proccalloption;
  1364. tprocvardef(resultdef).procoptions:=pd.procoptions;
  1365. tprocvardef(resultdef).returndef:=pd.returndef;
  1366. { method ? then set the methodpointer flag }
  1367. if (pd.owner.symtabletype=ObjectSymtable) then
  1368. include(tprocvardef(resultdef).procoptions,po_methodpointer);
  1369. { was it a local procedure? }
  1370. if (pd.owner.symtabletype=localsymtable) then
  1371. include(tprocvardef(resultdef).procoptions,po_local);
  1372. { only need the address of the method? this is needed
  1373. for @tobject.create. In this case there will be a loadn without
  1374. a methodpointer. }
  1375. if (left.nodetype=loadn) and
  1376. not assigned(tloadnode(left).left) then
  1377. include(tprocvardef(resultdef).procoptions,po_addressonly);
  1378. { Add parameters use only references, we don't need to keep the
  1379. parast. We use the parast from the original function to calculate
  1380. our parameter data and reset it afterwards }
  1381. pd.parast.SymList.ForEachCall(@copyparasym,tprocvardef(resultdef).parast);
  1382. tprocvardef(resultdef).calcparas;
  1383. end;
  1384. function ttypeconvnode.typecheck_call_helper(c : tconverttype) : tnode;
  1385. const
  1386. resultdefconvert : array[tconverttype] of pointer = (
  1387. {none} nil,
  1388. {equal} nil,
  1389. {not_possible} nil,
  1390. { string_2_string } nil,
  1391. { char_2_string } @ttypeconvnode.typecheck_char_to_string,
  1392. { char_2_chararray } @ttypeconvnode.typecheck_char_to_chararray,
  1393. { pchar_2_string } @ttypeconvnode.typecheck_pchar_to_string,
  1394. { cchar_2_pchar } @ttypeconvnode.typecheck_cchar_to_pchar,
  1395. { cstring_2_pchar } @ttypeconvnode.typecheck_cstring_to_pchar,
  1396. { cstring_2_int } @ttypeconvnode.typecheck_cstring_to_int,
  1397. { ansistring_2_pchar } nil,
  1398. { string_2_chararray } @ttypeconvnode.typecheck_string_to_chararray,
  1399. { chararray_2_string } @ttypeconvnode.typecheck_chararray_to_string,
  1400. { array_2_pointer } nil,
  1401. { pointer_2_array } nil,
  1402. { int_2_int } @ttypeconvnode.typecheck_int_to_int,
  1403. { int_2_bool } nil,
  1404. { bool_2_bool } nil,
  1405. { bool_2_int } nil,
  1406. { real_2_real } @ttypeconvnode.typecheck_real_to_real,
  1407. { int_2_real } @ttypeconvnode.typecheck_int_to_real,
  1408. { real_2_currency } @ttypeconvnode.typecheck_real_to_currency,
  1409. { proc_2_procvar } @ttypeconvnode.typecheck_proc_to_procvar,
  1410. { nil_2_methodprocvar } nil,
  1411. { arrayconstructor_2_set } @ttypeconvnode.typecheck_arrayconstructor_to_set,
  1412. { set_to_set } @ttypeconvnode.typecheck_set_to_set,
  1413. { cord_2_pointer } @ttypeconvnode.typecheck_cord_to_pointer,
  1414. { intf_2_string } @ttypeconvnode.typecheck_interface_to_string,
  1415. { intf_2_guid } @ttypeconvnode.typecheck_interface_to_guid,
  1416. { class_2_intf } nil,
  1417. { char_2_char } @ttypeconvnode.typecheck_char_to_char,
  1418. { dynarray_2_openarray} @ttypeconvnode.typecheck_dynarray_to_openarray,
  1419. { pwchar_2_string} @ttypeconvnode.typecheck_pwchar_to_string,
  1420. { variant_2_dynarray} @ttypeconvnode.typecheck_variant_to_dynarray,
  1421. { dynarray_2_variant} @ttypeconvnode.typecheck_dynarray_to_variant,
  1422. { variant_2_enum} @ttypeconvnode.typecheck_variant_to_enum,
  1423. { enum_2_variant} @ttypeconvnode.typecheck_enum_to_variant,
  1424. { variant_2_interface} @ttypeconvnode.typecheck_interface_to_variant,
  1425. { interface_2_variant} @ttypeconvnode.typecheck_variant_to_interface,
  1426. { array_2_dynarray} @ttypeconvnode.typecheck_array_2_dynarray
  1427. );
  1428. type
  1429. tprocedureofobject = function : tnode of object;
  1430. var
  1431. r : packed record
  1432. proc : pointer;
  1433. obj : pointer;
  1434. end;
  1435. begin
  1436. result:=nil;
  1437. { this is a little bit dirty but it works }
  1438. { and should be quite portable too }
  1439. r.proc:=resultdefconvert[c];
  1440. r.obj:=self;
  1441. if assigned(r.proc) then
  1442. result:=tprocedureofobject(r)();
  1443. end;
  1444. function ttypeconvnode.actualtargetnode: tnode;
  1445. begin
  1446. result:=self;
  1447. while (result.nodetype=typeconvn) and
  1448. ttypeconvnode(result).retains_value_location do
  1449. result:=ttypeconvnode(result).left;
  1450. end;
  1451. function ttypeconvnode.pass_typecheck:tnode;
  1452. var
  1453. hdef : tdef;
  1454. hp : tnode;
  1455. currprocdef : tabstractprocdef;
  1456. aprocdef : tprocdef;
  1457. eq : tequaltype;
  1458. cdoptions : tcompare_defs_options;
  1459. newblock: tblocknode;
  1460. newstatement: tstatementnode;
  1461. tempnode: ttempcreatenode;
  1462. begin
  1463. result:=nil;
  1464. resultdef:=totypedef;
  1465. typecheckpass(left);
  1466. if codegenerror then
  1467. exit;
  1468. { When absolute force tc_equal }
  1469. if (nf_absolute in flags) then
  1470. begin
  1471. convtype:=tc_equal;
  1472. if not(tstoreddef(resultdef).is_intregable) and
  1473. not(tstoreddef(resultdef).is_fpuregable) then
  1474. make_not_regable(left,[ra_addr_regable]);
  1475. exit;
  1476. end;
  1477. { tp procvar support. Skip typecasts to procvar, record or set. Those
  1478. convert on the procvar value. This is used to access the
  1479. fields of a methodpointer }
  1480. if not(nf_load_procvar in flags) and
  1481. not(resultdef.typ in [procvardef,recorddef,setdef]) then
  1482. maybe_call_procvar(left,true);
  1483. { convert array constructors to sets, because there is no conversion
  1484. possible for array constructors }
  1485. if (resultdef.typ<>arraydef) and
  1486. is_array_constructor(left.resultdef) then
  1487. begin
  1488. arrayconstructor_to_set(left);
  1489. typecheckpass(left);
  1490. end;
  1491. if convtype=tc_none then
  1492. begin
  1493. cdoptions:=[cdo_check_operator,cdo_allow_variant];
  1494. if nf_explicit in flags then
  1495. include(cdoptions,cdo_explicit);
  1496. if nf_internal in flags then
  1497. include(cdoptions,cdo_internal);
  1498. eq:=compare_defs_ext(left.resultdef,resultdef,left.nodetype,convtype,aprocdef,cdoptions);
  1499. case eq of
  1500. te_exact,
  1501. te_equal :
  1502. begin
  1503. result := simplify;
  1504. if assigned(result) then
  1505. exit;
  1506. { Only leave when there is no conversion to do.
  1507. We can still need to call a conversion routine,
  1508. like the routine to convert a stringconstnode }
  1509. if (convtype in [tc_equal,tc_not_possible]) and
  1510. { some conversions, like dynarray to pointer in Delphi
  1511. mode, must not be removed, because then we get memory
  1512. leaks due to missing temp finalization }
  1513. (not is_refcounted_type(left.resultdef) or
  1514. { different kinds of refcounted types may need calls
  1515. to different kinds of refcounting helpers }
  1516. (resultdef=left.resultdef)) then
  1517. begin
  1518. left.resultdef:=resultdef;
  1519. if (nf_explicit in flags) and (left.nodetype = addrn) then
  1520. include(left.flags, nf_typedaddr);
  1521. result:=left;
  1522. left:=nil;
  1523. exit;
  1524. end;
  1525. end;
  1526. te_convert_l1,
  1527. te_convert_l2,
  1528. te_convert_l3,
  1529. te_convert_l4,
  1530. te_convert_l5:
  1531. begin
  1532. result := simplify;
  1533. if assigned(result) then
  1534. exit;
  1535. { nothing to do }
  1536. end;
  1537. te_convert_operator :
  1538. begin
  1539. include(current_procinfo.flags,pi_do_call);
  1540. aprocdef.procsym.IncRefCountBy(1);
  1541. hp:=ccallnode.create(ccallparanode.create(left,nil),Tprocsym(aprocdef.procsym),nil,nil,[]);
  1542. { tell explicitly which def we must use !! (PM) }
  1543. tcallnode(hp).procdefinition:=aprocdef;
  1544. left:=nil;
  1545. result:=hp;
  1546. exit;
  1547. end;
  1548. te_incompatible :
  1549. begin
  1550. { Procedures have a resultdef of voiddef and functions of their
  1551. own resultdef. They will therefore always be incompatible with
  1552. a procvar. Because isconvertable cannot check for procedures we
  1553. use an extra check for them.}
  1554. if (left.nodetype=calln) and
  1555. (tcallnode(left).para_count=0) and
  1556. (resultdef.typ=procvardef) and
  1557. (
  1558. (m_tp_procvar in current_settings.modeswitches) or
  1559. (m_mac_procvar in current_settings.modeswitches)
  1560. ) then
  1561. begin
  1562. if assigned(tcallnode(left).right) then
  1563. begin
  1564. { this is already a procvar, if it is really equal
  1565. is checked below }
  1566. convtype:=tc_equal;
  1567. hp:=tcallnode(left).right.getcopy;
  1568. currprocdef:=tabstractprocdef(hp.resultdef);
  1569. end
  1570. else
  1571. begin
  1572. convtype:=tc_proc_2_procvar;
  1573. currprocdef:=Tprocsym(Tcallnode(left).symtableprocentry).Find_procdef_byprocvardef(Tprocvardef(resultdef));
  1574. hp:=cloadnode.create_procvar(tprocsym(tcallnode(left).symtableprocentry),
  1575. tprocdef(currprocdef),tcallnode(left).symtableproc);
  1576. if (tcallnode(left).symtableprocentry.owner.symtabletype=ObjectSymtable) then
  1577. begin
  1578. if assigned(tcallnode(left).methodpointer) then
  1579. tloadnode(hp).set_mp(tcallnode(left).methodpointer.getcopy)
  1580. else
  1581. tloadnode(hp).set_mp(load_self_node);
  1582. end;
  1583. typecheckpass(hp);
  1584. end;
  1585. left.free;
  1586. left:=hp;
  1587. { Now check if the procedure we are going to assign to
  1588. the procvar, is compatible with the procvar's type }
  1589. if not(nf_explicit in flags) and
  1590. (proc_to_procvar_equal(currprocdef,tprocvardef(resultdef))=te_incompatible) then
  1591. IncompatibleTypes(left.resultdef,resultdef);
  1592. exit;
  1593. end;
  1594. { Handle explicit type conversions }
  1595. if nf_explicit in flags then
  1596. begin
  1597. { do common tc_equal cast }
  1598. convtype:=tc_equal;
  1599. { ordinal constants can be resized to 1,2,4,8 bytes }
  1600. if (left.nodetype=ordconstn) then
  1601. begin
  1602. { Insert typeconv for ordinal to the correct size first on left, after
  1603. that the other conversion can be done }
  1604. hdef:=nil;
  1605. case longint(resultdef.size) of
  1606. 1 :
  1607. hdef:=s8inttype;
  1608. 2 :
  1609. hdef:=s16inttype;
  1610. 4 :
  1611. hdef:=s32inttype;
  1612. 8 :
  1613. hdef:=s64inttype;
  1614. end;
  1615. { we need explicit, because it can also be an enum }
  1616. if assigned(hdef) then
  1617. inserttypeconv_internal(left,hdef)
  1618. else
  1619. CGMessage2(type_e_illegal_type_conversion,left.resultdef.GetTypeName,resultdef.GetTypeName);
  1620. end;
  1621. { check if the result could be in a register }
  1622. if (not(tstoreddef(resultdef).is_intregable) and
  1623. not(tstoreddef(resultdef).is_fpuregable)) or
  1624. ((left.resultdef.typ = floatdef) and
  1625. (resultdef.typ <> floatdef)) then
  1626. make_not_regable(left,[ra_addr_regable]);
  1627. { class/interface to class/interface, with checkobject support }
  1628. if is_class_or_interface_or_objc(resultdef) and
  1629. is_class_or_interface_or_objc(left.resultdef) then
  1630. begin
  1631. { check if the types are related }
  1632. if not(nf_internal in flags) and
  1633. (not(tobjectdef(left.resultdef).is_related(tobjectdef(resultdef)))) and
  1634. (not(tobjectdef(resultdef).is_related(tobjectdef(left.resultdef)))) then
  1635. begin
  1636. { Give an error when typecasting class to interface, this is compatible
  1637. with delphi }
  1638. if is_interface(resultdef) and
  1639. not is_interface(left.resultdef) then
  1640. CGMessage2(type_e_classes_not_related,
  1641. FullTypeName(left.resultdef,resultdef),
  1642. FullTypeName(resultdef,left.resultdef))
  1643. else
  1644. CGMessage2(type_w_classes_not_related,
  1645. FullTypeName(left.resultdef,resultdef),
  1646. FullTypeName(resultdef,left.resultdef))
  1647. end;
  1648. { Add runtime check? }
  1649. if not is_objc_class_or_protocol(resultdef) and
  1650. not is_objc_class_or_protocol(left.resultdef) and
  1651. (cs_check_object in current_settings.localswitches) and
  1652. not(nf_internal in flags) then
  1653. begin
  1654. { we can translate the typeconvnode to 'as' when
  1655. typecasting to a class or interface }
  1656. { we need to make sure the result can still be
  1657. passed as a var parameter }
  1658. newblock:=internalstatements(newstatement);
  1659. if (valid_for_var(left,false)) then
  1660. begin
  1661. tempnode:=ctempcreatenode.create(voidpointertype,voidpointertype.size,tt_persistent,true);
  1662. addstatement(newstatement,tempnode);
  1663. addstatement(newstatement,cassignmentnode.create(
  1664. ctemprefnode.create(tempnode),
  1665. caddrnode.create_internal(left)));
  1666. left:=ctypeconvnode.create_internal(cderefnode.create(ctemprefnode.create(tempnode)),left.resultdef);
  1667. end
  1668. else
  1669. begin
  1670. tempnode:=ctempcreatenode.create(left.resultdef,left.resultdef.size,tt_persistent,true);
  1671. addstatement(newstatement,tempnode);
  1672. addstatement(newstatement,cassignmentnode.create(
  1673. ctemprefnode.create(tempnode),
  1674. left));
  1675. left:=ctemprefnode.create(tempnode);
  1676. end;
  1677. addstatement(newstatement,casnode.create(left.getcopy,cloadvmtaddrnode.create(ctypenode.create(resultdef))));
  1678. addstatement(newstatement,ctempdeletenode.create_normal_temp(tempnode));
  1679. addstatement(newstatement,ctypeconvnode.create_internal(left,resultdef));
  1680. left:=nil;
  1681. result:=newblock;
  1682. exit;
  1683. end;
  1684. end
  1685. else
  1686. begin
  1687. { only if the same size or formal def, and }
  1688. { don't allow type casting of constants to }
  1689. { structured types }
  1690. if not(
  1691. (left.resultdef.typ=formaldef) or
  1692. (
  1693. not(is_open_array(left.resultdef)) and
  1694. not(is_array_constructor(left.resultdef)) and
  1695. (left.resultdef.size=resultdef.size) and
  1696. { disallow casts of const nodes }
  1697. (not is_constnode(left) or
  1698. { however, there are some exceptions }
  1699. (not(resultdef.typ in [arraydef,recorddef,setdef,stringdef,
  1700. filedef,variantdef,objectdef]) or
  1701. is_class_or_interface_or_objc(resultdef) or
  1702. { the softfloat code generates casts <const. float> to record }
  1703. (nf_internal in flags)
  1704. ))
  1705. ) or
  1706. (
  1707. is_void(left.resultdef) and
  1708. (left.nodetype=derefn)
  1709. )
  1710. ) then
  1711. CGMessage2(type_e_illegal_type_conversion,left.resultdef.GetTypeName,resultdef.GetTypeName);
  1712. end;
  1713. end
  1714. else
  1715. IncompatibleTypes(left.resultdef,resultdef);
  1716. end;
  1717. else
  1718. internalerror(200211231);
  1719. end;
  1720. end;
  1721. { Give hint or warning for unportable code, exceptions are
  1722. - typecasts from constants
  1723. - void }
  1724. if not(nf_internal in flags) and
  1725. (left.nodetype<>ordconstn) and
  1726. not(is_void(left.resultdef)) and
  1727. (((left.resultdef.typ=orddef) and
  1728. (resultdef.typ in [pointerdef,procvardef,classrefdef])) or
  1729. ((resultdef.typ=orddef) and
  1730. (left.resultdef.typ in [pointerdef,procvardef,classrefdef]))) then
  1731. begin
  1732. {Converting pointers to signed integers is a bad idea. Warn.}
  1733. warn_pointer_to_signed:=(resultdef.typ=orddef) and (Torddef(resultdef).ordtype in [s8bit,s16bit,s32bit,s64bit]);
  1734. { Give a warning when sizes don't match, because then info will be lost }
  1735. if left.resultdef.size=resultdef.size then
  1736. CGMessage(type_h_pointer_to_longint_conv_not_portable)
  1737. else
  1738. CGMessage(type_w_pointer_to_longint_conv_not_portable);
  1739. end;
  1740. result := simplify;
  1741. if assigned(result) then
  1742. exit;
  1743. { now call the resultdef helper to do constant folding }
  1744. result:=typecheck_call_helper(convtype);
  1745. end;
  1746. {$ifndef cpu64bitaddr}
  1747. { checks whether we can safely remove 64 bit typeconversions }
  1748. { in case range and overflow checking are off, and in case }
  1749. { the result of this node tree is downcasted again to a }
  1750. { 8/16/32 bit value afterwards }
  1751. function checkremove64bittypeconvs(n: tnode): boolean;
  1752. var
  1753. gotmuldivmod, gotsint: boolean;
  1754. { checks whether a node is either an u32bit, or originally }
  1755. { was one but was implicitly converted to s64bit }
  1756. function wasoriginallyint32(n: tnode): boolean;
  1757. begin
  1758. if (n.resultdef.typ<>orddef) then
  1759. exit(false);
  1760. if (torddef(n.resultdef).ordtype in [s32bit,u32bit]) then
  1761. begin
  1762. if (torddef(n.resultdef).ordtype=s32bit) then
  1763. gotsint:=true;
  1764. exit(true);
  1765. end;
  1766. if (torddef(n.resultdef).ordtype=s64bit) and
  1767. { nf_explicit is also set for explicitly typecasted }
  1768. { ordconstn's }
  1769. ([nf_internal,nf_explicit]*n.flags=[]) and
  1770. { either a typeconversion node coming from u32bit }
  1771. (((n.nodetype=typeconvn) and
  1772. (ttypeconvnode(n).left.resultdef.typ=orddef) and
  1773. (torddef(ttypeconvnode(n).left.resultdef).ordtype in [s32bit,u32bit])) or
  1774. { or an ordconstnode which was/is a valid cardinal }
  1775. ((n.nodetype=ordconstn) and
  1776. (tordconstnode(n).value>=int64(low(longint))) and
  1777. (tordconstnode(n).value<=high(cardinal)))) then
  1778. begin
  1779. if ((n.nodetype=typeconvn) and
  1780. (torddef(ttypeconvnode(n).left.resultdef).ordtype=s32bit)) or
  1781. ((n.nodetype=ordconstn) and
  1782. (tordconstnode(n).value<0)) then
  1783. gotsint:=true;
  1784. exit(true);
  1785. end;
  1786. result:=false;
  1787. end;
  1788. function docheckremove64bittypeconvs(n: tnode): boolean;
  1789. begin
  1790. result:=false;
  1791. if wasoriginallyint32(n) then
  1792. exit(true);
  1793. case n.nodetype of
  1794. subn,orn,xorn:
  1795. begin
  1796. { nf_internal is set by taddnode.typecheckpass in }
  1797. { case the arguments of this subn were u32bit, but }
  1798. { upcasted to s64bit for calculation correctness }
  1799. { (normally only needed when range checking, but }
  1800. { also done otherwise so there is no difference }
  1801. { in overload choosing etc between $r+ and $r-) }
  1802. if (nf_internal in n.flags) then
  1803. result:=true
  1804. else
  1805. result:=
  1806. docheckremove64bittypeconvs(tbinarynode(n).left) and
  1807. docheckremove64bittypeconvs(tbinarynode(n).right);
  1808. end;
  1809. addn,muln,divn,modn,andn:
  1810. begin
  1811. if n.nodetype in [muln,divn,modn] then
  1812. gotmuldivmod:=true;
  1813. result:=
  1814. docheckremove64bittypeconvs(tbinarynode(n).left) and
  1815. docheckremove64bittypeconvs(tbinarynode(n).right);
  1816. end;
  1817. end;
  1818. end;
  1819. begin { checkremove64bittypeconvs }
  1820. gotmuldivmod:=false;
  1821. gotsint:=false;
  1822. result:=
  1823. docheckremove64bittypeconvs(n) and
  1824. not(gotmuldivmod and gotsint);
  1825. end;
  1826. procedure doremove64bittypeconvs(var n: tnode; todef: tdef);
  1827. begin
  1828. case n.nodetype of
  1829. subn,addn,muln,divn,modn,xorn,andn,orn:
  1830. begin
  1831. exclude(n.flags,nf_internal);
  1832. if is_signed(n.resultdef) then
  1833. begin
  1834. doremove64bittypeconvs(tbinarynode(n).left,s32inttype);
  1835. doremove64bittypeconvs(tbinarynode(n).right,s32inttype);
  1836. n.resultdef:=s32inttype
  1837. end
  1838. else
  1839. begin
  1840. doremove64bittypeconvs(tbinarynode(n).left,u32inttype);
  1841. doremove64bittypeconvs(tbinarynode(n).right,u32inttype);
  1842. n.resultdef:=u32inttype
  1843. end;
  1844. end;
  1845. ordconstn:
  1846. inserttypeconv_internal(n,todef);
  1847. typeconvn:
  1848. n.resultdef:=todef;
  1849. end;
  1850. end;
  1851. {$endif not cpu64bitaddr}
  1852. function ttypeconvnode.simplify: tnode;
  1853. var
  1854. hp: tnode;
  1855. begin
  1856. result := nil;
  1857. { Constant folding and other node transitions to
  1858. remove the typeconv node }
  1859. case left.nodetype of
  1860. stringconstn :
  1861. if (convtype=tc_string_2_string) and
  1862. (
  1863. ((not is_widechararray(left.resultdef) and
  1864. not is_wide_or_unicode_string(left.resultdef)) or
  1865. (tstringdef(resultdef).stringtype in [st_widestring,st_unicodestring]) or
  1866. { non-ascii chars would be replaced with '?' -> loses info }
  1867. not hasnonasciichars(pcompilerwidestring(tstringconstnode(left).value_str)))
  1868. ) then
  1869. begin
  1870. tstringconstnode(left).changestringtype(resultdef);
  1871. result:=left;
  1872. left:=nil;
  1873. exit;
  1874. end;
  1875. realconstn :
  1876. begin
  1877. if (convtype = tc_real_2_currency) then
  1878. result := typecheck_real_to_currency
  1879. else if (convtype = tc_real_2_real) then
  1880. result := typecheck_real_to_real
  1881. else
  1882. exit;
  1883. if not(assigned(result)) then
  1884. begin
  1885. result := left;
  1886. left := nil;
  1887. end;
  1888. if (result.nodetype = realconstn) then
  1889. begin
  1890. hp:=result;
  1891. result:=crealconstnode.create(trealconstnode(hp).value_real,resultdef);
  1892. if ([nf_explicit,nf_internal] * flags <> []) then
  1893. include(result.flags, nf_explicit);
  1894. hp.free;
  1895. end;
  1896. end;
  1897. niln :
  1898. begin
  1899. { nil to ordinal node }
  1900. if (resultdef.typ=orddef) then
  1901. begin
  1902. hp:=cordconstnode.create(0,resultdef,true);
  1903. if ([nf_explicit,nf_internal] * flags <> []) then
  1904. include(hp.flags, nf_explicit);
  1905. result:=hp;
  1906. exit;
  1907. end
  1908. else
  1909. { fold nil to any pointer type }
  1910. if (resultdef.typ=pointerdef) then
  1911. begin
  1912. hp:=cnilnode.create;
  1913. hp.resultdef:=resultdef;
  1914. if ([nf_explicit,nf_internal] * flags <> []) then
  1915. include(hp.flags, nf_explicit);
  1916. result:=hp;
  1917. exit;
  1918. end
  1919. else
  1920. { remove typeconv after niln, but not when the result is a
  1921. methodpointer. The typeconv of the methodpointer will then
  1922. take care of updateing size of niln to OS_64 }
  1923. if not((resultdef.typ=procvardef) and
  1924. (po_methodpointer in tprocvardef(resultdef).procoptions)) then
  1925. begin
  1926. left.resultdef:=resultdef;
  1927. if ([nf_explicit,nf_internal] * flags <> []) then
  1928. include(left.flags, nf_explicit);
  1929. result:=left;
  1930. left:=nil;
  1931. exit;
  1932. end;
  1933. end;
  1934. ordconstn :
  1935. begin
  1936. { ordinal contants can be directly converted }
  1937. { but not char to char because it is a widechar to char or via versa }
  1938. { which needs extra code to do the code page transistion }
  1939. { constant ordinal to pointer }
  1940. if (resultdef.typ=pointerdef) and
  1941. (convtype<>tc_cchar_2_pchar) then
  1942. begin
  1943. hp:=cpointerconstnode.create(TConstPtrUInt(tordconstnode(left).value.uvalue),resultdef);
  1944. if ([nf_explicit,nf_internal] * flags <> []) then
  1945. include(hp.flags, nf_explicit);
  1946. result:=hp;
  1947. exit;
  1948. end
  1949. else if is_ordinal(resultdef) and
  1950. not(convtype=tc_char_2_char) then
  1951. begin
  1952. { replace the resultdef and recheck the range }
  1953. if ([nf_explicit,nf_internal] * flags <> []) then
  1954. include(left.flags, nf_explicit)
  1955. else
  1956. { no longer an ordconst with an explicit typecast }
  1957. exclude(left.flags, nf_explicit);
  1958. { when converting from one boolean type to another, force }
  1959. { booleans to 0/1, and byte/word/long/qwordbool to 0/-1 }
  1960. { (Delphi-compatibile) }
  1961. if is_boolean(left.resultdef) and
  1962. is_boolean(resultdef) and
  1963. (is_cbool(left.resultdef) or
  1964. is_cbool(resultdef)) then
  1965. begin
  1966. if is_pasbool(resultdef) then
  1967. tordconstnode(left).value:=ord(tordconstnode(left).value<>0)
  1968. else
  1969. {$ifdef VER2_2}
  1970. tordconstnode(left).value:=ord(tordconstnode(left).value<>0);
  1971. tordconstnode(left).value:=-tordconstnode(left).value;
  1972. {$else}
  1973. tordconstnode(left).value:=-ord(tordconstnode(left).value<>0);
  1974. {$endif VER2_2}
  1975. end
  1976. else
  1977. testrange(resultdef,tordconstnode(left).value,(nf_explicit in flags));
  1978. left.resultdef:=resultdef;
  1979. result:=left;
  1980. left:=nil;
  1981. exit;
  1982. end;
  1983. end;
  1984. pointerconstn :
  1985. begin
  1986. { pointerconstn to any pointer is folded too }
  1987. if (resultdef.typ=pointerdef) then
  1988. begin
  1989. left.resultdef:=resultdef;
  1990. if ([nf_explicit,nf_internal] * flags <> []) then
  1991. include(left.flags, nf_explicit)
  1992. else
  1993. { no longer an ordconst with an explicit typecast }
  1994. exclude(left.flags, nf_explicit);
  1995. result:=left;
  1996. left:=nil;
  1997. exit;
  1998. end
  1999. { constant pointer to ordinal }
  2000. else if is_ordinal(resultdef) then
  2001. begin
  2002. hp:=cordconstnode.create(TConstExprInt(tpointerconstnode(left).value),
  2003. resultdef,not(nf_explicit in flags));
  2004. if ([nf_explicit,nf_internal] * flags <> []) then
  2005. include(hp.flags, nf_explicit);
  2006. result:=hp;
  2007. exit;
  2008. end;
  2009. end;
  2010. end;
  2011. {$ifndef cpu64bitaddr}
  2012. { must be done before code below, because we need the
  2013. typeconversions for ordconstn's as well }
  2014. case convtype of
  2015. tc_int_2_int:
  2016. begin
  2017. if (localswitches * [cs_check_range,cs_check_overflow] = []) and
  2018. (resultdef.typ in [pointerdef,orddef,enumdef]) and
  2019. (resultdef.size <= 4) and
  2020. is_64bitint(left.resultdef) and
  2021. (left.nodetype in [subn,addn,muln,divn,modn,xorn,andn,orn]) and
  2022. checkremove64bittypeconvs(left) then
  2023. begin
  2024. { avoid unnecessary widening of intermediary calculations }
  2025. { to 64 bit }
  2026. doremove64bittypeconvs(left,generrordef);
  2027. end;
  2028. end;
  2029. end;
  2030. {$endif not cpu64bitaddr}
  2031. end;
  2032. procedure Ttypeconvnode.mark_write;
  2033. begin
  2034. left.mark_write;
  2035. end;
  2036. function ttypeconvnode.first_cord_to_pointer : tnode;
  2037. begin
  2038. result:=nil;
  2039. internalerror(200104043);
  2040. end;
  2041. function ttypeconvnode.first_int_to_int : tnode;
  2042. begin
  2043. first_int_to_int:=nil;
  2044. expectloc:=left.expectloc;
  2045. if not is_void(left.resultdef) then
  2046. begin
  2047. if (left.expectloc<>LOC_REGISTER) and
  2048. (resultdef.size>left.resultdef.size) then
  2049. expectloc:=LOC_REGISTER
  2050. else
  2051. if (left.expectloc=LOC_CREGISTER) and
  2052. (resultdef.size<left.resultdef.size) then
  2053. expectloc:=LOC_REGISTER;
  2054. end;
  2055. end;
  2056. function ttypeconvnode.first_cstring_to_pchar : tnode;
  2057. begin
  2058. result:=nil;
  2059. expectloc:=LOC_REGISTER;
  2060. end;
  2061. function ttypeconvnode.first_cstring_to_int : tnode;
  2062. begin
  2063. result:=nil;
  2064. internalerror(200510014);
  2065. end;
  2066. function ttypeconvnode.first_string_to_chararray : tnode;
  2067. begin
  2068. first_string_to_chararray:=nil;
  2069. expectloc:=left.expectloc;
  2070. end;
  2071. function ttypeconvnode.first_char_to_string : tnode;
  2072. begin
  2073. first_char_to_string:=nil;
  2074. expectloc:=LOC_REFERENCE;
  2075. end;
  2076. function ttypeconvnode.first_nothing : tnode;
  2077. begin
  2078. first_nothing:=nil;
  2079. end;
  2080. function ttypeconvnode.first_array_to_pointer : tnode;
  2081. begin
  2082. first_array_to_pointer:=nil;
  2083. expectloc:=LOC_REGISTER;
  2084. end;
  2085. function ttypeconvnode.first_int_to_real: tnode;
  2086. var
  2087. fname: string[32];
  2088. begin
  2089. if target_info.system in system_wince then
  2090. begin
  2091. { converting a 64bit integer to a float requires a helper }
  2092. if is_64bitint(left.resultdef) or
  2093. is_currency(left.resultdef) then
  2094. begin
  2095. { hack to avoid double division by 10000, as it's
  2096. already done by typecheckpass.resultdef_int_to_real }
  2097. if is_currency(left.resultdef) then
  2098. left.resultdef := s64inttype;
  2099. if is_signed(left.resultdef) then
  2100. fname:='I64TO'
  2101. else
  2102. fname:='UI64TO';
  2103. end
  2104. else
  2105. { other integers are supposed to be 32 bit }
  2106. begin
  2107. if is_signed(left.resultdef) then
  2108. fname:='ITO'
  2109. else
  2110. fname:='UTO';
  2111. firstpass(left);
  2112. end;
  2113. if tfloatdef(resultdef).floattype=s64real then
  2114. fname:=fname+'D'
  2115. else
  2116. fname:=fname+'S';
  2117. result:=ccallnode.createintern(fname,ccallparanode.create(
  2118. left,nil));
  2119. left:=nil;
  2120. firstpass(result);
  2121. exit;
  2122. end
  2123. else
  2124. begin
  2125. { converting a 64bit integer to a float requires a helper }
  2126. if is_64bitint(left.resultdef) or
  2127. is_currency(left.resultdef) then
  2128. begin
  2129. { hack to avoid double division by 10000, as it's
  2130. already done by typecheckpass.resultdef_int_to_real }
  2131. if is_currency(left.resultdef) then
  2132. left.resultdef := s64inttype;
  2133. if is_signed(left.resultdef) then
  2134. fname:='int64_to_'
  2135. else
  2136. { we can't do better currently }
  2137. fname:='int64_to_';
  2138. end
  2139. else
  2140. { other integers are supposed to be 32 bit }
  2141. begin
  2142. if is_signed(left.resultdef) then
  2143. fname:='int32_to_'
  2144. else
  2145. fname:='int64_to_';
  2146. firstpass(left);
  2147. end;
  2148. if tfloatdef(resultdef).floattype=s64real then
  2149. fname:=fname+'float64'
  2150. else
  2151. fname:=fname+'float32';
  2152. result:=ctypeconvnode.create_internal(ccallnode.createintern(fname,ccallparanode.create(
  2153. left,nil)),resultdef);
  2154. left:=nil;
  2155. firstpass(result);
  2156. exit;
  2157. end;
  2158. end;
  2159. function ttypeconvnode.first_real_to_real : tnode;
  2160. begin
  2161. {$ifdef cpufpemu}
  2162. if cs_fp_emulation in current_settings.moduleswitches then
  2163. begin
  2164. if target_info.system in system_wince then
  2165. begin
  2166. case tfloatdef(left.resultdef).floattype of
  2167. s32real:
  2168. case tfloatdef(resultdef).floattype of
  2169. s64real:
  2170. result:=ccallnode.createintern('STOD',ccallparanode.create(left,nil));
  2171. s32real:
  2172. begin
  2173. result:=left;
  2174. left:=nil;
  2175. end;
  2176. else
  2177. internalerror(2005082704);
  2178. end;
  2179. s64real:
  2180. case tfloatdef(resultdef).floattype of
  2181. s32real:
  2182. result:=ccallnode.createintern('DTOS',ccallparanode.create(left,nil));
  2183. s64real:
  2184. begin
  2185. result:=left;
  2186. left:=nil;
  2187. end;
  2188. else
  2189. internalerror(2005082703);
  2190. end;
  2191. else
  2192. internalerror(2005082702);
  2193. end;
  2194. left:=nil;
  2195. firstpass(result);
  2196. exit;
  2197. end
  2198. else
  2199. begin
  2200. case tfloatdef(left.resultdef).floattype of
  2201. s32real:
  2202. case tfloatdef(resultdef).floattype of
  2203. s64real:
  2204. result:=ctypeconvnode.create_explicit(ccallnode.createintern('float32_to_float64',ccallparanode.create(
  2205. ctypeconvnode.create_internal(left,search_system_type('FLOAT32REC').typedef),nil)),resultdef);
  2206. s32real:
  2207. begin
  2208. result:=left;
  2209. left:=nil;
  2210. end;
  2211. else
  2212. internalerror(200610151);
  2213. end;
  2214. s64real:
  2215. case tfloatdef(resultdef).floattype of
  2216. s32real:
  2217. result:=ctypeconvnode.create_explicit(ccallnode.createintern('float64_to_float32',ccallparanode.create(
  2218. ctypeconvnode.create_internal(left,search_system_type('FLOAT64').typedef),nil)),resultdef);
  2219. s64real:
  2220. begin
  2221. result:=left;
  2222. left:=nil;
  2223. end;
  2224. else
  2225. internalerror(200610152);
  2226. end;
  2227. else
  2228. internalerror(200610153);
  2229. end;
  2230. left:=nil;
  2231. firstpass(result);
  2232. exit;
  2233. end;
  2234. end
  2235. else
  2236. {$endif cpufpemu}
  2237. begin
  2238. first_real_to_real:=nil;
  2239. expectloc:=LOC_FPUREGISTER;
  2240. end;
  2241. end;
  2242. function ttypeconvnode.first_pointer_to_array : tnode;
  2243. begin
  2244. first_pointer_to_array:=nil;
  2245. expectloc:=LOC_REFERENCE;
  2246. end;
  2247. function ttypeconvnode.first_cchar_to_pchar : tnode;
  2248. begin
  2249. first_cchar_to_pchar:=nil;
  2250. internalerror(200104021);
  2251. end;
  2252. function ttypeconvnode.first_bool_to_int : tnode;
  2253. begin
  2254. first_bool_to_int:=nil;
  2255. { byte(boolean) or word(wordbool) or longint(longbool) must
  2256. be accepted for var parameters }
  2257. if (nf_explicit in flags) and
  2258. (left.resultdef.size=resultdef.size) and
  2259. (left.expectloc in [LOC_REFERENCE,LOC_CREFERENCE,LOC_CREGISTER]) then
  2260. exit;
  2261. { when converting to 64bit, first convert to a 32bit int and then }
  2262. { convert to a 64bit int (only necessary for 32bit processors) (JM) }
  2263. if resultdef.size > sizeof(aint) then
  2264. begin
  2265. result := ctypeconvnode.create_internal(left,s32inttype);
  2266. result := ctypeconvnode.create(result,resultdef);
  2267. left := nil;
  2268. firstpass(result);
  2269. exit;
  2270. end;
  2271. expectloc:=LOC_REGISTER;
  2272. end;
  2273. function ttypeconvnode.first_int_to_bool : tnode;
  2274. begin
  2275. first_int_to_bool:=nil;
  2276. { byte(boolean) or word(wordbool) or longint(longbool) must
  2277. be accepted for var parameters }
  2278. if (nf_explicit in flags) and
  2279. (left.resultdef.size=resultdef.size) and
  2280. (left.expectloc in [LOC_REFERENCE,LOC_CREFERENCE,LOC_CREGISTER]) then
  2281. exit;
  2282. { when converting 64bit int to C-ctyle boolean, first convert to an int32 and then }
  2283. { convert to a boolean (only necessary for 32bit processors) }
  2284. if (left.resultdef.size > sizeof(aint)) and (left.resultdef.size<>resultdef.size)
  2285. and is_cbool(resultdef) then
  2286. begin
  2287. left:=ctypeconvnode.create_internal(left,s32inttype);
  2288. firstpass(left);
  2289. exit;
  2290. end;
  2291. expectloc:=LOC_REGISTER;
  2292. end;
  2293. function ttypeconvnode.first_bool_to_bool : tnode;
  2294. begin
  2295. first_bool_to_bool:=nil;
  2296. if (left.expectloc in [LOC_FLAGS,LOC_JUMP]) then
  2297. expectloc := left.expectloc
  2298. else
  2299. expectloc:=LOC_REGISTER;
  2300. end;
  2301. function ttypeconvnode.first_char_to_char : tnode;
  2302. var
  2303. fname: string[18];
  2304. begin
  2305. if (torddef(resultdef).ordtype=uchar) and
  2306. (torddef(left.resultdef).ordtype=uwidechar) then
  2307. fname := 'fpc_uchar_to_char'
  2308. else if (torddef(resultdef).ordtype=uwidechar) and
  2309. (torddef(left.resultdef).ordtype=uchar) then
  2310. fname := 'fpc_char_to_uchar'
  2311. else
  2312. internalerror(2007081201);
  2313. result := ccallnode.createintern(fname,ccallparanode.create(left,nil));
  2314. left:=nil;
  2315. firstpass(result);
  2316. end;
  2317. function ttypeconvnode.first_proc_to_procvar : tnode;
  2318. begin
  2319. first_proc_to_procvar:=nil;
  2320. { if we take the address of a nested function, it'll }
  2321. { probably be used in a foreach() construct and then }
  2322. { the parent needs a stackframe }
  2323. if (tprocdef(left.resultdef).parast.symtablelevel>=normal_function_level) then
  2324. include(current_procinfo.flags,pi_needs_stackframe);
  2325. if tabstractprocdef(resultdef).is_addressonly then
  2326. expectloc:=LOC_REGISTER
  2327. else
  2328. begin
  2329. if not(left.expectloc in [LOC_CREFERENCE,LOC_REFERENCE]) then
  2330. CGMessage(parser_e_illegal_expression);
  2331. expectloc:=left.expectloc;
  2332. end;
  2333. end;
  2334. function ttypeconvnode.first_nil_to_methodprocvar : tnode;
  2335. begin
  2336. first_nil_to_methodprocvar:=nil;
  2337. expectloc:=LOC_REFERENCE;
  2338. end;
  2339. function ttypeconvnode.first_set_to_set : tnode;
  2340. var
  2341. newstatement : tstatementnode;
  2342. temp : ttempcreatenode;
  2343. begin
  2344. { in theory, we should do range checking here,
  2345. but Delphi doesn't do it either (FK) }
  2346. if left.nodetype=setconstn then
  2347. begin
  2348. left.resultdef:=resultdef;
  2349. result:=left;
  2350. left:=nil;
  2351. end
  2352. { equal sets for the code generator? }
  2353. else if (left.resultdef.size=resultdef.size) and
  2354. (tsetdef(left.resultdef).setbase=tsetdef(resultdef).setbase) then
  2355. { TODO: This causes wrong (but Delphi-compatible) results for disjoint subsets}
  2356. { e.g., this prints true because of this:
  2357. var
  2358. sa: set of 1..2;
  2359. sb: set of 5..6;
  2360. b: byte;
  2361. begin
  2362. b:=1;
  2363. sa:=[1..2];
  2364. sb:=sa;
  2365. writeln(b in sb);
  2366. end.
  2367. }
  2368. begin
  2369. result:=left;
  2370. left:=nil;
  2371. end
  2372. else
  2373. begin
  2374. result:=internalstatements(newstatement);
  2375. { in case left is a smallset expression, it can be an addn or so. }
  2376. { fpc_varset_load expects a formal const parameter, which doesn't }
  2377. { accept set addn's -> assign to a temp first and pass the temp }
  2378. if not(left.expectloc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  2379. begin
  2380. temp:=ctempcreatenode.create(left.resultdef,left.resultdef.size,tt_persistent,false);
  2381. addstatement(newstatement,temp);
  2382. { temp := left }
  2383. addstatement(newstatement,cassignmentnode.create(
  2384. ctemprefnode.create(temp),left));
  2385. addstatement(newstatement,ctempdeletenode.create_normal_temp(temp));
  2386. addstatement(newstatement,ctemprefnode.create(temp));
  2387. left:=result;
  2388. firstpass(left);
  2389. { recreate the result's internalstatements list }
  2390. result:=internalstatements(newstatement);
  2391. end;
  2392. { create temp for result }
  2393. temp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,true);
  2394. addstatement(newstatement,temp);
  2395. addstatement(newstatement,ccallnode.createintern('fpc_varset_load',
  2396. ccallparanode.create(cordconstnode.create(tsetdef(left.resultdef).setbase div 8 - tsetdef(resultdef).setbase div 8,sinttype,false),
  2397. ccallparanode.create(cordconstnode.create(resultdef.size,sinttype,false),
  2398. ccallparanode.create(ctemprefnode.create(temp),
  2399. ccallparanode.create(cordconstnode.create(left.resultdef.size,sinttype,false),
  2400. ccallparanode.create(left,nil))))))
  2401. );
  2402. addstatement(newstatement,ctempdeletenode.create_normal_temp(temp));
  2403. addstatement(newstatement,ctemprefnode.create(temp));
  2404. left:=nil;
  2405. end;
  2406. end;
  2407. function ttypeconvnode.first_ansistring_to_pchar : tnode;
  2408. begin
  2409. first_ansistring_to_pchar:=nil;
  2410. expectloc:=LOC_REGISTER;
  2411. end;
  2412. function ttypeconvnode.first_arrayconstructor_to_set : tnode;
  2413. begin
  2414. first_arrayconstructor_to_set:=nil;
  2415. internalerror(200104022);
  2416. end;
  2417. function ttypeconvnode.first_class_to_intf : tnode;
  2418. var
  2419. hd : tobjectdef;
  2420. ImplIntf : TImplementedInterface;
  2421. begin
  2422. result:=nil;
  2423. expectloc:=LOC_REGISTER;
  2424. hd:=tobjectdef(left.resultdef);
  2425. while assigned(hd) do
  2426. begin
  2427. ImplIntf:=hd.find_implemented_interface(tobjectdef(resultdef));
  2428. if assigned(ImplIntf) then
  2429. begin
  2430. case ImplIntf.IType of
  2431. etStandard:
  2432. { handle in pass 2 }
  2433. ;
  2434. etFieldValue:
  2435. if is_interface(tobjectdef(resultdef)) then
  2436. begin
  2437. result:=left;
  2438. propaccesslist_to_node(result,tpropertysym(implintf.implementsgetter).owner,tpropertysym(implintf.implementsgetter).propaccesslist[palt_read]);
  2439. left:=nil;
  2440. end
  2441. else
  2442. begin
  2443. internalerror(200802213);
  2444. end;
  2445. etStaticMethodResult,
  2446. etVirtualMethodResult:
  2447. if is_interface(tobjectdef(resultdef)) then
  2448. begin
  2449. { constructor create(l:tnode; v : tprocsym;st : TSymtable; mp: tnode; callflags:tcallnodeflags); }
  2450. result:=ccallnode.create(nil,tprocsym(tpropertysym(implintf.implementsgetter).propaccesslist[palt_read].firstsym^.sym),
  2451. tprocsym(tpropertysym(implintf.implementsgetter).propaccesslist[palt_read].firstsym^.sym).owner,
  2452. left,[]);
  2453. addsymref(tpropertysym(implintf.implementsgetter).propaccesslist[palt_read].firstsym^.sym);
  2454. left:=nil;
  2455. end
  2456. else if is_class(tobjectdef(resultdef)) then
  2457. begin
  2458. internalerror(200802211);
  2459. end
  2460. else
  2461. internalerror(200802231);
  2462. else
  2463. internalerror(200802165);
  2464. end;
  2465. break;
  2466. end;
  2467. hd:=hd.childof;
  2468. end;
  2469. if hd=nil then
  2470. internalerror(200802164);
  2471. end;
  2472. function ttypeconvnode.first_string_to_string : tnode;
  2473. var
  2474. procname: string[31];
  2475. newblock : tblocknode;
  2476. newstat : tstatementnode;
  2477. restemp : ttempcreatenode;
  2478. begin
  2479. { get the correct procedure name }
  2480. procname := 'fpc_'+tstringdef(left.resultdef).stringtypname+
  2481. '_to_'+tstringdef(resultdef).stringtypname;
  2482. if tstringdef(resultdef).stringtype=st_shortstring then
  2483. begin
  2484. newblock:=internalstatements(newstat);
  2485. restemp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,false);
  2486. addstatement(newstat,restemp);
  2487. addstatement(newstat,ccallnode.createintern(procname,ccallparanode.create(left,ccallparanode.create(
  2488. ctemprefnode.create(restemp),nil))));
  2489. addstatement(newstat,ctempdeletenode.create_normal_temp(restemp));
  2490. addstatement(newstat,ctemprefnode.create(restemp));
  2491. result:=newblock;
  2492. end
  2493. else
  2494. result := ccallnode.createinternres(procname,ccallparanode.create(left,nil),resultdef);
  2495. left:=nil;
  2496. end;
  2497. function ttypeconvnode._first_int_to_int : tnode;
  2498. begin
  2499. result:=first_int_to_int;
  2500. end;
  2501. function ttypeconvnode._first_cstring_to_pchar : tnode;
  2502. begin
  2503. result:=first_cstring_to_pchar;
  2504. end;
  2505. function ttypeconvnode._first_cstring_to_int : tnode;
  2506. begin
  2507. result:=first_cstring_to_int;
  2508. end;
  2509. function ttypeconvnode._first_string_to_chararray : tnode;
  2510. begin
  2511. result:=first_string_to_chararray;
  2512. end;
  2513. function ttypeconvnode._first_char_to_string : tnode;
  2514. begin
  2515. result:=first_char_to_string;
  2516. end;
  2517. function ttypeconvnode._first_nothing : tnode;
  2518. begin
  2519. result:=first_nothing;
  2520. end;
  2521. function ttypeconvnode._first_array_to_pointer : tnode;
  2522. begin
  2523. result:=first_array_to_pointer;
  2524. end;
  2525. function ttypeconvnode._first_int_to_real : tnode;
  2526. begin
  2527. result:=first_int_to_real;
  2528. end;
  2529. function ttypeconvnode._first_real_to_real : tnode;
  2530. begin
  2531. result:=first_real_to_real;
  2532. end;
  2533. function ttypeconvnode._first_pointer_to_array : tnode;
  2534. begin
  2535. result:=first_pointer_to_array;
  2536. end;
  2537. function ttypeconvnode._first_cchar_to_pchar : tnode;
  2538. begin
  2539. result:=first_cchar_to_pchar;
  2540. end;
  2541. function ttypeconvnode._first_bool_to_int : tnode;
  2542. begin
  2543. result:=first_bool_to_int;
  2544. end;
  2545. function ttypeconvnode._first_int_to_bool : tnode;
  2546. begin
  2547. result:=first_int_to_bool;
  2548. end;
  2549. function ttypeconvnode._first_bool_to_bool : tnode;
  2550. begin
  2551. result:=first_bool_to_bool;
  2552. end;
  2553. function ttypeconvnode._first_proc_to_procvar : tnode;
  2554. begin
  2555. result:=first_proc_to_procvar;
  2556. end;
  2557. function ttypeconvnode._first_nil_to_methodprocvar : tnode;
  2558. begin
  2559. result:=first_nil_to_methodprocvar;
  2560. end;
  2561. function ttypeconvnode._first_set_to_set : tnode;
  2562. begin
  2563. result:=first_set_to_set;
  2564. end;
  2565. function ttypeconvnode._first_cord_to_pointer : tnode;
  2566. begin
  2567. result:=first_cord_to_pointer;
  2568. end;
  2569. function ttypeconvnode._first_ansistring_to_pchar : tnode;
  2570. begin
  2571. result:=first_ansistring_to_pchar;
  2572. end;
  2573. function ttypeconvnode._first_arrayconstructor_to_set : tnode;
  2574. begin
  2575. result:=first_arrayconstructor_to_set;
  2576. end;
  2577. function ttypeconvnode._first_class_to_intf : tnode;
  2578. begin
  2579. result:=first_class_to_intf;
  2580. end;
  2581. function ttypeconvnode._first_char_to_char : tnode;
  2582. begin
  2583. result:=first_char_to_char;
  2584. end;
  2585. function ttypeconvnode._first_string_to_string : tnode;
  2586. begin
  2587. result:=first_string_to_string;
  2588. end;
  2589. function ttypeconvnode.first_call_helper(c : tconverttype) : tnode;
  2590. const
  2591. firstconvert : array[tconverttype] of pointer = (
  2592. nil, { none }
  2593. @ttypeconvnode._first_nothing, {equal}
  2594. @ttypeconvnode._first_nothing, {not_possible}
  2595. @ttypeconvnode._first_string_to_string,
  2596. @ttypeconvnode._first_char_to_string,
  2597. @ttypeconvnode._first_nothing, { char_2_chararray, needs nothing extra }
  2598. nil, { removed in typecheck_chararray_to_string }
  2599. @ttypeconvnode._first_cchar_to_pchar,
  2600. @ttypeconvnode._first_cstring_to_pchar,
  2601. @ttypeconvnode._first_cstring_to_int,
  2602. @ttypeconvnode._first_ansistring_to_pchar,
  2603. @ttypeconvnode._first_string_to_chararray,
  2604. nil, { removed in typecheck_chararray_to_string }
  2605. @ttypeconvnode._first_array_to_pointer,
  2606. @ttypeconvnode._first_pointer_to_array,
  2607. @ttypeconvnode._first_int_to_int,
  2608. @ttypeconvnode._first_int_to_bool,
  2609. @ttypeconvnode._first_bool_to_bool,
  2610. @ttypeconvnode._first_bool_to_int,
  2611. @ttypeconvnode._first_real_to_real,
  2612. @ttypeconvnode._first_int_to_real,
  2613. nil, { removed in typecheck_real_to_currency }
  2614. @ttypeconvnode._first_proc_to_procvar,
  2615. @ttypeconvnode._first_nil_to_methodprocvar,
  2616. @ttypeconvnode._first_arrayconstructor_to_set,
  2617. @ttypeconvnode._first_set_to_set,
  2618. @ttypeconvnode._first_cord_to_pointer,
  2619. @ttypeconvnode._first_nothing,
  2620. @ttypeconvnode._first_nothing,
  2621. @ttypeconvnode._first_class_to_intf,
  2622. @ttypeconvnode._first_char_to_char,
  2623. @ttypeconvnode._first_nothing,
  2624. @ttypeconvnode._first_nothing,
  2625. nil,
  2626. nil,
  2627. nil,
  2628. nil,
  2629. nil,
  2630. nil,
  2631. nil
  2632. );
  2633. type
  2634. tprocedureofobject = function : tnode of object;
  2635. var
  2636. r : packed record
  2637. proc : pointer;
  2638. obj : pointer;
  2639. end;
  2640. begin
  2641. { this is a little bit dirty but it works }
  2642. { and should be quite portable too }
  2643. r.proc:=firstconvert[c];
  2644. r.obj:=self;
  2645. if not assigned(r.proc) then
  2646. internalerror(200312081);
  2647. first_call_helper:=tprocedureofobject(r)()
  2648. end;
  2649. function ttypeconvnode.pass_1 : tnode;
  2650. begin
  2651. if warn_pointer_to_signed then
  2652. cgmessage(type_w_pointer_to_signed);
  2653. result:=nil;
  2654. firstpass(left);
  2655. if codegenerror then
  2656. exit;
  2657. expectloc:=left.expectloc;
  2658. result:=first_call_helper(convtype);
  2659. end;
  2660. function ttypeconvnode.retains_value_location:boolean;
  2661. begin
  2662. result:=(convtype=tc_equal) or
  2663. { typecasting from void is always allowed }
  2664. is_void(left.resultdef) or
  2665. (left.resultdef.typ=formaldef) or
  2666. { int 2 int with same size reuses same location, or for
  2667. tp7 mode also allow size < orignal size }
  2668. (
  2669. (convtype=tc_int_2_int) and
  2670. (
  2671. (resultdef.size=left.resultdef.size) or
  2672. ((m_tp7 in current_settings.modeswitches) and
  2673. (resultdef.size<left.resultdef.size))
  2674. )
  2675. ) or
  2676. { int 2 bool/bool 2 int, explicit typecast, see also nx86cnv }
  2677. ((convtype in [tc_int_2_bool,tc_bool_2_int,tc_bool_2_bool]) and
  2678. (nf_explicit in flags) and
  2679. (resultdef.size=left.resultdef.size));
  2680. end;
  2681. function ttypeconvnode.assign_allowed:boolean;
  2682. begin
  2683. result:=retains_value_location;
  2684. { When using only a part of the value it can't be in a register since
  2685. that will load the value in a new register first }
  2686. { the same goes for changing the sign of equal-sized values which
  2687. are smaller than an entire register }
  2688. if result and
  2689. (resultdef.size<left.resultdef.size) or
  2690. ((resultdef.size=left.resultdef.size) and
  2691. (left.resultdef.size<sizeof(aint)) and
  2692. (is_signed(resultdef) xor is_signed(left.resultdef))) then
  2693. make_not_regable(left,[ra_addr_regable]);
  2694. end;
  2695. function ttypeconvnode.docompare(p: tnode) : boolean;
  2696. begin
  2697. docompare :=
  2698. inherited docompare(p) and
  2699. (convtype = ttypeconvnode(p).convtype);
  2700. end;
  2701. procedure ttypeconvnode._second_int_to_int;
  2702. begin
  2703. second_int_to_int;
  2704. end;
  2705. procedure ttypeconvnode._second_string_to_string;
  2706. begin
  2707. second_string_to_string;
  2708. end;
  2709. procedure ttypeconvnode._second_cstring_to_pchar;
  2710. begin
  2711. second_cstring_to_pchar;
  2712. end;
  2713. procedure ttypeconvnode._second_cstring_to_int;
  2714. begin
  2715. second_cstring_to_int;
  2716. end;
  2717. procedure ttypeconvnode._second_string_to_chararray;
  2718. begin
  2719. second_string_to_chararray;
  2720. end;
  2721. procedure ttypeconvnode._second_array_to_pointer;
  2722. begin
  2723. second_array_to_pointer;
  2724. end;
  2725. procedure ttypeconvnode._second_pointer_to_array;
  2726. begin
  2727. second_pointer_to_array;
  2728. end;
  2729. procedure ttypeconvnode._second_chararray_to_string;
  2730. begin
  2731. second_chararray_to_string;
  2732. end;
  2733. procedure ttypeconvnode._second_char_to_string;
  2734. begin
  2735. second_char_to_string;
  2736. end;
  2737. procedure ttypeconvnode._second_int_to_real;
  2738. begin
  2739. second_int_to_real;
  2740. end;
  2741. procedure ttypeconvnode._second_real_to_real;
  2742. begin
  2743. second_real_to_real;
  2744. end;
  2745. procedure ttypeconvnode._second_cord_to_pointer;
  2746. begin
  2747. second_cord_to_pointer;
  2748. end;
  2749. procedure ttypeconvnode._second_proc_to_procvar;
  2750. begin
  2751. second_proc_to_procvar;
  2752. end;
  2753. procedure ttypeconvnode._second_nil_to_methodprocvar;
  2754. begin
  2755. second_nil_to_methodprocvar;
  2756. end;
  2757. procedure ttypeconvnode._second_bool_to_int;
  2758. begin
  2759. second_bool_to_int;
  2760. end;
  2761. procedure ttypeconvnode._second_int_to_bool;
  2762. begin
  2763. second_int_to_bool;
  2764. end;
  2765. procedure ttypeconvnode._second_bool_to_bool;
  2766. begin
  2767. second_bool_to_bool;
  2768. end;
  2769. procedure ttypeconvnode._second_set_to_set;
  2770. begin
  2771. second_set_to_set;
  2772. end;
  2773. procedure ttypeconvnode._second_ansistring_to_pchar;
  2774. begin
  2775. second_ansistring_to_pchar;
  2776. end;
  2777. procedure ttypeconvnode._second_class_to_intf;
  2778. begin
  2779. second_class_to_intf;
  2780. end;
  2781. procedure ttypeconvnode._second_char_to_char;
  2782. begin
  2783. second_char_to_char;
  2784. end;
  2785. procedure ttypeconvnode._second_nothing;
  2786. begin
  2787. second_nothing;
  2788. end;
  2789. procedure ttypeconvnode.second_call_helper(c : tconverttype);
  2790. const
  2791. secondconvert : array[tconverttype] of pointer = (
  2792. @ttypeconvnode._second_nothing, {none}
  2793. @ttypeconvnode._second_nothing, {equal}
  2794. @ttypeconvnode._second_nothing, {not_possible}
  2795. @ttypeconvnode._second_nothing, {second_string_to_string, handled in resultdef pass }
  2796. @ttypeconvnode._second_char_to_string,
  2797. @ttypeconvnode._second_nothing, {char_to_charray}
  2798. @ttypeconvnode._second_nothing, { pchar_to_string, handled in resultdef pass }
  2799. @ttypeconvnode._second_nothing, {cchar_to_pchar}
  2800. @ttypeconvnode._second_cstring_to_pchar,
  2801. @ttypeconvnode._second_cstring_to_int,
  2802. @ttypeconvnode._second_ansistring_to_pchar,
  2803. @ttypeconvnode._second_string_to_chararray,
  2804. @ttypeconvnode._second_nothing, { chararray_to_string, handled in resultdef pass }
  2805. @ttypeconvnode._second_array_to_pointer,
  2806. @ttypeconvnode._second_pointer_to_array,
  2807. @ttypeconvnode._second_int_to_int,
  2808. @ttypeconvnode._second_int_to_bool,
  2809. @ttypeconvnode._second_bool_to_bool,
  2810. @ttypeconvnode._second_bool_to_int,
  2811. @ttypeconvnode._second_real_to_real,
  2812. @ttypeconvnode._second_int_to_real,
  2813. @ttypeconvnode._second_nothing, { real_to_currency, handled in resultdef pass }
  2814. @ttypeconvnode._second_proc_to_procvar,
  2815. @ttypeconvnode._second_nil_to_methodprocvar,
  2816. @ttypeconvnode._second_nothing, { arrayconstructor_to_set }
  2817. @ttypeconvnode._second_nothing, { second_set_to_set, handled in first pass }
  2818. @ttypeconvnode._second_cord_to_pointer,
  2819. @ttypeconvnode._second_nothing, { interface 2 string }
  2820. @ttypeconvnode._second_nothing, { interface 2 guid }
  2821. @ttypeconvnode._second_class_to_intf,
  2822. @ttypeconvnode._second_char_to_char,
  2823. @ttypeconvnode._second_nothing, { dynarray_2_openarray }
  2824. @ttypeconvnode._second_nothing, { pwchar_2_string }
  2825. @ttypeconvnode._second_nothing, { variant_2_dynarray }
  2826. @ttypeconvnode._second_nothing, { dynarray_2_variant}
  2827. @ttypeconvnode._second_nothing, { variant_2_enum }
  2828. @ttypeconvnode._second_nothing, { enum_2_variant }
  2829. @ttypeconvnode._second_nothing, { variant_2_interface }
  2830. @ttypeconvnode._second_nothing, { interface_2_variant }
  2831. @ttypeconvnode._second_nothing { array_2_dynarray }
  2832. );
  2833. type
  2834. tprocedureofobject = procedure of object;
  2835. var
  2836. r : packed record
  2837. proc : pointer;
  2838. obj : pointer;
  2839. end;
  2840. begin
  2841. { this is a little bit dirty but it works }
  2842. { and should be quite portable too }
  2843. r.proc:=secondconvert[c];
  2844. r.obj:=self;
  2845. tprocedureofobject(r)();
  2846. end;
  2847. {*****************************************************************************
  2848. TISNODE
  2849. *****************************************************************************}
  2850. constructor tisnode.create(l,r : tnode);
  2851. begin
  2852. inherited create(isn,l,r);
  2853. end;
  2854. function tisnode.pass_typecheck:tnode;
  2855. var
  2856. paras: tcallparanode;
  2857. begin
  2858. result:=nil;
  2859. typecheckpass(left);
  2860. typecheckpass(right);
  2861. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2862. set_varstate(right,vs_read,[vsf_must_be_valid]);
  2863. if codegenerror then
  2864. exit;
  2865. { Passing a class type to an "is" expression cannot result in a class
  2866. of that type to be constructed.
  2867. }
  2868. include(right.flags,nf_ignore_for_wpo);
  2869. if (right.resultdef.typ=classrefdef) then
  2870. begin
  2871. { left must be a class }
  2872. if is_class(left.resultdef) then
  2873. begin
  2874. { the operands must be related }
  2875. if (not(tobjectdef(left.resultdef).is_related(
  2876. tobjectdef(tclassrefdef(right.resultdef).pointeddef)))) and
  2877. (not(tobjectdef(tclassrefdef(right.resultdef).pointeddef).is_related(
  2878. tobjectdef(left.resultdef)))) then
  2879. CGMessage2(type_e_classes_not_related,left.resultdef.typename,
  2880. tclassrefdef(right.resultdef).pointeddef.typename);
  2881. end
  2882. else
  2883. CGMessage1(type_e_class_type_expected,left.resultdef.typename);
  2884. { call fpc_do_is helper }
  2885. paras := ccallparanode.create(
  2886. left,
  2887. ccallparanode.create(
  2888. right,nil));
  2889. result := ccallnode.createintern('fpc_do_is',paras);
  2890. left := nil;
  2891. right := nil;
  2892. end
  2893. else if is_interface(right.resultdef) then
  2894. begin
  2895. { left is a class }
  2896. if is_class(left.resultdef) then
  2897. begin
  2898. { the class must implement the interface }
  2899. if tobjectdef(left.resultdef).find_implemented_interface(tobjectdef(right.resultdef))=nil then
  2900. CGMessage2(type_e_classes_not_related,
  2901. FullTypeName(left.resultdef,right.resultdef),
  2902. FullTypeName(right.resultdef,left.resultdef))
  2903. end
  2904. { left is an interface }
  2905. else if is_interface(left.resultdef) then
  2906. begin
  2907. { the operands must be related }
  2908. if (not(tobjectdef(left.resultdef).is_related(tobjectdef(right.resultdef)))) and
  2909. (not(tobjectdef(right.resultdef).is_related(tobjectdef(left.resultdef)))) then
  2910. CGMessage2(type_e_classes_not_related,
  2911. FullTypeName(left.resultdef,right.resultdef),
  2912. FullTypeName(right.resultdef,left.resultdef));
  2913. end
  2914. else
  2915. CGMessage1(type_e_class_type_expected,left.resultdef.typename);
  2916. { call fpc_do_is helper }
  2917. paras := ccallparanode.create(
  2918. left,
  2919. ccallparanode.create(
  2920. right,nil));
  2921. result := ccallnode.createintern('fpc_do_is',paras);
  2922. left := nil;
  2923. right := nil;
  2924. end
  2925. else
  2926. CGMessage1(type_e_class_or_interface_type_expected,right.resultdef.typename);
  2927. resultdef:=booltype;
  2928. end;
  2929. function tisnode.pass_1 : tnode;
  2930. begin
  2931. internalerror(200204254);
  2932. result:=nil;
  2933. end;
  2934. { dummy pass_2, it will never be called, but we need one since }
  2935. { you can't instantiate an abstract class }
  2936. procedure tisnode.pass_generate_code;
  2937. begin
  2938. end;
  2939. {*****************************************************************************
  2940. TASNODE
  2941. *****************************************************************************}
  2942. constructor tasnode.create(l,r : tnode);
  2943. begin
  2944. inherited create(asn,l,r);
  2945. call := nil;
  2946. end;
  2947. destructor tasnode.destroy;
  2948. begin
  2949. call.free;
  2950. inherited destroy;
  2951. end;
  2952. function tasnode.pass_typecheck:tnode;
  2953. var
  2954. hp : tnode;
  2955. begin
  2956. result:=nil;
  2957. typecheckpass(right);
  2958. typecheckpass(left);
  2959. set_varstate(right,vs_read,[vsf_must_be_valid]);
  2960. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2961. if codegenerror then
  2962. exit;
  2963. if (right.resultdef.typ=classrefdef) then
  2964. begin
  2965. { left must be a class }
  2966. if is_class(left.resultdef) then
  2967. begin
  2968. { the operands must be related }
  2969. if (not(tobjectdef(left.resultdef).is_related(
  2970. tobjectdef(tclassrefdef(right.resultdef).pointeddef)))) and
  2971. (not(tobjectdef(tclassrefdef(right.resultdef).pointeddef).is_related(
  2972. tobjectdef(left.resultdef)))) then
  2973. CGMessage2(type_e_classes_not_related,
  2974. FullTypeName(left.resultdef,tclassrefdef(right.resultdef).pointeddef),
  2975. FullTypeName(tclassrefdef(right.resultdef).pointeddef,left.resultdef));
  2976. end
  2977. else
  2978. CGMessage1(type_e_class_type_expected,left.resultdef.typename);
  2979. resultdef:=tclassrefdef(right.resultdef).pointeddef;
  2980. end
  2981. else if is_interface(right.resultdef) then
  2982. begin
  2983. { left is a class }
  2984. if not(is_class(left.resultdef) or
  2985. is_interfacecom(left.resultdef)) then
  2986. CGMessage1(type_e_class_or_cominterface_type_expected,left.resultdef.typename);
  2987. resultdef:=right.resultdef;
  2988. { load the GUID of the interface }
  2989. if (right.nodetype=typen) then
  2990. begin
  2991. if tobjectdef(right.resultdef).objecttype=odt_interfacecorba then
  2992. begin
  2993. if assigned(tobjectdef(right.resultdef).iidstr) then
  2994. begin
  2995. hp:=cstringconstnode.createstr(tobjectdef(right.resultdef).iidstr^);
  2996. tstringconstnode(hp).changestringtype(cshortstringtype);
  2997. right.free;
  2998. right:=hp;
  2999. end
  3000. else
  3001. internalerror(200902081);
  3002. end
  3003. else
  3004. begin
  3005. if assigned(tobjectdef(right.resultdef).iidguid) then
  3006. begin
  3007. if not(oo_has_valid_guid in tobjectdef(right.resultdef).objectoptions) then
  3008. CGMessage1(type_interface_has_no_guid,tobjectdef(right.resultdef).typename);
  3009. hp:=cguidconstnode.create(tobjectdef(right.resultdef).iidguid^);
  3010. right.free;
  3011. right:=hp;
  3012. end
  3013. else
  3014. internalerror(200206282);
  3015. end;
  3016. typecheckpass(right);
  3017. end;
  3018. end
  3019. else
  3020. CGMessage1(type_e_class_or_interface_type_expected,right.resultdef.typename);
  3021. end;
  3022. function tasnode.dogetcopy: tnode;
  3023. begin
  3024. result := inherited dogetcopy;
  3025. if assigned(call) then
  3026. tasnode(result).call := call.getcopy
  3027. else
  3028. tasnode(result).call := nil;
  3029. end;
  3030. function tasnode.docompare(p: tnode): boolean;
  3031. begin
  3032. result:=
  3033. inherited docompare(p) and
  3034. tasnode(p).call.isequal(call);
  3035. end;
  3036. function tasnode.pass_1 : tnode;
  3037. var
  3038. procname: string;
  3039. begin
  3040. result:=nil;
  3041. { Passing a class type to an "as" expression cannot result in a class
  3042. of that type to be constructed.
  3043. }
  3044. include(right.flags,nf_ignore_for_wpo);
  3045. if not assigned(call) then
  3046. begin
  3047. if is_class(left.resultdef) and
  3048. (right.resultdef.typ=classrefdef) then
  3049. call := ccallnode.createinternres('fpc_do_as',
  3050. ccallparanode.create(left,ccallparanode.create(right,nil)),
  3051. resultdef)
  3052. else
  3053. begin
  3054. if is_class(left.resultdef) then
  3055. if is_shortstring(right.resultdef) then
  3056. procname := 'fpc_class_as_corbaintf'
  3057. else
  3058. procname := 'fpc_class_as_intf'
  3059. else
  3060. procname := 'fpc_intf_as';
  3061. call := ccallnode.createintern(procname,
  3062. ccallparanode.create(right,ccallparanode.create(left,nil)));
  3063. call := ctypeconvnode.create_internal(call,resultdef);
  3064. end;
  3065. left := nil;
  3066. right := nil;
  3067. firstpass(call);
  3068. if codegenerror then
  3069. exit;
  3070. expectloc:=call.expectloc;
  3071. end;
  3072. end;
  3073. begin
  3074. ctypeconvnode:=ttypeconvnode;
  3075. casnode:=tasnode;
  3076. cisnode:=tisnode;
  3077. end.