ncnv.pas 117 KB

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