ncnv.pas 128 KB

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