ncnv.pas 128 KB

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