ncnv.pas 124 KB

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