ncnv.pas 121 KB

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