ncnv.pas 131 KB

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