ncnv.pas 126 KB

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