ncnv.pas 127 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445
  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(resultdef) and
  1629. is_class_or_interface(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 (cs_check_object in current_settings.localswitches) and
  1650. not(nf_internal in flags) then
  1651. begin
  1652. { we can translate the typeconvnode to 'as' when
  1653. typecasting to a class or interface }
  1654. { we need to make sure the result can still be
  1655. passed as a var parameter }
  1656. newblock:=internalstatements(newstatement);
  1657. if (valid_for_var(left,false)) then
  1658. begin
  1659. tempnode:=ctempcreatenode.create(voidpointertype,voidpointertype.size,tt_persistent,true);
  1660. addstatement(newstatement,tempnode);
  1661. addstatement(newstatement,cassignmentnode.create(
  1662. ctemprefnode.create(tempnode),
  1663. caddrnode.create_internal(left)));
  1664. left:=ctypeconvnode.create_internal(cderefnode.create(ctemprefnode.create(tempnode)),left.resultdef);
  1665. end
  1666. else
  1667. begin
  1668. tempnode:=ctempcreatenode.create(left.resultdef,left.resultdef.size,tt_persistent,true);
  1669. addstatement(newstatement,tempnode);
  1670. addstatement(newstatement,cassignmentnode.create(
  1671. ctemprefnode.create(tempnode),
  1672. left));
  1673. left:=ctemprefnode.create(tempnode);
  1674. end;
  1675. addstatement(newstatement,casnode.create(left.getcopy,cloadvmtaddrnode.create(ctypenode.create(resultdef))));
  1676. addstatement(newstatement,ctempdeletenode.create_normal_temp(tempnode));
  1677. addstatement(newstatement,ctypeconvnode.create_internal(left,resultdef));
  1678. left:=nil;
  1679. result:=newblock;
  1680. exit;
  1681. end;
  1682. end
  1683. else
  1684. begin
  1685. { only if the same size or formal def, and }
  1686. { don't allow type casting of constants to }
  1687. { structured types }
  1688. if not(
  1689. (left.resultdef.typ=formaldef) or
  1690. (
  1691. not(is_open_array(left.resultdef)) and
  1692. not(is_array_constructor(left.resultdef)) and
  1693. (left.resultdef.size=resultdef.size) and
  1694. { disallow casts of const nodes }
  1695. (not is_constnode(left) or
  1696. { however, there are some exceptions }
  1697. (not(resultdef.typ in [arraydef,recorddef,setdef,stringdef,
  1698. filedef,variantdef,objectdef]) or
  1699. is_class_or_interface(resultdef) or
  1700. { the softfloat code generates casts <const. float> to record }
  1701. (nf_internal in flags)
  1702. ))
  1703. ) or
  1704. (
  1705. is_void(left.resultdef) and
  1706. (left.nodetype=derefn)
  1707. )
  1708. ) then
  1709. CGMessage2(type_e_illegal_type_conversion,left.resultdef.GetTypeName,resultdef.GetTypeName);
  1710. end;
  1711. end
  1712. else
  1713. IncompatibleTypes(left.resultdef,resultdef);
  1714. end;
  1715. else
  1716. internalerror(200211231);
  1717. end;
  1718. end;
  1719. { Give hint or warning for unportable code, exceptions are
  1720. - typecasts from constants
  1721. - void }
  1722. if not(nf_internal in flags) and
  1723. (left.nodetype<>ordconstn) and
  1724. not(is_void(left.resultdef)) and
  1725. (((left.resultdef.typ=orddef) and
  1726. (resultdef.typ in [pointerdef,procvardef,classrefdef])) or
  1727. ((resultdef.typ=orddef) and
  1728. (left.resultdef.typ in [pointerdef,procvardef,classrefdef]))) then
  1729. begin
  1730. {Converting pointers to signed integers is a bad idea. Warn.}
  1731. warn_pointer_to_signed:=(resultdef.typ=orddef) and (Torddef(resultdef).ordtype in [s8bit,s16bit,s32bit,s64bit]);
  1732. { Give a warning when sizes don't match, because then info will be lost }
  1733. if left.resultdef.size=resultdef.size then
  1734. CGMessage(type_h_pointer_to_longint_conv_not_portable)
  1735. else
  1736. CGMessage(type_w_pointer_to_longint_conv_not_portable);
  1737. end;
  1738. result := simplify;
  1739. if assigned(result) then
  1740. exit;
  1741. { now call the resultdef helper to do constant folding }
  1742. result:=typecheck_call_helper(convtype);
  1743. end;
  1744. {$ifndef cpu64bitaddr}
  1745. { checks whether we can safely remove 64 bit typeconversions }
  1746. { in case range and overflow checking are off, and in case }
  1747. { the result of this node tree is downcasted again to a }
  1748. { 8/16/32 bit value afterwards }
  1749. function checkremove64bittypeconvs(n: tnode): boolean;
  1750. var
  1751. gotmuldivmod, gotsint: boolean;
  1752. { checks whether a node is either an u32bit, or originally }
  1753. { was one but was implicitly converted to s64bit }
  1754. function wasoriginallyint32(n: tnode): boolean;
  1755. begin
  1756. if (n.resultdef.typ<>orddef) then
  1757. exit(false);
  1758. if (torddef(n.resultdef).ordtype in [s32bit,u32bit]) then
  1759. begin
  1760. if (torddef(n.resultdef).ordtype=s32bit) then
  1761. gotsint:=true;
  1762. exit(true);
  1763. end;
  1764. if (torddef(n.resultdef).ordtype=s64bit) and
  1765. { nf_explicit is also set for explicitly typecasted }
  1766. { ordconstn's }
  1767. ([nf_internal,nf_explicit]*n.flags=[]) and
  1768. { either a typeconversion node coming from u32bit }
  1769. (((n.nodetype=typeconvn) and
  1770. (ttypeconvnode(n).left.resultdef.typ=orddef) and
  1771. (torddef(ttypeconvnode(n).left.resultdef).ordtype in [s32bit,u32bit])) or
  1772. { or an ordconstnode which was/is a valid cardinal }
  1773. ((n.nodetype=ordconstn) and
  1774. (tordconstnode(n).value>=int64(low(longint))) and
  1775. (tordconstnode(n).value<=high(cardinal)))) then
  1776. begin
  1777. if ((n.nodetype=typeconvn) and
  1778. (torddef(ttypeconvnode(n).left.resultdef).ordtype=s32bit)) or
  1779. ((n.nodetype=ordconstn) and
  1780. (tordconstnode(n).value<0)) then
  1781. gotsint:=true;
  1782. exit(true);
  1783. end;
  1784. result:=false;
  1785. end;
  1786. function docheckremove64bittypeconvs(n: tnode): boolean;
  1787. begin
  1788. result:=false;
  1789. if wasoriginallyint32(n) then
  1790. exit(true);
  1791. case n.nodetype of
  1792. subn,orn,xorn:
  1793. begin
  1794. { nf_internal is set by taddnode.typecheckpass in }
  1795. { case the arguments of this subn were u32bit, but }
  1796. { upcasted to s64bit for calculation correctness }
  1797. { (normally only needed when range checking, but }
  1798. { also done otherwise so there is no difference }
  1799. { in overload choosing etc between $r+ and $r-) }
  1800. if (nf_internal in n.flags) then
  1801. result:=true
  1802. else
  1803. result:=
  1804. docheckremove64bittypeconvs(tbinarynode(n).left) and
  1805. docheckremove64bittypeconvs(tbinarynode(n).right);
  1806. end;
  1807. addn,muln,divn,modn,andn:
  1808. begin
  1809. if n.nodetype in [muln,divn,modn] then
  1810. gotmuldivmod:=true;
  1811. result:=
  1812. docheckremove64bittypeconvs(tbinarynode(n).left) and
  1813. docheckremove64bittypeconvs(tbinarynode(n).right);
  1814. end;
  1815. end;
  1816. end;
  1817. begin { checkremove64bittypeconvs }
  1818. gotmuldivmod:=false;
  1819. gotsint:=false;
  1820. result:=
  1821. docheckremove64bittypeconvs(n) and
  1822. not(gotmuldivmod and gotsint);
  1823. end;
  1824. procedure doremove64bittypeconvs(var n: tnode; todef: tdef);
  1825. begin
  1826. case n.nodetype of
  1827. subn,addn,muln,divn,modn,xorn,andn,orn:
  1828. begin
  1829. exclude(n.flags,nf_internal);
  1830. if is_signed(n.resultdef) then
  1831. begin
  1832. doremove64bittypeconvs(tbinarynode(n).left,s32inttype);
  1833. doremove64bittypeconvs(tbinarynode(n).right,s32inttype);
  1834. n.resultdef:=s32inttype
  1835. end
  1836. else
  1837. begin
  1838. doremove64bittypeconvs(tbinarynode(n).left,u32inttype);
  1839. doremove64bittypeconvs(tbinarynode(n).right,u32inttype);
  1840. n.resultdef:=u32inttype
  1841. end;
  1842. end;
  1843. ordconstn:
  1844. inserttypeconv_internal(n,todef);
  1845. typeconvn:
  1846. n.resultdef:=todef;
  1847. end;
  1848. end;
  1849. {$endif not cpu64bitaddr}
  1850. function ttypeconvnode.simplify: tnode;
  1851. var
  1852. hp: tnode;
  1853. begin
  1854. result := nil;
  1855. { Constant folding and other node transitions to
  1856. remove the typeconv node }
  1857. case left.nodetype of
  1858. stringconstn :
  1859. if (convtype=tc_string_2_string) and
  1860. (
  1861. ((not is_widechararray(left.resultdef) and
  1862. not is_wide_or_unicode_string(left.resultdef)) or
  1863. (tstringdef(resultdef).stringtype in [st_widestring,st_unicodestring]) or
  1864. { non-ascii chars would be replaced with '?' -> loses info }
  1865. not hasnonasciichars(pcompilerwidestring(tstringconstnode(left).value_str)))
  1866. ) then
  1867. begin
  1868. tstringconstnode(left).changestringtype(resultdef);
  1869. result:=left;
  1870. left:=nil;
  1871. exit;
  1872. end;
  1873. realconstn :
  1874. begin
  1875. if (convtype = tc_real_2_currency) then
  1876. result := typecheck_real_to_currency
  1877. else if (convtype = tc_real_2_real) then
  1878. result := typecheck_real_to_real
  1879. else
  1880. exit;
  1881. if not(assigned(result)) then
  1882. begin
  1883. result := left;
  1884. left := nil;
  1885. end;
  1886. if (result.nodetype = realconstn) then
  1887. begin
  1888. hp:=result;
  1889. result:=crealconstnode.create(trealconstnode(hp).value_real,resultdef);
  1890. if ([nf_explicit,nf_internal] * flags <> []) then
  1891. include(result.flags, nf_explicit);
  1892. hp.free;
  1893. end;
  1894. end;
  1895. niln :
  1896. begin
  1897. { nil to ordinal node }
  1898. if (resultdef.typ=orddef) then
  1899. begin
  1900. hp:=cordconstnode.create(0,resultdef,true);
  1901. if ([nf_explicit,nf_internal] * flags <> []) then
  1902. include(hp.flags, nf_explicit);
  1903. result:=hp;
  1904. exit;
  1905. end
  1906. else
  1907. { fold nil to any pointer type }
  1908. if (resultdef.typ=pointerdef) then
  1909. begin
  1910. hp:=cnilnode.create;
  1911. hp.resultdef:=resultdef;
  1912. if ([nf_explicit,nf_internal] * flags <> []) then
  1913. include(hp.flags, nf_explicit);
  1914. result:=hp;
  1915. exit;
  1916. end
  1917. else
  1918. { remove typeconv after niln, but not when the result is a
  1919. methodpointer. The typeconv of the methodpointer will then
  1920. take care of updateing size of niln to OS_64 }
  1921. if not((resultdef.typ=procvardef) and
  1922. (po_methodpointer in tprocvardef(resultdef).procoptions)) then
  1923. begin
  1924. left.resultdef:=resultdef;
  1925. if ([nf_explicit,nf_internal] * flags <> []) then
  1926. include(left.flags, nf_explicit);
  1927. result:=left;
  1928. left:=nil;
  1929. exit;
  1930. end;
  1931. end;
  1932. ordconstn :
  1933. begin
  1934. { ordinal contants can be directly converted }
  1935. { but not char to char because it is a widechar to char or via versa }
  1936. { which needs extra code to do the code page transistion }
  1937. { constant ordinal to pointer }
  1938. if (resultdef.typ=pointerdef) and
  1939. (convtype<>tc_cchar_2_pchar) then
  1940. begin
  1941. hp:=cpointerconstnode.create(TConstPtrUInt(tordconstnode(left).value.uvalue),resultdef);
  1942. if ([nf_explicit,nf_internal] * flags <> []) then
  1943. include(hp.flags, nf_explicit);
  1944. result:=hp;
  1945. exit;
  1946. end
  1947. else if is_ordinal(resultdef) and
  1948. not(convtype=tc_char_2_char) then
  1949. begin
  1950. { replace the resultdef and recheck the range }
  1951. if ([nf_explicit,nf_internal] * flags <> []) then
  1952. include(left.flags, nf_explicit)
  1953. else
  1954. { no longer an ordconst with an explicit typecast }
  1955. exclude(left.flags, nf_explicit);
  1956. { when converting from one boolean type to another, force }
  1957. { booleans to 0/1, and byte/word/long/qwordbool to 0/-1 }
  1958. { (Delphi-compatibile) }
  1959. if is_boolean(left.resultdef) and
  1960. is_boolean(resultdef) and
  1961. (is_cbool(left.resultdef) or
  1962. is_cbool(resultdef)) then
  1963. begin
  1964. if is_pasbool(resultdef) then
  1965. tordconstnode(left).value:=ord(tordconstnode(left).value<>0)
  1966. else
  1967. {$ifdef VER2_2}
  1968. tordconstnode(left).value:=ord(tordconstnode(left).value<>0);
  1969. tordconstnode(left).value:=-tordconstnode(left).value;
  1970. {$else}
  1971. tordconstnode(left).value:=-ord(tordconstnode(left).value<>0);
  1972. {$endif VER2_2}
  1973. end
  1974. else
  1975. testrange(resultdef,tordconstnode(left).value,(nf_explicit in flags));
  1976. left.resultdef:=resultdef;
  1977. result:=left;
  1978. left:=nil;
  1979. exit;
  1980. end;
  1981. end;
  1982. pointerconstn :
  1983. begin
  1984. { pointerconstn to any pointer is folded too }
  1985. if (resultdef.typ=pointerdef) then
  1986. begin
  1987. left.resultdef:=resultdef;
  1988. if ([nf_explicit,nf_internal] * flags <> []) then
  1989. include(left.flags, nf_explicit)
  1990. else
  1991. { no longer an ordconst with an explicit typecast }
  1992. exclude(left.flags, nf_explicit);
  1993. result:=left;
  1994. left:=nil;
  1995. exit;
  1996. end
  1997. { constant pointer to ordinal }
  1998. else if is_ordinal(resultdef) then
  1999. begin
  2000. hp:=cordconstnode.create(TConstExprInt(tpointerconstnode(left).value),
  2001. resultdef,not(nf_explicit in flags));
  2002. if ([nf_explicit,nf_internal] * flags <> []) then
  2003. include(hp.flags, nf_explicit);
  2004. result:=hp;
  2005. exit;
  2006. end;
  2007. end;
  2008. end;
  2009. {$ifndef cpu64bitaddr}
  2010. { must be done before code below, because we need the
  2011. typeconversions for ordconstn's as well }
  2012. case convtype of
  2013. tc_int_2_int:
  2014. begin
  2015. if (localswitches * [cs_check_range,cs_check_overflow] = []) and
  2016. (resultdef.typ in [pointerdef,orddef,enumdef]) and
  2017. (resultdef.size <= 4) and
  2018. is_64bitint(left.resultdef) and
  2019. (left.nodetype in [subn,addn,muln,divn,modn,xorn,andn,orn]) and
  2020. checkremove64bittypeconvs(left) then
  2021. begin
  2022. { avoid unnecessary widening of intermediary calculations }
  2023. { to 64 bit }
  2024. doremove64bittypeconvs(left,generrordef);
  2025. end;
  2026. end;
  2027. end;
  2028. {$endif not cpu64bitaddr}
  2029. end;
  2030. procedure Ttypeconvnode.mark_write;
  2031. begin
  2032. left.mark_write;
  2033. end;
  2034. function ttypeconvnode.first_cord_to_pointer : tnode;
  2035. begin
  2036. result:=nil;
  2037. internalerror(200104043);
  2038. end;
  2039. function ttypeconvnode.first_int_to_int : tnode;
  2040. begin
  2041. first_int_to_int:=nil;
  2042. expectloc:=left.expectloc;
  2043. if not is_void(left.resultdef) then
  2044. begin
  2045. if (left.expectloc<>LOC_REGISTER) and
  2046. (resultdef.size>left.resultdef.size) then
  2047. expectloc:=LOC_REGISTER
  2048. else
  2049. if (left.expectloc=LOC_CREGISTER) and
  2050. (resultdef.size<left.resultdef.size) then
  2051. expectloc:=LOC_REGISTER;
  2052. end;
  2053. end;
  2054. function ttypeconvnode.first_cstring_to_pchar : tnode;
  2055. begin
  2056. result:=nil;
  2057. expectloc:=LOC_REGISTER;
  2058. end;
  2059. function ttypeconvnode.first_cstring_to_int : tnode;
  2060. begin
  2061. result:=nil;
  2062. internalerror(200510014);
  2063. end;
  2064. function ttypeconvnode.first_string_to_chararray : tnode;
  2065. begin
  2066. first_string_to_chararray:=nil;
  2067. expectloc:=left.expectloc;
  2068. end;
  2069. function ttypeconvnode.first_char_to_string : tnode;
  2070. begin
  2071. first_char_to_string:=nil;
  2072. expectloc:=LOC_REFERENCE;
  2073. end;
  2074. function ttypeconvnode.first_nothing : tnode;
  2075. begin
  2076. first_nothing:=nil;
  2077. end;
  2078. function ttypeconvnode.first_array_to_pointer : tnode;
  2079. begin
  2080. first_array_to_pointer:=nil;
  2081. expectloc:=LOC_REGISTER;
  2082. end;
  2083. function ttypeconvnode.first_int_to_real: tnode;
  2084. var
  2085. fname: string[32];
  2086. begin
  2087. if target_info.system in system_wince then
  2088. begin
  2089. { converting a 64bit integer to a float requires a helper }
  2090. if is_64bitint(left.resultdef) or
  2091. is_currency(left.resultdef) then
  2092. begin
  2093. { hack to avoid double division by 10000, as it's
  2094. already done by typecheckpass.resultdef_int_to_real }
  2095. if is_currency(left.resultdef) then
  2096. left.resultdef := s64inttype;
  2097. if is_signed(left.resultdef) then
  2098. fname:='I64TO'
  2099. else
  2100. fname:='UI64TO';
  2101. end
  2102. else
  2103. { other integers are supposed to be 32 bit }
  2104. begin
  2105. if is_signed(left.resultdef) then
  2106. fname:='ITO'
  2107. else
  2108. fname:='UTO';
  2109. firstpass(left);
  2110. end;
  2111. if tfloatdef(resultdef).floattype=s64real then
  2112. fname:=fname+'D'
  2113. else
  2114. fname:=fname+'S';
  2115. result:=ccallnode.createintern(fname,ccallparanode.create(
  2116. left,nil));
  2117. left:=nil;
  2118. firstpass(result);
  2119. exit;
  2120. end
  2121. else
  2122. begin
  2123. { converting a 64bit integer to a float requires a helper }
  2124. if is_64bitint(left.resultdef) or
  2125. is_currency(left.resultdef) then
  2126. begin
  2127. { hack to avoid double division by 10000, as it's
  2128. already done by typecheckpass.resultdef_int_to_real }
  2129. if is_currency(left.resultdef) then
  2130. left.resultdef := s64inttype;
  2131. if is_signed(left.resultdef) then
  2132. fname:='int64_to_'
  2133. else
  2134. { we can't do better currently }
  2135. fname:='int64_to_';
  2136. end
  2137. else
  2138. { other integers are supposed to be 32 bit }
  2139. begin
  2140. if is_signed(left.resultdef) then
  2141. fname:='int32_to_'
  2142. else
  2143. fname:='int64_to_';
  2144. firstpass(left);
  2145. end;
  2146. if tfloatdef(resultdef).floattype=s64real then
  2147. fname:=fname+'float64'
  2148. else
  2149. fname:=fname+'float32';
  2150. result:=ctypeconvnode.create_internal(ccallnode.createintern(fname,ccallparanode.create(
  2151. left,nil)),resultdef);
  2152. left:=nil;
  2153. firstpass(result);
  2154. exit;
  2155. end;
  2156. end;
  2157. function ttypeconvnode.first_real_to_real : tnode;
  2158. begin
  2159. {$ifdef cpufpemu}
  2160. if cs_fp_emulation in current_settings.moduleswitches then
  2161. begin
  2162. if target_info.system in system_wince then
  2163. begin
  2164. case tfloatdef(left.resultdef).floattype of
  2165. s32real:
  2166. case tfloatdef(resultdef).floattype of
  2167. s64real:
  2168. result:=ccallnode.createintern('STOD',ccallparanode.create(left,nil));
  2169. s32real:
  2170. begin
  2171. result:=left;
  2172. left:=nil;
  2173. end;
  2174. else
  2175. internalerror(2005082704);
  2176. end;
  2177. s64real:
  2178. case tfloatdef(resultdef).floattype of
  2179. s32real:
  2180. result:=ccallnode.createintern('DTOS',ccallparanode.create(left,nil));
  2181. s64real:
  2182. begin
  2183. result:=left;
  2184. left:=nil;
  2185. end;
  2186. else
  2187. internalerror(2005082703);
  2188. end;
  2189. else
  2190. internalerror(2005082702);
  2191. end;
  2192. left:=nil;
  2193. firstpass(result);
  2194. exit;
  2195. end
  2196. else
  2197. begin
  2198. case tfloatdef(left.resultdef).floattype of
  2199. s32real:
  2200. case tfloatdef(resultdef).floattype of
  2201. s64real:
  2202. result:=ctypeconvnode.create_explicit(ccallnode.createintern('float32_to_float64',ccallparanode.create(
  2203. ctypeconvnode.create_internal(left,search_system_type('FLOAT32REC').typedef),nil)),resultdef);
  2204. s32real:
  2205. begin
  2206. result:=left;
  2207. left:=nil;
  2208. end;
  2209. else
  2210. internalerror(200610151);
  2211. end;
  2212. s64real:
  2213. case tfloatdef(resultdef).floattype of
  2214. s32real:
  2215. result:=ctypeconvnode.create_explicit(ccallnode.createintern('float64_to_float32',ccallparanode.create(
  2216. ctypeconvnode.create_internal(left,search_system_type('FLOAT64').typedef),nil)),resultdef);
  2217. s64real:
  2218. begin
  2219. result:=left;
  2220. left:=nil;
  2221. end;
  2222. else
  2223. internalerror(200610152);
  2224. end;
  2225. else
  2226. internalerror(200610153);
  2227. end;
  2228. left:=nil;
  2229. firstpass(result);
  2230. exit;
  2231. end;
  2232. end
  2233. else
  2234. {$endif cpufpemu}
  2235. begin
  2236. first_real_to_real:=nil;
  2237. expectloc:=LOC_FPUREGISTER;
  2238. end;
  2239. end;
  2240. function ttypeconvnode.first_pointer_to_array : tnode;
  2241. begin
  2242. first_pointer_to_array:=nil;
  2243. expectloc:=LOC_REFERENCE;
  2244. end;
  2245. function ttypeconvnode.first_cchar_to_pchar : tnode;
  2246. begin
  2247. first_cchar_to_pchar:=nil;
  2248. internalerror(200104021);
  2249. end;
  2250. function ttypeconvnode.first_bool_to_int : tnode;
  2251. begin
  2252. first_bool_to_int:=nil;
  2253. { byte(boolean) or word(wordbool) or longint(longbool) must
  2254. be accepted for var parameters }
  2255. if (nf_explicit in flags) and
  2256. (left.resultdef.size=resultdef.size) and
  2257. (left.expectloc in [LOC_REFERENCE,LOC_CREFERENCE,LOC_CREGISTER]) then
  2258. exit;
  2259. { when converting to 64bit, first convert to a 32bit int and then }
  2260. { convert to a 64bit int (only necessary for 32bit processors) (JM) }
  2261. if resultdef.size > sizeof(aint) then
  2262. begin
  2263. result := ctypeconvnode.create_internal(left,s32inttype);
  2264. result := ctypeconvnode.create(result,resultdef);
  2265. left := nil;
  2266. firstpass(result);
  2267. exit;
  2268. end;
  2269. expectloc:=LOC_REGISTER;
  2270. end;
  2271. function ttypeconvnode.first_int_to_bool : tnode;
  2272. begin
  2273. first_int_to_bool:=nil;
  2274. { byte(boolean) or word(wordbool) or longint(longbool) must
  2275. be accepted for var parameters }
  2276. if (nf_explicit in flags) and
  2277. (left.resultdef.size=resultdef.size) and
  2278. (left.expectloc in [LOC_REFERENCE,LOC_CREFERENCE,LOC_CREGISTER]) then
  2279. exit;
  2280. { when converting 64bit int to C-ctyle boolean, first convert to an int32 and then }
  2281. { convert to a boolean (only necessary for 32bit processors) }
  2282. if (left.resultdef.size > sizeof(aint)) and (left.resultdef.size<>resultdef.size)
  2283. and is_cbool(resultdef) then
  2284. begin
  2285. left:=ctypeconvnode.create_internal(left,s32inttype);
  2286. firstpass(left);
  2287. exit;
  2288. end;
  2289. expectloc:=LOC_REGISTER;
  2290. end;
  2291. function ttypeconvnode.first_bool_to_bool : tnode;
  2292. begin
  2293. first_bool_to_bool:=nil;
  2294. if (left.expectloc in [LOC_FLAGS,LOC_JUMP]) then
  2295. expectloc := left.expectloc
  2296. else
  2297. expectloc:=LOC_REGISTER;
  2298. end;
  2299. function ttypeconvnode.first_char_to_char : tnode;
  2300. var
  2301. fname: string[18];
  2302. begin
  2303. if (torddef(resultdef).ordtype=uchar) and
  2304. (torddef(left.resultdef).ordtype=uwidechar) then
  2305. fname := 'fpc_uchar_to_char'
  2306. else if (torddef(resultdef).ordtype=uwidechar) and
  2307. (torddef(left.resultdef).ordtype=uchar) then
  2308. fname := 'fpc_char_to_uchar'
  2309. else
  2310. internalerror(2007081201);
  2311. result := ccallnode.createintern(fname,ccallparanode.create(left,nil));
  2312. left:=nil;
  2313. firstpass(result);
  2314. end;
  2315. function ttypeconvnode.first_proc_to_procvar : tnode;
  2316. begin
  2317. first_proc_to_procvar:=nil;
  2318. { if we take the address of a nested function, it'll }
  2319. { probably be used in a foreach() construct and then }
  2320. { the parent needs a stackframe }
  2321. if (tprocdef(left.resultdef).parast.symtablelevel>=normal_function_level) then
  2322. include(current_procinfo.flags,pi_needs_stackframe);
  2323. if tabstractprocdef(resultdef).is_addressonly then
  2324. expectloc:=LOC_REGISTER
  2325. else
  2326. begin
  2327. if not(left.expectloc in [LOC_CREFERENCE,LOC_REFERENCE]) then
  2328. CGMessage(parser_e_illegal_expression);
  2329. expectloc:=left.expectloc;
  2330. end;
  2331. end;
  2332. function ttypeconvnode.first_nil_to_methodprocvar : tnode;
  2333. begin
  2334. first_nil_to_methodprocvar:=nil;
  2335. expectloc:=LOC_REFERENCE;
  2336. end;
  2337. function ttypeconvnode.first_set_to_set : tnode;
  2338. var
  2339. newstatement : tstatementnode;
  2340. temp : ttempcreatenode;
  2341. begin
  2342. { in theory, we should do range checking here,
  2343. but Delphi doesn't do it either (FK) }
  2344. if left.nodetype=setconstn then
  2345. begin
  2346. left.resultdef:=resultdef;
  2347. result:=left;
  2348. left:=nil;
  2349. end
  2350. { equal sets for the code generator? }
  2351. else if (left.resultdef.size=resultdef.size) and
  2352. (tsetdef(left.resultdef).setbase=tsetdef(resultdef).setbase) then
  2353. { TODO: This causes wrong (but Delphi-compatible) results for disjoint subsets}
  2354. { e.g., this prints true because of this:
  2355. var
  2356. sa: set of 1..2;
  2357. sb: set of 5..6;
  2358. b: byte;
  2359. begin
  2360. b:=1;
  2361. sa:=[1..2];
  2362. sb:=sa;
  2363. writeln(b in sb);
  2364. end.
  2365. }
  2366. begin
  2367. result:=left;
  2368. left:=nil;
  2369. end
  2370. else
  2371. begin
  2372. result:=internalstatements(newstatement);
  2373. { in case left is a smallset expression, it can be an addn or so. }
  2374. { fpc_varset_load expects a formal const parameter, which doesn't }
  2375. { accept set addn's -> assign to a temp first and pass the temp }
  2376. if not(left.expectloc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  2377. begin
  2378. temp:=ctempcreatenode.create(left.resultdef,left.resultdef.size,tt_persistent,false);
  2379. addstatement(newstatement,temp);
  2380. { temp := left }
  2381. addstatement(newstatement,cassignmentnode.create(
  2382. ctemprefnode.create(temp),left));
  2383. addstatement(newstatement,ctempdeletenode.create_normal_temp(temp));
  2384. addstatement(newstatement,ctemprefnode.create(temp));
  2385. left:=result;
  2386. firstpass(left);
  2387. { recreate the result's internalstatements list }
  2388. result:=internalstatements(newstatement);
  2389. end;
  2390. { create temp for result }
  2391. temp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,true);
  2392. addstatement(newstatement,temp);
  2393. addstatement(newstatement,ccallnode.createintern('fpc_varset_load',
  2394. ccallparanode.create(cordconstnode.create(tsetdef(left.resultdef).setbase div 8 - tsetdef(resultdef).setbase div 8,sinttype,false),
  2395. ccallparanode.create(cordconstnode.create(resultdef.size,sinttype,false),
  2396. ccallparanode.create(ctemprefnode.create(temp),
  2397. ccallparanode.create(cordconstnode.create(left.resultdef.size,sinttype,false),
  2398. ccallparanode.create(left,nil))))))
  2399. );
  2400. addstatement(newstatement,ctempdeletenode.create_normal_temp(temp));
  2401. addstatement(newstatement,ctemprefnode.create(temp));
  2402. left:=nil;
  2403. end;
  2404. end;
  2405. function ttypeconvnode.first_ansistring_to_pchar : tnode;
  2406. begin
  2407. first_ansistring_to_pchar:=nil;
  2408. expectloc:=LOC_REGISTER;
  2409. end;
  2410. function ttypeconvnode.first_arrayconstructor_to_set : tnode;
  2411. begin
  2412. first_arrayconstructor_to_set:=nil;
  2413. internalerror(200104022);
  2414. end;
  2415. function ttypeconvnode.first_class_to_intf : tnode;
  2416. var
  2417. hd : tobjectdef;
  2418. ImplIntf : TImplementedInterface;
  2419. begin
  2420. result:=nil;
  2421. expectloc:=LOC_REGISTER;
  2422. hd:=tobjectdef(left.resultdef);
  2423. while assigned(hd) do
  2424. begin
  2425. ImplIntf:=hd.find_implemented_interface(tobjectdef(resultdef));
  2426. if assigned(ImplIntf) then
  2427. begin
  2428. case ImplIntf.IType of
  2429. etStandard:
  2430. { handle in pass 2 }
  2431. ;
  2432. etFieldValue:
  2433. if is_interface(tobjectdef(resultdef)) then
  2434. begin
  2435. result:=left;
  2436. propaccesslist_to_node(result,tpropertysym(implintf.implementsgetter).owner,tpropertysym(implintf.implementsgetter).propaccesslist[palt_read]);
  2437. left:=nil;
  2438. end
  2439. else
  2440. begin
  2441. internalerror(200802213);
  2442. end;
  2443. etStaticMethodResult,
  2444. etVirtualMethodResult:
  2445. if is_interface(tobjectdef(resultdef)) then
  2446. begin
  2447. { constructor create(l:tnode; v : tprocsym;st : TSymtable; mp: tnode; callflags:tcallnodeflags); }
  2448. result:=ccallnode.create(nil,tprocsym(tpropertysym(implintf.implementsgetter).propaccesslist[palt_read].firstsym^.sym),
  2449. tprocsym(tpropertysym(implintf.implementsgetter).propaccesslist[palt_read].firstsym^.sym).owner,
  2450. left,[]);
  2451. addsymref(tpropertysym(implintf.implementsgetter).propaccesslist[palt_read].firstsym^.sym);
  2452. left:=nil;
  2453. end
  2454. else if is_class(tobjectdef(resultdef)) then
  2455. begin
  2456. internalerror(200802211);
  2457. end
  2458. else
  2459. internalerror(200802231);
  2460. else
  2461. internalerror(200802165);
  2462. end;
  2463. break;
  2464. end;
  2465. hd:=hd.childof;
  2466. end;
  2467. if hd=nil then
  2468. internalerror(200802164);
  2469. end;
  2470. function ttypeconvnode.first_string_to_string : tnode;
  2471. var
  2472. procname: string[31];
  2473. newblock : tblocknode;
  2474. newstat : tstatementnode;
  2475. restemp : ttempcreatenode;
  2476. begin
  2477. { get the correct procedure name }
  2478. procname := 'fpc_'+tstringdef(left.resultdef).stringtypname+
  2479. '_to_'+tstringdef(resultdef).stringtypname;
  2480. if tstringdef(resultdef).stringtype=st_shortstring then
  2481. begin
  2482. newblock:=internalstatements(newstat);
  2483. restemp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,false);
  2484. addstatement(newstat,restemp);
  2485. addstatement(newstat,ccallnode.createintern(procname,ccallparanode.create(left,ccallparanode.create(
  2486. ctemprefnode.create(restemp),nil))));
  2487. addstatement(newstat,ctempdeletenode.create_normal_temp(restemp));
  2488. addstatement(newstat,ctemprefnode.create(restemp));
  2489. result:=newblock;
  2490. end
  2491. else
  2492. result := ccallnode.createinternres(procname,ccallparanode.create(left,nil),resultdef);
  2493. left:=nil;
  2494. end;
  2495. function ttypeconvnode._first_int_to_int : tnode;
  2496. begin
  2497. result:=first_int_to_int;
  2498. end;
  2499. function ttypeconvnode._first_cstring_to_pchar : tnode;
  2500. begin
  2501. result:=first_cstring_to_pchar;
  2502. end;
  2503. function ttypeconvnode._first_cstring_to_int : tnode;
  2504. begin
  2505. result:=first_cstring_to_int;
  2506. end;
  2507. function ttypeconvnode._first_string_to_chararray : tnode;
  2508. begin
  2509. result:=first_string_to_chararray;
  2510. end;
  2511. function ttypeconvnode._first_char_to_string : tnode;
  2512. begin
  2513. result:=first_char_to_string;
  2514. end;
  2515. function ttypeconvnode._first_nothing : tnode;
  2516. begin
  2517. result:=first_nothing;
  2518. end;
  2519. function ttypeconvnode._first_array_to_pointer : tnode;
  2520. begin
  2521. result:=first_array_to_pointer;
  2522. end;
  2523. function ttypeconvnode._first_int_to_real : tnode;
  2524. begin
  2525. result:=first_int_to_real;
  2526. end;
  2527. function ttypeconvnode._first_real_to_real : tnode;
  2528. begin
  2529. result:=first_real_to_real;
  2530. end;
  2531. function ttypeconvnode._first_pointer_to_array : tnode;
  2532. begin
  2533. result:=first_pointer_to_array;
  2534. end;
  2535. function ttypeconvnode._first_cchar_to_pchar : tnode;
  2536. begin
  2537. result:=first_cchar_to_pchar;
  2538. end;
  2539. function ttypeconvnode._first_bool_to_int : tnode;
  2540. begin
  2541. result:=first_bool_to_int;
  2542. end;
  2543. function ttypeconvnode._first_int_to_bool : tnode;
  2544. begin
  2545. result:=first_int_to_bool;
  2546. end;
  2547. function ttypeconvnode._first_bool_to_bool : tnode;
  2548. begin
  2549. result:=first_bool_to_bool;
  2550. end;
  2551. function ttypeconvnode._first_proc_to_procvar : tnode;
  2552. begin
  2553. result:=first_proc_to_procvar;
  2554. end;
  2555. function ttypeconvnode._first_nil_to_methodprocvar : tnode;
  2556. begin
  2557. result:=first_nil_to_methodprocvar;
  2558. end;
  2559. function ttypeconvnode._first_set_to_set : tnode;
  2560. begin
  2561. result:=first_set_to_set;
  2562. end;
  2563. function ttypeconvnode._first_cord_to_pointer : tnode;
  2564. begin
  2565. result:=first_cord_to_pointer;
  2566. end;
  2567. function ttypeconvnode._first_ansistring_to_pchar : tnode;
  2568. begin
  2569. result:=first_ansistring_to_pchar;
  2570. end;
  2571. function ttypeconvnode._first_arrayconstructor_to_set : tnode;
  2572. begin
  2573. result:=first_arrayconstructor_to_set;
  2574. end;
  2575. function ttypeconvnode._first_class_to_intf : tnode;
  2576. begin
  2577. result:=first_class_to_intf;
  2578. end;
  2579. function ttypeconvnode._first_char_to_char : tnode;
  2580. begin
  2581. result:=first_char_to_char;
  2582. end;
  2583. function ttypeconvnode._first_string_to_string : tnode;
  2584. begin
  2585. result:=first_string_to_string;
  2586. end;
  2587. function ttypeconvnode.first_call_helper(c : tconverttype) : tnode;
  2588. const
  2589. firstconvert : array[tconverttype] of pointer = (
  2590. nil, { none }
  2591. @ttypeconvnode._first_nothing, {equal}
  2592. @ttypeconvnode._first_nothing, {not_possible}
  2593. @ttypeconvnode._first_string_to_string,
  2594. @ttypeconvnode._first_char_to_string,
  2595. @ttypeconvnode._first_nothing, { char_2_chararray, needs nothing extra }
  2596. nil, { removed in typecheck_chararray_to_string }
  2597. @ttypeconvnode._first_cchar_to_pchar,
  2598. @ttypeconvnode._first_cstring_to_pchar,
  2599. @ttypeconvnode._first_cstring_to_int,
  2600. @ttypeconvnode._first_ansistring_to_pchar,
  2601. @ttypeconvnode._first_string_to_chararray,
  2602. nil, { removed in typecheck_chararray_to_string }
  2603. @ttypeconvnode._first_array_to_pointer,
  2604. @ttypeconvnode._first_pointer_to_array,
  2605. @ttypeconvnode._first_int_to_int,
  2606. @ttypeconvnode._first_int_to_bool,
  2607. @ttypeconvnode._first_bool_to_bool,
  2608. @ttypeconvnode._first_bool_to_int,
  2609. @ttypeconvnode._first_real_to_real,
  2610. @ttypeconvnode._first_int_to_real,
  2611. nil, { removed in typecheck_real_to_currency }
  2612. @ttypeconvnode._first_proc_to_procvar,
  2613. @ttypeconvnode._first_nil_to_methodprocvar,
  2614. @ttypeconvnode._first_arrayconstructor_to_set,
  2615. @ttypeconvnode._first_set_to_set,
  2616. @ttypeconvnode._first_cord_to_pointer,
  2617. @ttypeconvnode._first_nothing,
  2618. @ttypeconvnode._first_nothing,
  2619. @ttypeconvnode._first_class_to_intf,
  2620. @ttypeconvnode._first_char_to_char,
  2621. @ttypeconvnode._first_nothing,
  2622. @ttypeconvnode._first_nothing,
  2623. nil,
  2624. nil,
  2625. nil,
  2626. nil,
  2627. nil,
  2628. nil,
  2629. nil
  2630. );
  2631. type
  2632. tprocedureofobject = function : tnode of object;
  2633. var
  2634. r : packed record
  2635. proc : pointer;
  2636. obj : pointer;
  2637. end;
  2638. begin
  2639. { this is a little bit dirty but it works }
  2640. { and should be quite portable too }
  2641. r.proc:=firstconvert[c];
  2642. r.obj:=self;
  2643. if not assigned(r.proc) then
  2644. internalerror(200312081);
  2645. first_call_helper:=tprocedureofobject(r)()
  2646. end;
  2647. function ttypeconvnode.pass_1 : tnode;
  2648. begin
  2649. if warn_pointer_to_signed then
  2650. cgmessage(type_w_pointer_to_signed);
  2651. result:=nil;
  2652. firstpass(left);
  2653. if codegenerror then
  2654. exit;
  2655. expectloc:=left.expectloc;
  2656. result:=first_call_helper(convtype);
  2657. end;
  2658. function ttypeconvnode.retains_value_location:boolean;
  2659. begin
  2660. result:=(convtype=tc_equal) or
  2661. { typecasting from void is always allowed }
  2662. is_void(left.resultdef) or
  2663. (left.resultdef.typ=formaldef) or
  2664. { int 2 int with same size reuses same location, or for
  2665. tp7 mode also allow size < orignal size }
  2666. (
  2667. (convtype=tc_int_2_int) and
  2668. (
  2669. (resultdef.size=left.resultdef.size) or
  2670. ((m_tp7 in current_settings.modeswitches) and
  2671. (resultdef.size<left.resultdef.size))
  2672. )
  2673. ) or
  2674. { int 2 bool/bool 2 int, explicit typecast, see also nx86cnv }
  2675. ((convtype in [tc_int_2_bool,tc_bool_2_int,tc_bool_2_bool]) and
  2676. (nf_explicit in flags) and
  2677. (resultdef.size=left.resultdef.size));
  2678. end;
  2679. function ttypeconvnode.assign_allowed:boolean;
  2680. begin
  2681. result:=retains_value_location;
  2682. { When using only a part of the value it can't be in a register since
  2683. that will load the value in a new register first }
  2684. { the same goes for changing the sign of equal-sized values which
  2685. are smaller than an entire register }
  2686. if result and
  2687. (resultdef.size<left.resultdef.size) or
  2688. ((resultdef.size=left.resultdef.size) and
  2689. (left.resultdef.size<sizeof(aint)) and
  2690. (is_signed(resultdef) xor is_signed(left.resultdef))) then
  2691. make_not_regable(left,[ra_addr_regable]);
  2692. end;
  2693. function ttypeconvnode.docompare(p: tnode) : boolean;
  2694. begin
  2695. docompare :=
  2696. inherited docompare(p) and
  2697. (convtype = ttypeconvnode(p).convtype);
  2698. end;
  2699. procedure ttypeconvnode._second_int_to_int;
  2700. begin
  2701. second_int_to_int;
  2702. end;
  2703. procedure ttypeconvnode._second_string_to_string;
  2704. begin
  2705. second_string_to_string;
  2706. end;
  2707. procedure ttypeconvnode._second_cstring_to_pchar;
  2708. begin
  2709. second_cstring_to_pchar;
  2710. end;
  2711. procedure ttypeconvnode._second_cstring_to_int;
  2712. begin
  2713. second_cstring_to_int;
  2714. end;
  2715. procedure ttypeconvnode._second_string_to_chararray;
  2716. begin
  2717. second_string_to_chararray;
  2718. end;
  2719. procedure ttypeconvnode._second_array_to_pointer;
  2720. begin
  2721. second_array_to_pointer;
  2722. end;
  2723. procedure ttypeconvnode._second_pointer_to_array;
  2724. begin
  2725. second_pointer_to_array;
  2726. end;
  2727. procedure ttypeconvnode._second_chararray_to_string;
  2728. begin
  2729. second_chararray_to_string;
  2730. end;
  2731. procedure ttypeconvnode._second_char_to_string;
  2732. begin
  2733. second_char_to_string;
  2734. end;
  2735. procedure ttypeconvnode._second_int_to_real;
  2736. begin
  2737. second_int_to_real;
  2738. end;
  2739. procedure ttypeconvnode._second_real_to_real;
  2740. begin
  2741. second_real_to_real;
  2742. end;
  2743. procedure ttypeconvnode._second_cord_to_pointer;
  2744. begin
  2745. second_cord_to_pointer;
  2746. end;
  2747. procedure ttypeconvnode._second_proc_to_procvar;
  2748. begin
  2749. second_proc_to_procvar;
  2750. end;
  2751. procedure ttypeconvnode._second_nil_to_methodprocvar;
  2752. begin
  2753. second_nil_to_methodprocvar;
  2754. end;
  2755. procedure ttypeconvnode._second_bool_to_int;
  2756. begin
  2757. second_bool_to_int;
  2758. end;
  2759. procedure ttypeconvnode._second_int_to_bool;
  2760. begin
  2761. second_int_to_bool;
  2762. end;
  2763. procedure ttypeconvnode._second_bool_to_bool;
  2764. begin
  2765. second_bool_to_bool;
  2766. end;
  2767. procedure ttypeconvnode._second_set_to_set;
  2768. begin
  2769. second_set_to_set;
  2770. end;
  2771. procedure ttypeconvnode._second_ansistring_to_pchar;
  2772. begin
  2773. second_ansistring_to_pchar;
  2774. end;
  2775. procedure ttypeconvnode._second_class_to_intf;
  2776. begin
  2777. second_class_to_intf;
  2778. end;
  2779. procedure ttypeconvnode._second_char_to_char;
  2780. begin
  2781. second_char_to_char;
  2782. end;
  2783. procedure ttypeconvnode._second_nothing;
  2784. begin
  2785. second_nothing;
  2786. end;
  2787. procedure ttypeconvnode.second_call_helper(c : tconverttype);
  2788. const
  2789. secondconvert : array[tconverttype] of pointer = (
  2790. @ttypeconvnode._second_nothing, {none}
  2791. @ttypeconvnode._second_nothing, {equal}
  2792. @ttypeconvnode._second_nothing, {not_possible}
  2793. @ttypeconvnode._second_nothing, {second_string_to_string, handled in resultdef pass }
  2794. @ttypeconvnode._second_char_to_string,
  2795. @ttypeconvnode._second_nothing, {char_to_charray}
  2796. @ttypeconvnode._second_nothing, { pchar_to_string, handled in resultdef pass }
  2797. @ttypeconvnode._second_nothing, {cchar_to_pchar}
  2798. @ttypeconvnode._second_cstring_to_pchar,
  2799. @ttypeconvnode._second_cstring_to_int,
  2800. @ttypeconvnode._second_ansistring_to_pchar,
  2801. @ttypeconvnode._second_string_to_chararray,
  2802. @ttypeconvnode._second_nothing, { chararray_to_string, handled in resultdef pass }
  2803. @ttypeconvnode._second_array_to_pointer,
  2804. @ttypeconvnode._second_pointer_to_array,
  2805. @ttypeconvnode._second_int_to_int,
  2806. @ttypeconvnode._second_int_to_bool,
  2807. @ttypeconvnode._second_bool_to_bool,
  2808. @ttypeconvnode._second_bool_to_int,
  2809. @ttypeconvnode._second_real_to_real,
  2810. @ttypeconvnode._second_int_to_real,
  2811. @ttypeconvnode._second_nothing, { real_to_currency, handled in resultdef pass }
  2812. @ttypeconvnode._second_proc_to_procvar,
  2813. @ttypeconvnode._second_nil_to_methodprocvar,
  2814. @ttypeconvnode._second_nothing, { arrayconstructor_to_set }
  2815. @ttypeconvnode._second_nothing, { second_set_to_set, handled in first pass }
  2816. @ttypeconvnode._second_cord_to_pointer,
  2817. @ttypeconvnode._second_nothing, { interface 2 string }
  2818. @ttypeconvnode._second_nothing, { interface 2 guid }
  2819. @ttypeconvnode._second_class_to_intf,
  2820. @ttypeconvnode._second_char_to_char,
  2821. @ttypeconvnode._second_nothing, { dynarray_2_openarray }
  2822. @ttypeconvnode._second_nothing, { pwchar_2_string }
  2823. @ttypeconvnode._second_nothing, { variant_2_dynarray }
  2824. @ttypeconvnode._second_nothing, { dynarray_2_variant}
  2825. @ttypeconvnode._second_nothing, { variant_2_enum }
  2826. @ttypeconvnode._second_nothing, { enum_2_variant }
  2827. @ttypeconvnode._second_nothing, { variant_2_interface }
  2828. @ttypeconvnode._second_nothing, { interface_2_variant }
  2829. @ttypeconvnode._second_nothing { array_2_dynarray }
  2830. );
  2831. type
  2832. tprocedureofobject = procedure of object;
  2833. var
  2834. r : packed record
  2835. proc : pointer;
  2836. obj : pointer;
  2837. end;
  2838. begin
  2839. { this is a little bit dirty but it works }
  2840. { and should be quite portable too }
  2841. r.proc:=secondconvert[c];
  2842. r.obj:=self;
  2843. tprocedureofobject(r)();
  2844. end;
  2845. {*****************************************************************************
  2846. TISNODE
  2847. *****************************************************************************}
  2848. constructor tisnode.create(l,r : tnode);
  2849. begin
  2850. inherited create(isn,l,r);
  2851. end;
  2852. function tisnode.pass_typecheck:tnode;
  2853. var
  2854. paras: tcallparanode;
  2855. begin
  2856. result:=nil;
  2857. typecheckpass(left);
  2858. typecheckpass(right);
  2859. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2860. set_varstate(right,vs_read,[vsf_must_be_valid]);
  2861. if codegenerror then
  2862. exit;
  2863. { Passing a class type to an "is" expression cannot result in a class
  2864. of that type to be constructed.
  2865. }
  2866. include(right.flags,nf_ignore_for_wpo);
  2867. if (right.resultdef.typ=classrefdef) then
  2868. begin
  2869. { left must be a class }
  2870. if is_class(left.resultdef) then
  2871. begin
  2872. { the operands must be related }
  2873. if (not(tobjectdef(left.resultdef).is_related(
  2874. tobjectdef(tclassrefdef(right.resultdef).pointeddef)))) and
  2875. (not(tobjectdef(tclassrefdef(right.resultdef).pointeddef).is_related(
  2876. tobjectdef(left.resultdef)))) then
  2877. CGMessage2(type_e_classes_not_related,left.resultdef.typename,
  2878. tclassrefdef(right.resultdef).pointeddef.typename);
  2879. end
  2880. else
  2881. CGMessage1(type_e_class_type_expected,left.resultdef.typename);
  2882. { call fpc_do_is helper }
  2883. paras := ccallparanode.create(
  2884. left,
  2885. ccallparanode.create(
  2886. right,nil));
  2887. result := ccallnode.createintern('fpc_do_is',paras);
  2888. left := nil;
  2889. right := nil;
  2890. end
  2891. else if is_interface(right.resultdef) then
  2892. begin
  2893. { left is a class }
  2894. if is_class(left.resultdef) then
  2895. begin
  2896. { the class must implement the interface }
  2897. if tobjectdef(left.resultdef).find_implemented_interface(tobjectdef(right.resultdef))=nil then
  2898. CGMessage2(type_e_classes_not_related,
  2899. FullTypeName(left.resultdef,right.resultdef),
  2900. FullTypeName(right.resultdef,left.resultdef))
  2901. end
  2902. { left is an interface }
  2903. else if is_interface(left.resultdef) then
  2904. begin
  2905. { the operands must be related }
  2906. if (not(tobjectdef(left.resultdef).is_related(tobjectdef(right.resultdef)))) and
  2907. (not(tobjectdef(right.resultdef).is_related(tobjectdef(left.resultdef)))) then
  2908. CGMessage2(type_e_classes_not_related,
  2909. FullTypeName(left.resultdef,right.resultdef),
  2910. FullTypeName(right.resultdef,left.resultdef));
  2911. end
  2912. else
  2913. CGMessage1(type_e_class_type_expected,left.resultdef.typename);
  2914. { call fpc_do_is helper }
  2915. paras := ccallparanode.create(
  2916. left,
  2917. ccallparanode.create(
  2918. right,nil));
  2919. result := ccallnode.createintern('fpc_do_is',paras);
  2920. left := nil;
  2921. right := nil;
  2922. end
  2923. else
  2924. CGMessage1(type_e_class_or_interface_type_expected,right.resultdef.typename);
  2925. resultdef:=booltype;
  2926. end;
  2927. function tisnode.pass_1 : tnode;
  2928. begin
  2929. internalerror(200204254);
  2930. result:=nil;
  2931. end;
  2932. { dummy pass_2, it will never be called, but we need one since }
  2933. { you can't instantiate an abstract class }
  2934. procedure tisnode.pass_generate_code;
  2935. begin
  2936. end;
  2937. {*****************************************************************************
  2938. TASNODE
  2939. *****************************************************************************}
  2940. constructor tasnode.create(l,r : tnode);
  2941. begin
  2942. inherited create(asn,l,r);
  2943. call := nil;
  2944. end;
  2945. destructor tasnode.destroy;
  2946. begin
  2947. call.free;
  2948. inherited destroy;
  2949. end;
  2950. function tasnode.pass_typecheck:tnode;
  2951. var
  2952. hp : tnode;
  2953. begin
  2954. result:=nil;
  2955. typecheckpass(right);
  2956. typecheckpass(left);
  2957. set_varstate(right,vs_read,[vsf_must_be_valid]);
  2958. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2959. if codegenerror then
  2960. exit;
  2961. if (right.resultdef.typ=classrefdef) then
  2962. begin
  2963. { left must be a class }
  2964. if is_class(left.resultdef) then
  2965. begin
  2966. { the operands must be related }
  2967. if (not(tobjectdef(left.resultdef).is_related(
  2968. tobjectdef(tclassrefdef(right.resultdef).pointeddef)))) and
  2969. (not(tobjectdef(tclassrefdef(right.resultdef).pointeddef).is_related(
  2970. tobjectdef(left.resultdef)))) then
  2971. CGMessage2(type_e_classes_not_related,
  2972. FullTypeName(left.resultdef,tclassrefdef(right.resultdef).pointeddef),
  2973. FullTypeName(tclassrefdef(right.resultdef).pointeddef,left.resultdef));
  2974. end
  2975. else
  2976. CGMessage1(type_e_class_type_expected,left.resultdef.typename);
  2977. resultdef:=tclassrefdef(right.resultdef).pointeddef;
  2978. end
  2979. else if is_interface(right.resultdef) then
  2980. begin
  2981. { left is a class }
  2982. if not(is_class(left.resultdef) or
  2983. is_interfacecom(left.resultdef)) then
  2984. CGMessage1(type_e_class_or_cominterface_type_expected,left.resultdef.typename);
  2985. resultdef:=right.resultdef;
  2986. { load the GUID of the interface }
  2987. if (right.nodetype=typen) then
  2988. begin
  2989. if tobjectdef(right.resultdef).objecttype=odt_interfacecorba then
  2990. begin
  2991. if assigned(tobjectdef(right.resultdef).iidstr) then
  2992. begin
  2993. hp:=cstringconstnode.createstr(tobjectdef(right.resultdef).iidstr^);
  2994. tstringconstnode(hp).changestringtype(cshortstringtype);
  2995. right.free;
  2996. right:=hp;
  2997. end
  2998. else
  2999. internalerror(200902081);
  3000. end
  3001. else
  3002. begin
  3003. if assigned(tobjectdef(right.resultdef).iidguid) then
  3004. begin
  3005. if not(oo_has_valid_guid in tobjectdef(right.resultdef).objectoptions) then
  3006. CGMessage1(type_interface_has_no_guid,tobjectdef(right.resultdef).typename);
  3007. hp:=cguidconstnode.create(tobjectdef(right.resultdef).iidguid^);
  3008. right.free;
  3009. right:=hp;
  3010. end
  3011. else
  3012. internalerror(200206282);
  3013. end;
  3014. typecheckpass(right);
  3015. end;
  3016. end
  3017. else
  3018. CGMessage1(type_e_class_or_interface_type_expected,right.resultdef.typename);
  3019. end;
  3020. function tasnode.dogetcopy: tnode;
  3021. begin
  3022. result := inherited dogetcopy;
  3023. if assigned(call) then
  3024. tasnode(result).call := call.getcopy
  3025. else
  3026. tasnode(result).call := nil;
  3027. end;
  3028. function tasnode.docompare(p: tnode): boolean;
  3029. begin
  3030. result:=
  3031. inherited docompare(p) and
  3032. tasnode(p).call.isequal(call);
  3033. end;
  3034. function tasnode.pass_1 : tnode;
  3035. var
  3036. procname: string;
  3037. begin
  3038. result:=nil;
  3039. { Passing a class type to an "as" expression cannot result in a class
  3040. of that type to be constructed.
  3041. }
  3042. include(right.flags,nf_ignore_for_wpo);
  3043. if not assigned(call) then
  3044. begin
  3045. if is_class(left.resultdef) and
  3046. (right.resultdef.typ=classrefdef) then
  3047. call := ccallnode.createinternres('fpc_do_as',
  3048. ccallparanode.create(left,ccallparanode.create(right,nil)),
  3049. resultdef)
  3050. else
  3051. begin
  3052. if is_class(left.resultdef) then
  3053. if is_shortstring(right.resultdef) then
  3054. procname := 'fpc_class_as_corbaintf'
  3055. else
  3056. procname := 'fpc_class_as_intf'
  3057. else
  3058. procname := 'fpc_intf_as';
  3059. call := ccallnode.createintern(procname,
  3060. ccallparanode.create(right,ccallparanode.create(left,nil)));
  3061. call := ctypeconvnode.create_internal(call,resultdef);
  3062. end;
  3063. left := nil;
  3064. right := nil;
  3065. firstpass(call);
  3066. if codegenerror then
  3067. exit;
  3068. expectloc:=call.expectloc;
  3069. end;
  3070. end;
  3071. begin
  3072. ctypeconvnode:=ttypeconvnode;
  3073. casnode:=tasnode;
  3074. cisnode:=tisnode;
  3075. end.