ncnv.pas 125 KB

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