ncnv.pas 129 KB

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